forked from mr-sergi/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AStar.java
203 lines (168 loc) · 6.48 KB
/
AStar.java
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
package Graphs;
import java.util.*;
public class Astar {
public static final int DIAGONAL_COST = 14;
public static final int V_H_COST = 10;
static class Cell{
int heuristicCost = 0; //Heuristic cost
int finalCost = 0; //G+H
int i, j;
Cell parent;
Cell(int i, int j){
this.i = i;
this.j = j;
}
@Override
public String toString(){
return "["+this.i+", "+this.j+"]";
}
}
//Blocked cells are just null Cell values in grid
static Cell [][] grid = new Cell[5][5];
static PriorityQueue<Cell> open;
static boolean closed[][];
static int startI, startJ;
static int endI, endJ;
public static void setBlocked(int i, int j){
grid[i][j] = null;
}
public static void setStartCell(int i, int j){
startI = i;
startJ = j;
}
public static void setEndCell(int i, int j){
endI = i;
endJ = j;
}
static void checkAndUpdateCost(Cell current, Cell t, int cost){
if(t == null || closed[t.i][t.j])return;
int t_final_cost = t.heuristicCost+cost;
boolean inOpen = open.contains(t);
if(!inOpen || t_final_cost<t.finalCost){
t.finalCost = t_final_cost;
t.parent = current;
if(!inOpen)open.add(t);
}
}
public static void AStar(){
//add the start location to open list.
open.add(grid[startI][startJ]);
Cell current;
while(true){
current = open.poll();
if(current==null)break;
closed[current.i][current.j]=true;
if(current.equals(grid[endI][endJ])){
return;
}
Cell t;
if(current.i-1>=0){
t = grid[current.i-1][current.j];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
if(current.j-1>=0){
t = grid[current.i-1][current.j-1];
checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);
}
if(current.j+1<grid[0].length){
t = grid[current.i-1][current.j+1];
checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);
}
}
if(current.j-1>=0){
t = grid[current.i][current.j-1];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
}
if(current.j+1<grid[0].length){
t = grid[current.i][current.j+1];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
}
if(current.i+1<grid.length){
t = grid[current.i+1][current.j];
checkAndUpdateCost(current, t, current.finalCost+V_H_COST);
if(current.j-1>=0){
t = grid[current.i+1][current.j-1];
checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);
}
if(current.j+1<grid[0].length){
t = grid[current.i+1][current.j+1];
checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);
}
}
}
}
/*
Params :
tCase = test case No.
x, y = Board's dimensions
si, sj = start location's x and y coordinates
ei, ej = end location's x and y coordinates
int[][] blocked = array containing inaccessible cell coordinates
*/
public static void test(int tCase, int x, int y, int si, int sj, int ei, int ej, int[][] blocked){
System.out.println("\n\nTest Case #"+tCase);
//Reset
grid = new Cell[x][y];
closed = new boolean[x][y];
open = new PriorityQueue<>((Object o1, Object o2) -> {
Cell c1 = (Cell)o1;
Cell c2 = (Cell)o2;
return c1.finalCost<c2.finalCost?-1:c1.finalCost>c2.finalCost?1:0;
});
//Set start position
setStartCell(si, sj); //Setting to 0,0 by default. Will be useful for the UI part
//Set End Location
setEndCell(ei, ej);
for(int i=0;i<x;++i){
for(int j=0;j<y;++j){
grid[i][j] = new Cell(i, j);
grid[i][j].heuristicCost = Math.abs(i-endI)+Math.abs(j-endJ);
// System.out.print(grid[i][j].heuristicCost+" ");
}
// System.out.println();
}
grid[si][sj].finalCost = 0;
/*
Set blocked cells. Simply set the cell values to null
for blocked cells.
*/
for(int i=0;i<blocked.length;++i){
setBlocked(blocked[i][0], blocked[i][1]);
}
//Display initial map
System.out.println("Grid: ");
for(int i=0;i<x;++i){
for(int j=0;j<y;++j){
if(i==si&&j==sj)System.out.print("SO "); //Source
else if(i==ei && j==ej)System.out.print("DE "); //Destination
else if(grid[i][j]!=null)System.out.printf("%-3d ", 0);
else System.out.print("BL ");
}
System.out.println();
}
System.out.println();
AStar();
System.out.println("\nScores for cells: ");
for(int i=0;i<x;++i){
for(int j=0;j<x;++j){
if(grid[i][j]!=null)System.out.printf("%-3d ", grid[i][j].finalCost);
else System.out.print("BL ");
}
System.out.println();
}
System.out.println();
if(closed[endI][endJ]){
//Trace back the path
System.out.println("Path: ");
Cell current = grid[endI][endJ];
System.out.print(current);
while(current.parent!=null){
System.out.print(" -> "+current.parent);
current = current.parent;
}
System.out.println();
}else System.out.println("No possible path");
}
public static void main(String[] args) throws Exception{
test(1, 5, 5, 0, 0, 3, 2, new int[][]{{0,4},{2,2},{3,1},{3,3}});
}
}