1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| public class Labyrinth { public static int EndX = 8; public static int EndY = 10;
public static int[][] MAZE = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1}, {1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1}, {1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1}, {1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1}, {1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, };
public static void main(String[] args) { int i,j,x,y; TraceRecord record = new TraceRecord(); x=y=1;
while (x<=EndX && y<=EndY){ MAZE[x][y] = 2; if (MAZE[x-1][y] == 0){ x-=1; record.push(x,y); }else if (MAZE[x+1][y] == 0){ x+=1; record.push(x,y); }else if (MAZE[x][y-1] == 0){ y-=1; record.push(x,y); }else if (MAZE[x][y+1] == 0){ y+=1; record.push(x,y); }else if (isExit(x,y)){ break; }else { MAZE[x][y] = 2; TraceRecord.Node node = record.pop(); x = node.x; y = node.y; } } }
public static boolean isExit(int x, int y){ if (x == EndX && y == EndY){ return true; } return false; }
}
|