-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
124 lines (113 loc) · 4.04 KB
/
Solver.java
File metadata and controls
124 lines (113 loc) · 4.04 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.ArrayList;
import java.util.Random;
import java.util.Stack;
class Solver {
void shuffleDirs() {
for(int count = 0; count < 2; count++) {
int i = rnd.nextInt(4), j = rnd.nextInt(4);
int[] temp = dirs[i];
dirs[i] = dirs[j];
dirs[j] = temp;
}
}
public Solver (MazePanel _mp) {
mp = _mp; a = mp.a;
n = mp.n; m = mp.m;
rnd = new Random();
rnd.setSeed(rnd.nextLong());
}
public void solveStart(String s) {
if (s.equals("Depth First Search"))
DFS();
else if (s.equals("Breadth First Search"))
BFS();
repaintDelay();
}
private void DFS() {
Stack<Coordinate> s = new Stack<>();
s.push(new Coordinate(1, 1));
while(!s.empty()) {
Coordinate p = s.peek();
a[p.i][p.j] = MAZE.GENERATED;
boolean f = true;
shuffleDirs();
for(int[] d : dirs) {
int ni = p.i + d[0], nj = p.j + d[1];
if (a[ni][nj] == MAZE.PASSAGE) {
if (a[ni + d[0]][nj + d[1]] == MAZE.GENERATED) {
a[ni][nj] = MAZE.GENERATED;
}
else {
s.push(new Coordinate(ni + d[0], nj + d[1]));
f = false;
}
}
}
if (f) s.pop();
Coordinate prev = s.elementAt(0);
for(Coordinate pp : s) {
if (a[pp.i][pp.j] == MAZE.GENERATED) {
a[pp.i][pp.j] = a[(pp.i + prev.i) / 2][(pp.j + prev.j) / 2] = MAZE.WAYOUT;
prev = pp;
}
}
repaintDelay();
if (p.compareTo(new Coordinate(n - 2, m - 2)) == 0) break;
prev = s.elementAt(0);
for(Coordinate pp : s) {
if (a[pp.i][pp.j] == MAZE.WAYOUT) {
a[pp.i][pp.j] = a[(pp.i + prev.i) / 2][(pp.j + prev.j) / 2] = MAZE.GENERATED;
prev = pp;
}
}
}
}
private void BFS() {
// array of path
ArrayList<ArrayList<Coordinate>> q = new ArrayList<>();
ArrayList<Coordinate> path = new ArrayList<>();
path.add(new Coordinate(1, 1));
q.add(path);
while(!q.isEmpty()) {
// extract path from queue of paths
path = q.remove(0);
Coordinate p = path.get(path.size() - 1);
// and add all paths that adjacent with it
shuffleDirs();
for (int[] d : dirs) {
int ni = p.i + d[0], nj = p.j + d[1];
if (a[ni][nj] == MAZE.PASSAGE) {
a[ni + d[0]][nj + d[1]] = a[ni][nj] = MAZE.GENERATED;
ArrayList<Coordinate> newPath = new ArrayList<>(path);
newPath.add(new Coordinate(ni + d[0], nj + d[1]));
q.add(newPath);
}
}
// we need to color current path
Coordinate prev = path.get(0);
for (Coordinate pp : path) {
a[pp.i][pp.j] = a[prev.i][prev.j] =
a[(pp.i + prev.i) / 2][(pp.j + prev.j) / 2] =
MAZE.WAYOUT;
prev = pp;
}
repaintDelay();
if (p.i == n - 2 && p.j == m - 2) break;
prev = path.get(0);
for (Coordinate pp : path) {
a[pp.i][pp.j] = a[prev.i][prev.j] =
a[(pp.i + prev.i) / 2][(pp.j + prev.j) / 2] =
MAZE.GENERATED;
prev = pp;
}
}
}
private void repaintDelay() {
mp.repaintDelay();
}
private final Random rnd;
private final MazePanel mp;
private final int n, m;
private final int[][] a;
private int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
}