forked from Ohohcakester/Any-Angle-Pathfinding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstSearch.java
More file actions
126 lines (102 loc) · 3.54 KB
/
Copy pathBreadthFirstSearch.java
File metadata and controls
126 lines (102 loc) · 3.54 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
125
126
package algorithms;
import grid.GridGraph;
import java.util.LinkedList;
import java.util.Queue;
public class BreadthFirstSearch extends AStar {
private Queue<Integer> queue;
public BreadthFirstSearch(GridGraph graph, int sx, int sy, int ex, int ey) {
super(graph, sx, sy, ex, ey);
postSmoothingOn = false;
}
public static BreadthFirstSearch postSmooth(GridGraph graph, int sx, int sy, int ex, int ey) {
BreadthFirstSearch bfs = new BreadthFirstSearch(graph, sx, sy, ex, ey);
bfs.postSmoothingOn = true;
return bfs;
}
@Override
public void computePath() {
int totalSize = (graph.sizeX+1) * (graph.sizeY+1);
int start = toOneDimIndex(sx, sy);
finish = toOneDimIndex(ex, ey);
visited = new boolean[totalSize];
parent = new int[totalSize];
for (int i=0; i<parent.length; i++) {
parent[i] = -1;
}
queue = new LinkedList<>();
queue.offer(start);
visited[start] = true;
while (!queue.isEmpty()) {
int current = queue.poll();
int currX = toTwoDimX(current);
int currY = toTwoDimY(current);
if (canGoDown(currX, currY)) {
int index = toOneDimIndex(currX, currY-1);
if (!visited[index]) {
if (addToQueue(current, index))
break;
}
}
if (canGoUp(currX, currY)) {
int index = toOneDimIndex(currX, currY+1);
if (!visited[index]) {
if (addToQueue(current, index))
break;
}
}
if (canGoLeft(currX, currY)) {
int index = toOneDimIndex(currX-1, currY);
if (!visited[index]) {
if (addToQueue(current, index))
break;
}
}
if (canGoRight(currX, currY)) {
int index = toOneDimIndex(currX+1, currY);
if (!visited[index]) {
if (addToQueue(current, index))
break;
}
}
maybeSaveSearchSnapshot();
}
maybePostSmooth();
}
/**
* Returns true iff finish is found.
*/
private boolean addToQueue(int current, int index) {
parent[index] = current;
queue.offer(index);
visited[index] = true;
if (index == finish) {
return true;
} else {
return false;
}
}
private boolean canGoUp(int x, int y) {
return !bottomRightOfBlockedTile(x,y) || !bottomLeftOfBlockedTile(x,y);
}
private boolean canGoDown(int x, int y) {
return !topRightOfBlockedTile(x,y) || !topLeftOfBlockedTile(x,y);
}
private boolean canGoLeft(int x, int y) {
return !topRightOfBlockedTile(x,y) || !bottomRightOfBlockedTile(x,y);
}
private boolean canGoRight(int x, int y) {
return !topLeftOfBlockedTile(x,y) || !bottomLeftOfBlockedTile(x,y);
}
private boolean topRightOfBlockedTile(int x, int y) {
return graph.isBlocked(x-1, y-1);
}
private boolean topLeftOfBlockedTile(int x, int y) {
return graph.isBlocked(x, y-1);
}
private boolean bottomRightOfBlockedTile(int x, int y) {
return graph.isBlocked(x-1, y);
}
private boolean bottomLeftOfBlockedTile(int x, int y) {
return graph.isBlocked(x, y);
}
}