Skip to content

Commit 1baca8c

Browse files
committed
Chapter 05 codes updated.
1 parent dfd4ba7 commit 1baca8c

File tree

17 files changed

+320
-381
lines changed

17 files changed

+320
-381
lines changed

05-Maze-Solver/06-Non-Recursive-DFS-Maze-Solver/src/AlgoFrame.java

Lines changed: 4 additions & 10 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+
private MazeData data;
36+
public void render(MazeData data){
4337
this.data = data;
4438
repaint();
4539
}
@@ -70,7 +64,7 @@ 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);

05-Maze-Solver/06-Non-Recursive-DFS-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-
static public void strokeCircle(Graphics2D g, int x, int y, int r){
35+
public static 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-
static public void fillCircle(Graphics2D g, int x, int y, int r){
41+
public static 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-
static public void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
47+
public static 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-
static public void fillRectangle(Graphics2D g, int x, int y, int w, int h){
53+
public static 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-
static public void setColor(Graphics2D g, Color color){
59+
public static void setColor(Graphics2D g, Color color){
6060
g.setColor(color);
6161
}
6262

63-
static public void setStrokeWidth(Graphics2D g, int w){
63+
public static 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: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,77 @@
11
import java.awt.*;
2-
import java.util.*;
2+
import java.util.Stack;
33

44
public class AlgoVisualizer {
55

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

89
private MazeData data;
910
private AlgoFrame frame;
10-
private final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
11+
private static final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
1112

12-
public AlgoVisualizer(AlgoFrame frame, MazeData data){
13+
public AlgoVisualizer(String mazeFile){
1314

14-
this.frame = frame;
15-
this.data = data;
16-
17-
this.setData(false, -1, -1);
18-
}
19-
20-
public void run(){
15+
// 初始化数据
16+
data = new MazeData(mazeFile);
17+
int sceneHeight = data.N() * blockSide;
18+
int sceneWidth = data.M() * blockSide;
2119

22-
if(go())
23-
System.out.println("The maze solved.");
24-
else
25-
System.out.println("The maze has no solution.");
20+
// 初始化视图
21+
EventQueue.invokeLater(() -> {
22+
frame = new AlgoFrame("Maze Solver Visualization", sceneWidth, sceneHeight);
2623

27-
this.setData(false, -1, -1);
28-
AlgoVisHelper.pause(DELAY);
24+
new Thread(() -> {
25+
run();
26+
}).start();
27+
});
2928
}
3029

31-
public boolean go(){
30+
public void run(){
3231

33-
boolean isSolved = false;
32+
setData(-1, -1, false);
3433

3534
Stack<Position> stack = new Stack<Position>();
36-
stack.push(new Position(data.getEntranceX(), data.getEntranceY()));
37-
data.visited[data.getEntranceX()][data.getEntranceY()] = true;
35+
Position entrance = new Position(data.getEntranceX(), data.getEntranceY());
36+
stack.push(entrance);
37+
data.visited[entrance.x][entrance.y] = true;
3838

3939
while(!stack.empty()){
40-
Position cur = stack.pop();
41-
this.setData(true, cur.x, cur.y);
42-
AlgoVisHelper.pause(DELAY);
40+
Position curPos = stack.pop();
41+
setData(curPos.x, curPos.y, true);
4342

44-
if(cur.x == data.getExitX() && cur.y == data.getExitY()){
45-
isSolved = true;
43+
if(curPos.x == data.getExitX() && curPos.y == data.getExitY())
4644
break;
47-
}
4845

49-
for(int i = 0 ; i < 4 ; i ++){
50-
int newX = cur.x + d[i][0];
51-
int newY = cur.y + d[i][1];
52-
if(data.inArea(newX, newY) && !data.visited[newX][newY] && data.maze[newX][newY] == MazeData.ROAD){
46+
for(int i = 0 ; i < 4 ; i ++){
47+
int newX = curPos.x + d[i][0];
48+
int newY = curPos.y + d[i][1];
49+
50+
if(data.inArea(newX, newY)
51+
&& !data.visited[newX][newY]
52+
&& data.getMaze(newX, newY) == MazeData.ROAD){
5353
stack.push(new Position(newX, newY));
5454
data.visited[newX][newY] = true;
5555
}
5656
}
57+
5758
}
5859

59-
return isSolved;
60+
setData(-1, -1, false);
6061
}
6162

62-
63-
private void setData(boolean isPath, int x, int y){
63+
private void setData(int x, int y, boolean isPath){
6464
if(data.inArea(x, y))
6565
data.path[x][y] = isPath;
66-
frame.setData(data);
66+
67+
frame.render(data);
68+
AlgoVisHelper.pause(DELAY);
6769
}
6870

6971
public static void main(String[] args) {
7072

71-
String filename = "maze_101_101.txt";
72-
MazeData data = new MazeData(filename);
73-
//data.print();
73+
String mazeFile = "maze_101_101.txt";
7474

75-
int blockSide = 8;
76-
int sceneHeight = data.N() * blockSide;
77-
int sceneWidth = data.M() * blockSide;
78-
79-
EventQueue.invokeLater(() -> {
80-
AlgoFrame frame = new AlgoFrame("Maze Solver Visualization", sceneWidth,sceneHeight);
81-
82-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
83-
new Thread(() -> {
84-
vis.run();
85-
}).start();
86-
});
75+
AlgoVisualizer vis = new AlgoVisualizer(mazeFile);
8776
}
8877
}

05-Maze-Solver/06-Non-Recursive-DFS-Maze-Solver/src/MazeData.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ 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;
19+
public boolean[][] visited;
2020

2121
public MazeData(String filename){
2222

@@ -32,34 +32,29 @@ public MazeData(String filename){
3232
FileInputStream fis = new FileInputStream(file);
3333
scanner = new Scanner(new BufferedInputStream(fis), "UTF-8");
3434

35+
// 读取第一行
3536
String nmline = scanner.nextLine();
37+
String[] nm = nmline.trim().split("\\s+");
38+
//System.out.print(nm[0] + ' ' + nm[1]);
3639

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

45-
maze = new char[N][M];
45+
// 读取后续的N行
4646
visited = new boolean[N][M];
4747
path = new boolean[N][M];
48+
maze = new char[N][M];
4849
for(int i = 0 ; i < N ; i ++){
4950
String line = scanner.nextLine();
51+
52+
// 每行保证有M个字符
5053
if(line.length() != M)
5154
throw new IllegalArgumentException("Maze file " + filename + " is invalid");
52-
for(int j = 0 ; j < M ; j ++){
55+
for(int j = 0 ; j < M ; j ++)
5356
maze[i][j] = line.charAt(j);
54-
visited[i][j] = false;
55-
path[i][j] = false;
56-
}
5757
}
58-
59-
entranceX = 1;
60-
entranceY = 0;
61-
exitX = N - 2;
62-
exitY = M - 1;
6358
}
6459
catch(IOException e){
6560
e.printStackTrace();
@@ -68,14 +63,24 @@ public MazeData(String filename){
6863
if(scanner != null)
6964
scanner.close();
7065
}
66+
67+
entranceX = 1;
68+
entranceY = 0;
69+
exitX = N - 2;
70+
exitY = M - 1;
7171
}
7272

7373
public int N(){ return N; }
7474
public int M(){ return M; }
75-
public int getEntranceX(){ return entranceX; }
76-
public int getEntranceY(){ return entranceY; }
77-
public int getExitX(){ return exitX; }
78-
public int getExitY(){ return exitY; }
75+
public int getEntranceX(){return entranceX;}
76+
public int getEntranceY(){return entranceY;}
77+
public int getExitX(){return exitX;}
78+
public int getExitY(){return exitY;}
79+
public char getMaze(int i, int j){
80+
if(!inArea(i,j))
81+
throw new IllegalArgumentException("i or j is out of index in getMaze!");
82+
return maze[i][j];
83+
}
7984

8085
public boolean inArea(int x, int y){
8186
return x >= 0 && x < N && y >= 0 && y < M;

05-Maze-Solver/07-Non-Recursive-DFS-Maze-Solver/src/AlgoFrame.java renamed to 05-Maze-Solver/07-More-about-Non-Recursive-DFS-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+
private MazeData 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.inStack[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/07-Non-Recursive-DFS-Maze-Solver/src/AlgoVisHelper.java renamed to 05-Maze-Solver/07-More-about-Non-Recursive-DFS-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-
static public void strokeCircle(Graphics2D g, int x, int y, int r){
35+
public static 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-
static public void fillCircle(Graphics2D g, int x, int y, int r){
41+
public static 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-
static public void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
47+
public static 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-
static public void fillRectangle(Graphics2D g, int x, int y, int w, int h){
53+
public static 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-
static public void setColor(Graphics2D g, Color color){
59+
public static void setColor(Graphics2D g, Color color){
6060
g.setColor(color);
6161
}
6262

63-
static public void setStrokeWidth(Graphics2D g, int w){
63+
public static void setStrokeWidth(Graphics2D g, int w){
6464
int strokeWidth = w;
6565
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6666
}

0 commit comments

Comments
 (0)