Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5e45ddb

Browse files
Chapter 05 codes updated.
1 parent c8474c1 commit 5e45ddb

File tree

14 files changed

+281
-331
lines changed

14 files changed

+281
-331
lines changed

‎05-Maze-Solver/Chapter-04-Completed-Codes/BFS-Maze-Solver/src/AlgoFrame.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import java.awt.Graphics2D;
2-
import java.awt.Graphics;
3-
import java.awt.Dimension;
4-
import java.awt.Color;
5-
import java.awt.RenderingHints;
6-
1+
import java.awt.*;
72
import javax.swing.*;
83

94
public class AlgoFrame extends JFrame{
105

116
private int canvasWidth;
127
private int canvasHeight;
13-
private JPanel canvas;
148

159
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1610

@@ -38,8 +32,8 @@ public AlgoFrame(String title){
3832
public int getCanvasHeight(){return canvasHeight;}
3933

4034
// data
41-
MazeData data;
42-
public void setData(MazeData data){
35+
privateMazeData data;
36+
public void render(MazeData data){
4337
this.data = data;
4438
repaint();
4539
}
@@ -70,16 +64,17 @@ public void paintComponent(Graphics g) {
7064

7165
for(int i = 0 ; i < data.N() ; i ++ )
7266
for(int j = 0 ; j < data.M() ; j ++){
73-
if (data.maze[i][j] == MazeData.WALL)
67+
if (data.getMaze(i,j) == MazeData.WALL)
7468
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
7569
else
7670
AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
7771

78-
if(data.inQueue[i][j])
79-
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
8072
if(data.path[i][j])
8173
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Yellow);
8274

75+
if(data.result[i][j])
76+
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
77+
8378
AlgoVisHelper.fillRectangle(g2d, j * w, i * h, w, h);
8479
}
8580
}

‎05-Maze-Solver/Chapter-04-Completed-Codes/BFS-Maze-Solver/src/AlgoVisHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,35 @@ private AlgoVisHelper(){}
3232
public static final Color White = new Color(0xFFFFFF);
3333

3434

35-
staticpublic void strokeCircle(Graphics2D g, int x, int y, int r){
35+
publicstatic void strokeCircle(Graphics2D g, int x, int y, int r){
3636

3737
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
3838
g.draw(circle);
3939
}
4040

41-
staticpublic void fillCircle(Graphics2D g, int x, int y, int r){
41+
publicstatic void fillCircle(Graphics2D g, int x, int y, int r){
4242

4343
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4444
g.fill(circle);
4545
}
4646

47-
staticpublic void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
47+
publicstatic void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
4848

4949
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5050
g.draw(rectangle);
5151
}
5252

53-
staticpublic void fillRectangle(Graphics2D g, int x, int y, int w, int h){
53+
publicstatic void fillRectangle(Graphics2D g, int x, int y, int w, int h){
5454

5555
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5656
g.fill(rectangle);
5757
}
5858

59-
staticpublic void setColor(Graphics2D g, Color color){
59+
publicstatic void setColor(Graphics2D g, Color color){
6060
g.setColor(color);
6161
}
6262

63-
staticpublic void setStrokeWidth(Graphics2D g, int w){
63+
publicstatic void setStrokeWidth(Graphics2D g, int w){
6464
int strokeWidth = w;
6565
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6666
}
Lines changed: 53 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,95 @@
1-
import javafx.geometry.Pos;
2-
31
import java.awt.*;
4-
import java.util.*;
2+
import java.util.LinkedList;
3+
import java.util.Stack;
54

65
public class AlgoVisualizer {
76

8-
private static int DELAY = 40;
7+
private static int DELAY = 5;
8+
private static int blockSide = 8;
99

1010
private MazeData data;
1111
private AlgoFrame frame;
12-
private final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
13-
14-
public AlgoVisualizer(AlgoFrame frame, MazeData data){
12+
private static final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
1513

16-
this.frame = frame;
17-
this.data = data;
14+
public AlgoVisualizer(String mazeFile){
1815

19-
this.setData(false, Position.invalidPosition(), false, Position.invalidPosition());
20-
}
16+
// 初始化数据
17+
data = new MazeData(mazeFile);
18+
int sceneHeight = data.N() * blockSide;
19+
int sceneWidth = data.M() * blockSide;
2120

22-
public void run(){
21+
// 初始化视图
22+
EventQueue.invokeLater(() -> {
23+
frame = new AlgoFrame("Maze Solver Visualization", sceneWidth, sceneHeight);
2324

24-
if(go())
25-
System.out.println("The maze solved.");
26-
else
27-
System.out.println("The maze has no solution.");
25+
new Thread(() -> {
26+
run();
27+
}).start();
28+
});
29+
}
2830

29-
findPath();
31+
privatevoidrun(){
3032

31-
this.setData(false, Position.invalidPosition(), false, Position.invalidPosition());
32-
AlgoVisHelper.pause(DELAY);
33-
}
33+
setData(-1, -1, false);
3434

35-
public boolean go(){
35+
LinkedList<Position> queue = new LinkedList<Position>();
36+
Position entrance = new Position(data.getEntranceX(), data.getEntranceY());
37+
queue.addLast(entrance);
38+
data.visited[entrance.getX()][entrance.getY()] = true;
3639

3740
boolean isSolved = false;
3841

39-
Queue<Position> queue = new LinkedList<>();
40-
queue.add(new Position(data.getEntranceX(), data.getEntranceY()));
41-
data.visited[data.getEntranceX()][data.getEntranceY()] = true;
42-
this.setData(false, Position.invalidPosition(), true, queue.peek());
43-
AlgoVisHelper.pause(DELAY);
44-
4542
while(queue.size() != 0){
46-
Position cur = queue.remove();
47-
this.setData(true, cur, false, cur);
48-
AlgoVisHelper.pause(DELAY);
43+
Position curPos = queue.pop();
44+
setData(curPos.getX(), curPos.getY(), true);
4945

50-
if(cur.x == data.getExitX() && cur.y == data.getExitY()){
46+
if(curPos.getX() == data.getExitX() && curPos.getY() == data.getExitY()){
5147
isSolved = true;
48+
findPath(curPos);
5249
break;
5350
}
5451

55-
for(int i = 0 ; i < 4 ; i ++){
56-
int newX = cur.x + d[i][0];
57-
int newY = cur.y + d[i][1];
58-
if(data.inArea(newX, newY) && !data.visited[newX][newY] && data.maze[newX][newY] == MazeData.ROAD){
59-
queue.add(new Position(newX, newY));
52+
for(int i = 0 ; i < 4 ; i ++){
53+
int newX = curPos.getX() + d[i][0];
54+
int newY = curPos.getY() + d[i][1];
55+
56+
if(data.inArea(newX, newY)
57+
&& !data.visited[newX][newY]
58+
&& data.getMaze(newX, newY) == MazeData.ROAD){
59+
queue.addLast(new Position(newX, newY, curPos));
6060
data.visited[newX][newY] = true;
61-
data.prev[newX][newY] = cur;
62-
this.setData(false, Position.invalidPosition(), true, new Position(newX, newY));
63-
AlgoVisHelper.pause(DELAY);
6461
}
6562
}
63+
6664
}
6765

68-
returnisSolved;
69-
}
66+
if(!isSolved)
67+
System.out.println("The maze has no Solution!");
7068

71-
private void findPath(){
69+
setData(-1, -1, false);
70+
}
7271

73-
data.clearTag(data.path);
74-
data.clearTag(data.inQueue);
72+
private void findPath(Position des){
7573

76-
Position cur = newPosition(data.getExitX(), data.getExitY());
74+
Position cur = des;
7775
while(cur != null){
78-
data.path[cur.x][cur.y] = true;
79-
this.setData(true, cur, false, Position.invalidPosition());
80-
AlgoVisHelper.pause(DELAY);
81-
82-
cur = data.prev[cur.x][cur.y];
76+
data.result[cur.getX()][cur.getY()] = true;
77+
cur = cur.getPrev();
8378
}
84-
85-
return;
8679
}
8780

88-
private void setData(booleanisPath, PositionpathPos, boolean inQueue, PositionqueuePos){
89-
if(data.inArea(pathPos.x, pathPos.y))
90-
data.path[pathPos.x][pathPos.y] = isPath;
91-
if(data.inArea(queuePos.x, queuePos.y))
92-
data.inQueue[queuePos.x][queuePos.y] = inQueue;
93-
frame.setData(data);
81+
private void setData(intx, inty, boolean isPath){
82+
if(data.inArea(x, y))
83+
data.path[x][y] = isPath;
84+
85+
frame.render(data);
86+
AlgoVisHelper.pause(DELAY);
9487
}
9588

9689
public static void main(String[] args) {
9790

98-
String filename = "maze_101_101.txt";
99-
MazeData data = new MazeData(filename);
100-
//data.print();
91+
String mazeFile = "maze_101_101.txt";
10192

102-
int blockSide = 8;
103-
int sceneHeight = data.N() * blockSide;
104-
int sceneWidth = data.M() * blockSide;
105-
106-
EventQueue.invokeLater(() -> {
107-
AlgoFrame frame = new AlgoFrame("Maze Solver Visualization", sceneWidth,sceneHeight);
108-
109-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
110-
new Thread(() -> {
111-
vis.run();
112-
}).start();
113-
});
93+
AlgoVisualizer vis = new AlgoVisualizer(mazeFile);
11494
}
11595
}

‎05-Maze-Solver/Chapter-04-Completed-Codes/BFS-Maze-Solver/src/MazeData.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ public class MazeData {
1414
private int exitX, exitY;
1515

1616
private int N, M;
17-
public char[][] maze;
18-
public boolean[][] visited;
17+
private char[][] maze;
1918
public boolean[][] path;
20-
public boolean[][] inQueue;
21-
public Position[][] prev;
19+
public boolean[][] visited;
20+
public boolean[][] result;
2221

2322
public MazeData(String filename){
2423

@@ -34,38 +33,34 @@ public MazeData(String filename){
3433
FileInputStream fis = new FileInputStream(file);
3534
scanner = new Scanner(new BufferedInputStream(fis), "UTF-8");
3635

36+
// 读取第一行
3737
String nmline = scanner.nextLine();
38+
String[] nm = nmline.trim().split("\\s+");
39+
//System.out.print(nm[0] + ' ' + nm[1]);
3840

39-
Scanner nmScanner = null;
40-
nmScanner = new Scanner(nmline);
41-
N = nmScanner.nextInt();
42-
M = nmScanner.nextInt();
43-
nmScanner.close();
44-
if(N < 2 || M < 1)
45-
throw new IllegalArgumentException("The size of maze is invalid.");
41+
N = Integer.parseInt(nm[0]);
42+
// System.out.println("N = " + N);
43+
M = Integer.parseInt(nm[1]);
44+
// System.out.println("M = " + M);
4645

47-
maze = newchar[N][M];
46+
// 读取后续的N行
4847
visited = new boolean[N][M];
4948
path = new boolean[N][M];
50-
inQueue = new boolean[N][M];
51-
prev = new Position[N][M];
49+
result = new boolean[N][M];
50+
maze = new char[N][M];
5251
for(int i = 0 ; i < N ; i ++){
5352
String line = scanner.nextLine();
53+
54+
// 每行保证有M个字符
5455
if(line.length() != M)
5556
throw new IllegalArgumentException("Maze file " + filename + " is invalid");
5657
for(int j = 0 ; j < M ; j ++){
5758
maze[i][j] = line.charAt(j);
5859
visited[i][j] = false;
5960
path[i][j] = false;
60-
inQueue[i][j] = false;
61-
prev[i][j] = null;
61+
result[i][j] = false;
6262
}
6363
}
64-
65-
entranceX = 1;
66-
entranceY = 0;
67-
exitX = N - 2;
68-
exitY = M - 1;
6964
}
7065
catch(IOException e){
7166
e.printStackTrace();
@@ -74,14 +69,24 @@ public MazeData(String filename){
7469
if(scanner != null)
7570
scanner.close();
7671
}
72+
73+
entranceX = 1;
74+
entranceY = 0;
75+
exitX = N - 2;
76+
exitY = M - 1;
7777
}
7878

7979
public int N(){ return N; }
8080
public int M(){ return M; }
81-
public int getEntranceX(){ return entranceX; }
82-
public int getEntranceY(){ return entranceY; }
83-
public int getExitX(){ return exitX; }
84-
public int getExitY(){ return exitY; }
81+
public int getEntranceX(){return entranceX;}
82+
public int getEntranceY(){return entranceY;}
83+
public int getExitX(){return exitX;}
84+
public int getExitY(){return exitY;}
85+
public char getMaze(int i, int j){
86+
if(!inArea(i,j))
87+
throw new IllegalArgumentException("i or j is out of index in getMaze!");
88+
return maze[i][j];
89+
}
8590

8691
public boolean inArea(int x, int y){
8792
return x >= 0 && x < N && y >= 0 && y < M;
@@ -97,10 +102,4 @@ public void print(){
97102
return;
98103
}
99104

100-
public void clearTag(boolean[][] tag){
101-
for(int i = 0 ; i < N ; i ++)
102-
for(int j = 0 ; j < M ; j ++)
103-
tag[i][j] = false;
104-
return;
105-
}
106105
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
public class Position {
22

3-
public int x, y;
3+
private int x, y;
4+
private Position prev;
45

5-
public Position(int x, int y){
6+
public Position(int x, int y, Positionprev){
67
this.x = x;
78
this.y = y;
9+
this.prev = prev;
810
}
911

10-
public staticPositioninvalidPosition(){
11-
returnnewPosition(-1, -1);
12+
public Position(intx, inty){
13+
this(x, y, null);
1214
}
15+
16+
public int getX(){return x;}
17+
public int getY(){return y;}
18+
public Position getPrev(){return prev;}
1319
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /