-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathAlgoVisualizer.java
94 lines (69 loc) · 2.48 KB
/
AlgoVisualizer.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.awt.*;
import java.util.Stack;
public class AlgoVisualizer {
private static int DELAY = 5;
private static int blockSide = 8;
private MazeData data;
private AlgoFrame frame;
private static final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
public AlgoVisualizer(String mazeFile){
// 初始化数据
data = new MazeData(mazeFile);
int sceneHeight = data.N() * blockSide;
int sceneWidth = data.M() * blockSide;
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Maze Solver Visualization", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
private void run(){
setData(-1, -1, false);
Stack<Position> stack = new Stack<Position>();
Position entrance = new Position(data.getEntranceX(), data.getEntranceY());
stack.push(entrance);
data.visited[entrance.getX()][entrance.getY()] = true;
boolean isSolved = false;
while(!stack.empty()){
Position curPos = stack.pop();
setData(curPos.getX(), curPos.getY(), true);
if(curPos.getX() == data.getExitX() && curPos.getY() == data.getExitY()){
isSolved = true;
findPath(curPos);
break;
}
for(int i = 0 ; i < 4 ; i ++){
int newX = curPos.getX() + d[i][0];
int newY = curPos.getY() + d[i][1];
if(data.inArea(newX, newY)
&& !data.visited[newX][newY]
&& data.getMaze(newX, newY) == MazeData.ROAD){
stack.push(new Position(newX, newY, curPos));
data.visited[newX][newY] = true;
}
}
}
if(!isSolved)
System.out.println("The maze has no Solution!");
setData(-1, -1, false);
}
private void findPath(Position des){
Position cur = des;
while(cur != null){
data.result[cur.getX()][cur.getY()] = true;
cur = cur.getPrev();
}
}
private void setData(int x, int y, boolean isPath){
if(data.inArea(x, y))
data.path[x][y] = isPath;
frame.render(data);
AlgoVisHelper.pause(DELAY);
}
public static void main(String[] args) {
String mazeFile = "maze_101_101.txt";
AlgoVisualizer vis = new AlgoVisualizer(mazeFile);
}
}