Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you might want to try <a href="http://en.wikipedia.org/wiki/Backtracking" rel="nofollow">backtracking</a></p> <p>here's a complete example of how to solve this problem... however: it can't operate on a maze with "islands" in it as it would be necessary to aditionally track where you have been already. but i think you can figure out how to do this as well... </p> <p>the output should be:<br> <code>right, up, up, up, right, right, right, up, up, up, up, right, right, down, down, right, right, up, up, right, finish! </code></p> <pre><code>import java.awt.Point; public class Maze { enum Direction { up, right, down, left } static Direction[] dirs = Direction.values(); int[][] maze = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, { 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 }, { 1, 1, 1, 1, 0, 1, 0, 0, 0, 1 }, { 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 }, { 1, 0, 0, 0, 0, 1, 1, 1, 1, 1 }, { 1, 0, 1, 1, 0, 1, 1, 1, 1, 1 }, { 1, 0, 1, 0, 0, 0, 0, 1, 1, 1 }, { 0, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; Point start = new Point(0, 8); Point finish = new Point(9, 1); Point go(Direction dir, Point from) { Point result = new Point(from); switch (dir) { case up: if ((from.y == 0) || (maze[from.y - 1][from.x] != 0)) return null; result.translate(0, -1); break; case right: if ((from.x == maze[0].length) || (maze[from.y][from.x + 1] != 0)) return null; result.translate(1, 0); break; case down: if ((from.y == maze.length) || (maze[from.y + 1][from.x] != 0)) return null; result.translate(0, 1); break; case left: if ((from.x == 0) || (maze[from.y][from.x - 1] != 0)) return null; result.translate(-1, 0); break; } return result; } String tryToGo(Direction dir, Point from) { String result; Point newPosition = go(dir, from); if (newPosition == null) return null; else if (newPosition.equals(start)) return null; else if (newPosition.equals(finish)) return "finish!"; else { for (Direction newDir : dirs) { switch (newDir) { case up: if (dir == Direction.down) continue; break; case down: if (dir == Direction.up) continue; break; case left: if (dir == Direction.right) continue; break; case right: if (dir == Direction.left) continue; break; } result = tryToGo(newDir, newPosition); if (result == null) continue; else return newDir + ", " + result; } return null; } } public static void main(String[] args) { Maze maze = new Maze(); System.out.println("right" + maze.tryToGo(Direction.right, maze.start)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload