-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFindingOnSquaredGrid.java
More file actions
273 lines (222 loc) · 8.59 KB
/
Copy pathPathFindingOnSquaredGrid.java
File metadata and controls
273 lines (222 loc) · 8.59 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Created by Sonal Baba on 4/1/2017.
*/
import java.awt.*;
import java.util.*;
public class PathFindingOnSquaredGrid {
Node start;
Node end;
Node[][] gridNode;
// Horizontal and VerticalDistance
double hVDistance = 1.0;
// Diagonal Distance
// Manhattan values.
public static double Manhattan() {
double diagonalDistance = 2;
return diagonalDistance;
}
// Euclidean values.
public static double Euclidean() {
double diagonalDistance = 1.4;
return diagonalDistance;
}
// Chebyshev values.
public static double Chebyshev() {
double dDistance = 1;
return dDistance;
}
public ArrayList<Node> distance(boolean[][] matrix, int startx, int starty, int endx, int endy,double diagonalDistance,String name,boolean isManhat) {
int size = matrix.length;
start = new Node(startx, starty);
end = new Node(endx, endy);
// The grid that is used to store nodes
gridNode = new Node[size][size];
// Creating nodes and finding blocked cells in matrix and mapping accordingly to our grid
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
gridNode[i][j] = new Node(i, j);
if (matrix[i][j] == false) {
gridNode[i][j].blocked = true;
}
}
}
// setting start distance to 0.
// All other nodes will have infinity distance at the beginning
start.distance =0;
// a comparator object to deal with Priority Queue
Comparator<Node> adjacencyComparator = (left, right) -> {
if (left.distance > (right.distance)) {
return 1;
}
return -1;
};
//A Priority Queue to store visiting nodes
Queue<Node> queue = new PriorityQueue(size, adjacencyComparator);
queue.add(start);
while (queue.size() > 0) {
Node current = queue.remove();
//next selected node, it is to save adjacency cell temporary
Node nextNode;
//Breaks the loop when then end node becomes the current node
if (current.x==end.x && current.y==end.y){
break;
}
if(!isManhat){
//checking Top Left Node
if (current.x - 1 >= 0 && current.y - 1 >= 0) {
nextNode = gridNode[current.x - 1][current.y - 1];
double newDistance = current.distance + diagonalDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}}
//Checking Top Node
if (current.x - 1 >= 0) {
nextNode = gridNode[current.x - 1][current.y];
double newDistance = current.distance + hVDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}
if(!isManhat){
//checking Top Right Node
if (current.x - 1 >= 0 && current.y + 1 < size) {
nextNode = gridNode[current.x - 1][current.y + 1];
double newDistance = current.distance + diagonalDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}}
//checking Left Node
if (current.y - 1 >= 0) {
nextNode = gridNode[current.x][current.y - 1];
double newDistance = current.distance + hVDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}
//checking Right Node
if (current.y + 1 < size) {
nextNode = gridNode[current.x][current.y + 1];
double newDistance = current.distance + hVDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}
if(!isManhat){
//checking Bottom Left Node
if (current.x + 1 < size && current.y - 1 >= 0) {
nextNode = gridNode[current.x + 1][current.y - 1];
double newDistance = current.distance + diagonalDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}}
//checking Bottom Node
if (current.x + 1 < size) {
nextNode = gridNode[current.x + 1][current.y];
double newDistance = current.distance + hVDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}
if(!isManhat){
//checking Bottom Right Node
if (current.x + 1 < size && current.y + 1 < size) {
nextNode = gridNode[current.x + 1][current.y + 1];
double newDistance = current.distance + diagonalDistance;
if (!nextNode.blocked && !nextNode.visited && nextNode.distance > newDistance) {
nextNode.distance = newDistance;
nextNode.parent = current;
queue.add(nextNode);
}
}}
current.visited = true;
}
ArrayList<Node> path = new ArrayList<>();
// Checking if a path exists
if (!(gridNode[end.x][end.y].distance == Integer.MAX_VALUE || gridNode[start.x][start.y].distance == Integer.MAX_VALUE)) {
//Trace back the path
Node current = gridNode[end.x][end.y];
System.out.println(name+":"+current.distance);
while (current.parent != null) {
//add visited,lowest parent nodes to the path variable
path.add(current.parent);
current = current.parent;
}
} else System.out.println("NO POSSIBLE PATH");
return path;
}
//inner node class
class Node {
int x;
int y;
double distance = Integer.MAX_VALUE;
Node parent = null;
boolean visited;
boolean blocked;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
// the first show method is to show the randomly generated bloked cells
public static void show(boolean[][] a, boolean which) {
int N = a.length;
StdDraw.setXscale(-1, N);
StdDraw.setYscale(-1, N);
StdDraw.setPenColor(StdDraw.BLACK);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (a[i][j] == which)
StdDraw.square(j, N - i - 1, .5);
else
StdDraw.filledSquare(j, N - i - 1, .5);
}
// draw the N-by-N boolean matrix to standard draw, including the points A
// (x1, y1) and B (x2,y2) to be marked by a circle
public static void show(boolean[][] a, boolean which, int x1, int y1, int x2, int y2, ArrayList<Node> path) {
int N = a.length;
int s = path.size();
int count = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (a[i][j] == which)
if ((i == x1 && j == y1) || (i == x2 && j == y2)) {
StdDraw.setPenColor(Color.RED);
StdDraw.filledCircle(j, N - i - 1, .5);
}
// show the shortest path
for (PathFindingOnSquaredGrid.Node node : path) {
if (s - count == 1) {
return;
}
count++;
StdDraw.setPenColor(Color.BLUE);
StdDraw.filledCircle(node.y, N - node.x - 1, .5);
}
}
// return a random N-by-N boolean matrix, where each entry is
// true with probability p(
public static boolean[][] random(int N, double p) {
boolean[][] a = new boolean[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
a[i][j] = StdRandom.bernoulli(p);
return a;
}
}