Maze solving: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Switch from draft to complete task)
(Using a smaller example)
Line 26: Line 26:
Using the maze produced in [[Maze generation#PicoLisp]], this finds the shortest
Using the maze produced in [[Maze generation#PicoLisp]], this finds the shortest
path from the top-left cell 'a12' to the bottom-righ exit 'p1':
path from the top-left cell 'a12' to the bottom-righ exit 'p1':
<pre>: (shortestPath 'a12 'p1 (maze 16 12))
<pre>: (shortestPath 'a8 'k1 (maze 11 8))
+ +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
+ +---+---+---+---+---+---+---+---+---+---+
12 | # | # # # | # # # | # # # # | # # # |
8 | # # # | # # | |
+ + + + + +---+ + +---+---+ + +---+ +---+ +
+ + + + + + +---+ +---+---+ +
11 | # # | | # # | | # # | | # # | | # # | # # |
7 | | | # # | # | | | | |
+ +---+ +---+---+ +---+---+ + +---+---+ +---+ +---+
+---+ +---+---+ + + +---+ + + +
10 | | | | | # # | # # | # # | # # | |
6 | | | # | | | | |
+ + + +---+ + + + + +---+---+ +---+ +---+ +
+ +---+ +---+ +---+---+---+ + +---+
9 | | | | | # | # # | # # | # | # # | # # |
5 | | | # # # # | | |
+ +---+---+ + +---+ +---+---+ + + + +---+ + +
+---+ +---+ +---+---+---+ +---+---+ +
8 | | | | # # # | # | # # | # | # | # |
4 | | | | | # | # # # |
+ + + + + +---+---+ +---+ +---+---+ +---+ + +
+ +---+ +---+ +---+ + + +---+ +
7 | | | | # # # | | # # | # # | # # | # |
3 | | | | | # | # # | # |
+---+ +---+---+---+---+ +---+ +---+ + +---+ +---+ +
+ +---+---+ + + + + +---+ + +
6 | | | | # | | | # | # # # | | # |
2 | | | | | | # | # # | # |
+ +---+ +---+ +---+ + +---+ + +---+---+---+ + +
+ + + +---+ + +---+ + +---+ +
5 | | | # # | | # | | # |
1 | | | # # | #
+---+ +---+ +---+ +---+ +---+---+ + +---+ +---+ +
+---+---+---+---+---+---+---+---+---+---+---+
4 | | | | # # | | # # | # | | | # # |
a b c d e f g h i j k</pre>
+ +---+ +---+ +---+ +---+ + + +---+---+ + +---+
3 | | | | | # # # | # | # # # | | # # |
+ +---+---+ + + +---+---+---+ +---+ + + +---+ +
2 | | | | | | # # | | # | # # # |
+ + + +---+ + + +---+ +---+ +---+ + +---+---+
1 | | | | # # # | # # #
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
a b c d e f g h i j k l m n o p</pre>

Revision as of 08:20, 22 December 2010

Task
Maze solving
You are encouraged to solve this task according to the task description, using any language you may know.

For a maze generated by this task, write a function that finds (and displays) the shortest path between two cells. Note that because these mazes are generated by the Depth-first search algorithm, they contain no circular paths, and a simple depth-first tree search can be used.

PicoLisp

<lang PicoLisp>(de shortestPath (This Goal Maze)

  (let (Path NIL  Best NIL)
     (recur (This Path)
        (when (and This (not (: mark)))
           (push 'Path This)
           (if (== Goal This)
              (unless (and Best (>= (length Path) (length Best)))
                 (setq Best Path) )
              (=: mark T)
              (recurse (: west) Path)
              (recurse (: east) Path)
              (recurse (: south) Path)
              (recurse (: north) Path)
              (=: mark NIL) ) ) )
     (disp Maze 0
        '((This) (if (memq This Best) " # " "   ")) ) ) )</lang>

Using the maze produced in Maze generation#PicoLisp, this finds the shortest path from the top-left cell 'a12' to the bottom-righ exit 'p1':

: (shortestPath 'a8 'k1 (maze 11 8))
   +   +---+---+---+---+---+---+---+---+---+---+
 8 | #   #   # | #   # |                       |
   +   +   +   +   +   +   +---+   +---+---+   +
 7 |   |   | #   # | # |   |       |       |   |
   +---+   +---+---+   +   +   +---+   +   +   +
 6 |   |       |     # |   |           |   |   |
   +   +---+   +---+   +---+---+---+   +   +---+
 5 |       |       | #   #   #   # |   |       |
   +---+   +---+   +---+---+---+   +---+---+   +
 4 |   |       |       |       | # | #   #   # |
   +   +---+   +---+   +---+   +   +   +---+   +
 3 |       |       |   |       | # | #   # | # |
   +   +---+---+   +   +   +   +   +---+   +   +
 2 |       |       |   |   |   | # | #   # | # |
   +   +   +   +---+   +   +---+   +   +---+   +
 1 |   |               |         #   # |     #
   +---+---+---+---+---+---+---+---+---+---+---+
     a   b   c   d   e   f   g   h   i   j   k