Skip to content

Instantly share code, notes, and snippets.

@fguillen
Created February 27, 2012 17:15
Show Gist options
  • Save fguillen/1925593 to your computer and use it in GitHub Desktop.
Save fguillen/1925593 to your computer and use it in GitHub Desktop.
Coding Puzzle example

EvasiveMouse

An smart mouse always obtains the cheese without being detected.

Description

You are an office mouse, and you're very lucky because in every office of the building where you live there is always one piece of cheese.

You have worked hard to make one mousehole in every office.

Now what remains is the easy part: go out from the mousehole and run until the cheese.

The only problem is that in the offices there are clerks, they are not looking for you but you better keep as far as you can from the clerks.

Problem

Show to the mouse the path he has to take:

  • The path has to be the safest one.
  • The path has to be the shortest path that fulfills the previous statement.
  • You move only horizontally and vertically, not diagonal.
  • You only can walk in an empty tile.

How to know if a path is safer than another

The one which exposes you to the least risky tiles.

The risky of any tile is inversely proportional to the minimum distance for this tile to any of the clerks.

The risky of a tile is exponential not lineal, which means that the risky of a tile that is 1 tile distance for a clerk is infinitely bigger than other that is 2 tiles distance for a clerk. So it is always safer to cross through a path that exposes you several times to a 2 tiles distance of a clerk rather than other that exposes you only once to a 1 tile distance of a clerk.

The risky of a tile is not accumulative which means that a tile which is 2 tiles distance from two clerks is the same than other that is 2 tiles distance from only one clerk. The only number that means is the minimum of all clerk distances.

How to know if a path is shorter than another

Well, just comparing the number of steps :)

How to choose between two path that are same safeness and length

It is not necessary since we are only asking for the number of steps.

Input

You have all the offices maps in your building in a plain text file.

Each map is separated with an empty line.

The file looks like this:

---------
|      #|
|   * * |
|       |
|       |
----U----

---------
|   *   |
U   *   |
|       |
|       |
| *     |
|     # |
---------

Where:

  • #: is the cheese
  • *: is a clerk
  • U: is the mousehole
  • -, |: are walls
  • : this is an empty tile, you can walk over it!

Output

Just the number of steps of the path you have chosen of every map in the file.

Every number in a line.

The mousehole position DOES count.

The cheese position DOES count.

8
11

Examples

The safest one

Se how the mouse keep as far from the clerk as it can.

Input

---------
|      #|
|       |
|       |
|     * |
|       |
|       |
-------U-

Output

19

Explanation

Even when the shortest way would be this (wrong):

---------
|      #|
|      .|
|      .|
|     *.|
|      .|
|      .|
-------U-

The most important thing is remains as safest as possible so this is because the mouse takes a very longer way (correct):

---------
|......#|
|.      |
|.      |
|.    * |
|.      |
|.......|
-------U-

The suicidal mouse

This map has a solution that doesn't look correct because it looks like the mouse is going directly to the risk. But at the end you will understand that this solution is the best one.

Input

---------
|      #|
|   * * |
|       |
|       |
----U----

Output

8

Explanation

There are multiple paths those carry us until the cheese but the safest and shortest one is this (correct):

---------
|      #|
|   * *.|
|      .|
|   ....|
----U----

Let's see another paths that are not the correct one (wrong):

---------
|......#|
|.  * * |
|.      |
|....   |
----U----

This one looks safer because you are taking a detour and instance of going directly to one of the clerks you are trying to keep far from them. But this is only working in the beginning because as far you achieve the top corner you are taking as much risks as in the correct solution and not only once but twice.

A long way

In this example the mouse looks like it is going too far, but you will realize that is safer that the short way.

Input

-------------
|           |
|           U
|*   * * * *|
|           |
|     #     |
|           |
|           |
-------------

Output

22

Explanation

This is the safest and shortest way (correct):

-------------
|  .........|
|  .       .U
|* . * * * *|
|  .        |
|  .  #     |
|  .  .     |
|  ....     |
-------------

You can think that once you are almost close to the cheese why not just turn to its direction like this (wrong):

-------------
|  .........|
|  .       .U
|* . * * * *|
|  .        |
|  ...#     |
|           |
|           |
-------------

Which is only 18 steps. You are taking a very shorter way but this new two steps are more risky than every one of the extra steps in the correct path.

---------
| #|
| * * |
| |
| |
----U----
-------------
| * |
| U
| * |
| |
| * |
| |
|# * |
-------------
-----------------------
| * |
| U
| |
| |
| * * * * |
| |
| * # |
| |
| |
| * |
| |
| |
| |
| |
-----------------------
-----------------------
| |
| U
| |
| |
| * * * * |
| |
| * # |
| |
| |
| * |
| |
| |
| |
| |
-----------------------
-----------------------
|* |
| U
| |
| * * * * |
| |
| |
| * |
| * |
| * # |
| * * * |
| |
| |
| |
| |
-----------------------
---------
| |
| #|
| * * |
| |
----U----
-----------------------
| |
| U
| |
| |
| |
| |
| * * |
| * * |
| * # * |
| * * * |
| |
| |
| |
| |
-----------------------
-----------------------
| |
| U
| |
| |
| |
| |
| * * |
| * * |
| * # * |
| * * |
| |
| |
| |
| |
-----------------------
-----------------------
| |
| U
| |
| |
| |
| |
| * * |
|* * * |
| * # * |
| * * |
| |
| |
| |
| |
-----------------------
-----------------------
| |
| U
| |
| |
| |
| |
| * * |
|* * * *|
| * # * |
| * * |
| |
| |
| |
| |
-----------------------
-----------------------
| |
| U
| |
| * * |
| * * |
| * # * |
| * * |
| |
| |
| |
| |
| |
| |
| |
-----------------------
-------------
| * |
| * U
| * |
| |
| |
| |
|# * |
-------------
-----------------------
| # |
| |
| |
| * |
| |
| * |
| |
| * |
| |
| * |
| |
| |
| |
| |
---------U-------------
---------
| #|
| |
| |
| * |
| |
| |
-------U-
-------------
| ****** |
| |
| #|
| |
| ****** |
| |
| |
| |
---------U---
-------------
| * |
| U
| * ---
| |
| * * |
| |
| ---
|# |
-------------
-------------
| * |
| U
| * |--
| |-------
| * * |
| ----- |
| --| | |
|# | | |
| ------- |
| |
---------------------|
-------------
| * |
| * U
| * |
| |
| |
| |
|# * |
-------------
-------------
| |
| U
| |
| * |
| |
| |
| #|
-------------
-------------
| |
| * U
| |
| |
| |
| |
|# * |
-------------
-------------
| |
| U
|* * * * *|
| |
| # |
| |
| |
-------------
-------------
| |
U * #|
| |
| |
| |
| |
| * |
-------------
-------------
| # |
U * |
| * |
| * |
| * |
| |
| |
-------------
-------------
| # |
U * |
| * |
| * |
| * |
| |
| * |
-------------
----------U--
| |
| * * |
| * * |
| * # * |
| * * |
| |
| |
-------------
-----------------------
| * |
| U
| |
| |
| * * * * |
| |
| * # |
| |
| |
| * |
| |
| |
| |
| |
-----------------------
-----------------------
| * |
| |
| |
| * * * * |
| * * * * |
| * * * * |
| * * * * |
| # * * * |
| * * * * |
| * * * * |
| * * * * U
-----------------------
-----------------------
| * # |
U |
| * |
| |
| * |
| |
| * |
| |
| * |
| * |
| |
| |
| |
| * |
-----------------------
-----------------------
| # |
| * |
| * |
| * |
| * |
| * |
| * |
| |
| |
| * |
| * |
| * |
| * |
U |
-----------------------
-----------------------
| # |
| |
| |
| * |
| |
| * |
| |
| * |
| |
| * |
| |
| |
| |
| |
---------U-------------
---U-------------------
| * # |
| |
| |
| * |
| *|
| |
| * |
| |
| *|
| |
| |
| |
| |
| |
-----------------------
-----------------------
| |
| U
| |
| * * |
| * * |
| * # * |
| * * |
| |
| |
| |
| |
| |
| |
| |
-----------------------
-----------------------
| # |
| |
| * |
| * |
| * U
| * |
| * |
| |
| |
| |
| |
| |
| |
| |
-----------------------
-----------------------
| |
| * |
| * |
| * * |
|# |
| U
| * * |
| * |
| |
| * |
| |
| * |
| |
| |
-----------------------
-----------------------
| # |
| |
| |
|* **** ***** |
| |
| |
| |
| |
| |
| |
| |
| |
| |
U * |
-----------------------
Calculating map 1
Steps: 8
---------
| #|
| * *.|
| .|
| ....|
----U----
Calculating map 2
Steps: 17
-------------
| * |
| .U
| * .|
| .|
| * ........|
| . |
|#... * |
-------------
Calculating map 3
Steps: 8
-----------------------
| * |
| .U
| .|
| .|
| * * * * .|
| .|
| * #.|
| |
| |
| * |
| |
| |
| |
| |
-----------------------
Calculating map 4
Steps: 62
-----------------------
|.....................|
|. .U
|. |
|. |
|. * * * * |
|. |
|. * # |
|. . |
|. . |
|. * . |
|. . |
|. . |
|. . |
|.................... |
-----------------------
Calculating map 5
Steps: 74
-----------------------
|* ..................|
| . .U
| . |
| .. * * * * |
| . |
| .. |
| . * ..... |
| . * . .. |
| . * #. ... |
|.. * * * ..|
|. .|
|. .|
|. .|
|.....................|
-----------------------
Calculating map 6
Steps: 15
---------
|.......|
|. #|
|. * * |
|.... |
----U----
Calculating map 7
Steps: 20
-----------------------
| ..........|
| . .U
| . |
| . |
| . |
| . |
| * . * |
| * . * |
| * # * |
| * * * |
| |
| |
| |
| |
-----------------------
Calculating map 8
Steps: 52
-----------------------
|.....................|
|. .U
|. |
|. |
|. |
|. |
|. * * |
|. * * |
|. * # * |
|. * . * |
|. . |
|. . |
|. . |
|............ |
-----------------------
Calculating map 9
Steps: 28
-----------------------
| |
| .U
| .|
| .|
| .|
| .|
| * * .|
|* * * .|
| * # * .|
| * . * .|
| . .|
| . .|
| . .|
| ..........|
-----------------------
Calculating map 10
Steps: 46
-----------------------
| .................|
| . .U
| .. |
| . |
| . |
| . |
| . * * |
|* . * * *|
| .. * # * |
| . * . * |
| . . |
| . . |
| . . |
| ........ |
-----------------------
Calculating map 11
Steps: 33
-----------------------
| |
| .U
| .|
| * * .|
| * * .|
| * # * .|
| * . * .|
| . .|
| . .|
| . .|
| . .|
| . .|
| . .|
| ............|
-----------------------
Calculating map 12
Steps: 17
-------------
| * |
| * .U
| * ...|
| .. |
| .. |
| .. |
|#..... * |
-------------
Calculating map 13
Steps: 30
-----------------------
|.......# |
|. |
|. |
|. * |
|. |
|. * |
|. |
|. * |
|. |
|. * |
|. |
|. |
|. |
|......... |
---------U-------------
Calculating map 14
Steps: 19
---------
|......#|
|. |
|. |
|. * |
|. |
|.......|
-------U-
Calculating map 15
Steps: 25
-------------
| ****** |
| |
|..........#|
|. |
|. ****** |
|. |
|. |
|......... |
---------U---
Calculating map 1
Steps: 22
-------------
| * |
| .U
| * .---
| ...|
| * * .|
| ...|
| .---
|#..........|
-------------
Calculating map 2
Steps: 40
-------------
| * |
| .U
| * .|--
| ...|-------
| * * ........|
| ----- .|
| --| | .|
|# | | .|
|. ------- .|
|....................|
---------------------|
Calculating map 1
Steps: 17
-------------
| * |
| * .U
| * ...|
| .. |
| .. |
| .. |
|#..... * |
-------------
Calculating map 2
Steps: 29
-------------
|...........|
|. .U
|. |
|. * |
|. |
|. |
|..........#|
-------------
Calculating map 3
Steps: 17
-------------
| |
| * .U
| .|
| .|
|...........|
|. |
|# * |
-------------
Calculating map 4
Steps: 22
-------------
| .........|
| . .U
|* . * * * *|
| . |
| . # |
| . . |
| .... |
-------------
Calculating map 5
Steps: 16
-------------
| |
U. * #|
|. .|
|...........|
| |
| |
| * |
-------------
Calculating map 6
Steps: 25
-------------
| #..|
U. * .|
|. * .|
|. * .|
|. * .|
|. .|
|...........|
-------------
Calculating map 7
Steps: 11
-------------
|........# |
U. * |
| * |
| * |
| * |
| |
| * |
-------------
Calculating map 8
Steps: 19
----------U--
| ..|
| * * .|
| * * .|
| * # * .|
| * . * .|
| . .|
| ........|
-------------
Calculating map 1
Steps: 8
-----------------------
| * |
| .U
| .|
| .|
| * * * * .|
| .|
| * #.|
| |
| |
| * |
| |
| |
| |
| |
-----------------------
Calculating map 2
Steps: 39
-----------------------
| ... * .........|
| . ....... .|
| . .|
| * . * * * .|
| * .* * * .|
| * . * * * .|
| * ..* * * .|
| #. * * * .|
| * * * * .|
| * * * * .|
| * * * * .U
-----------------------
Calculating map 3
Steps: 44
-----------------------
| * #.|
U. .|
|. * .|
|. .|
|. * .|
|. .|
|. * .|
|. .|
|. * .|
|. * .|
|. .|
|.....................|
| |
| * |
-----------------------
Calculating map 4
Steps: 50
-----------------------
| #.|
| * .|
| * .|
| * .|
| * .|
| * .|
|.......... * .|
|. .. .|
|. .. .|
|. * . .|
|. * .. .|
|. * .. .|
|. * . .|
U. ........|
-----------------------
Calculating map 5
Steps: 30
-----------------------
|.......# |
|. |
|. |
|. * |
|. |
|. * |
|. |
|. * |
|. |
|. * |
|. |
|. |
|. |
|......... |
---------U-------------
Calculating map 6
Steps: 49
---U-------------------
|... * .....# |
|. . |
|. . |
|. * . |
|. . *|
|. . |
|. * . |
|. .. |
|. . *|
|. . |
|. . |
|. . |
|. . |
|.............. |
-----------------------
Calculating map 7
Steps: 33
-----------------------
| |
| .U
| .|
| * * .|
| * * .|
| * # * .|
| * . * .|
| . .|
| . .|
| . .|
| . .|
| . .|
| . .|
| ............|
-----------------------
Calculating map 8
Steps: 45
-----------------------
|.# |
|. |
|. * |
|. * |
|. * .U
|. * .|
|. * .|
|. .|
|. .|
|. .|
|. .|
|. .|
|. .|
|.....................|
-----------------------
Calculating map 9
Steps: 39
-----------------------
| |
| * |
| * |
| * * |
|# |
|. .U
|. * * .|
|. * .|
|. .|
|......... * .|
| . .|
| * . .|
| .. .|
| ............|
-----------------------
Calculating map 10
Steps: 34
-----------------------
| ........# |
| . |
| . |
|* **** . ***** |
| . |
| . |
| . |
| . |
| .... |
| .... |
| .. |
| ... |
| .. |
U.. * |
-----------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment