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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
public class Mouse {
private int startI, startJ; // 入口
private int endI, endJ; // 出口
public static void main(String[] args) {
int maze[][] = {{2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 2, 0, 2, 2, 0, 2},
{2, 0, 2, 0, 0, 2, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 2},
{2, 2, 0, 2, 2, 0, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2}};
System.out.println("显示迷宫:");
for(int i = 0; i < maze.length; i++) {
for(int j = 0; j < maze[0].length; j++)
if(maze[i][j] == 2)
System.out.print("█");
else
System.out.print(" ");
System.out.println();
}
Mouse mouse = new Mouse();
mouse.setStart(1, 1);
mouse.setEnd(7, 7);
mouse.go(maze);
}
public void setStart(int i, int j) {
this.startI = i;
this.startJ = j;
}
public void setEnd(int i, int j) {
this.endI = i;
this.endJ = j;
}
public void go(int[][] maze) {
visit(maze, startI, startJ);
}
private void visit(int[][] maze, int i, int j) {
maze[i][j] = 1;
if(i == endI && j == endJ) {
System.out.println("\n找到出口!");
for(int m = 0; m < maze.length; m++) {
for(int n = 0; n < maze[0].length; n++) {
if(maze[m][n] == 2)
System.out.print("█");
else if(maze[m][n] == 1)
System.out.print("◇");
else
System.out.print(" ");
}
System.out.println();
}
}
if(maze[i][j+1] == 0)
visit(maze, i, j+1);
if(maze[i+1][j] == 0)
visit(maze, i+1, j);
if(maze[i][j-1] == 0)
visit(maze, i, j-1);
if(maze[i-1][j] == 0)
visit(maze, i-1, j);
maze[i][j] = 0;
}
}
|