-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.java
256 lines (213 loc) · 7.97 KB
/
maze.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
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
/* Maze Solving algorithm.
*
* Code Log
*
* 17th May 2013.
* Status : Incomplete
* BFS in Incomplete
* Tree Implementation is not working as required
* DFS is Complete.
*
* 18th May 2013. 9:00AM
* Status : Complete
* BFS : complete
* Tree construction is complete
* DFS : Complete
* Print function not working
*
* 12:20Am *
* Print function Started Working.
* Hence Complete;
*
* 2:30 PM Revised the code for any general Matrix
*
* Build By : Anirudh Chhangani.
*/
import java.util.HashSet; // to remmember the nodes that are in the tree.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import javax.swing.JOptionPane;
class node{
node links[] = new node[4];
int val=0;
boolean value;
String loc;
int visit=0;
}
class maze{
static int maze[][]= { // 0 : Free Nodes ; 1 : Wall
{0,0,1,1,1,0,0,0},
{0,1,1,1,1,0,1,0},
{0,0,0,1,0,0,0,0},
{1,1,0,0,0,1,1,1},
{1,0,0,1,0,1,1,1},
{1,1,0,0,0,0,1,1},
{0,0,0,1,1,0,0,0},
{0,1,1,1,1,0,1,0},
{0,0,0,1,0,0,0,0},
{1,1,0,0,0,1,1,1},
};
public static void main(String [] args){
String dec = JOptionPane.showInputDialog("Enter the End location"); // Destination Address
char err = dec.charAt(0);
char err2 = dec.charAt(1);
int y1 = (int)(err-'0');
int u =(int)(err2-'0');
if(y1>=0&&u>=0&&maze[y1][u]!=0){
System.out.println("No path Exist");
}
// select start point x and y
String strt = JOptionPane.showInputDialog("Enter the Start location");
//String strt = "54";
char err3 = strt.charAt(0);
char err4 = strt.charAt(1);
int x = (int)(err3-'0');
int y = (int)(err4-'0');
HashSet set = new HashSet();
//HashSet set2 = new HashSet();
Queue<node> que = new LinkedList<node>();
Queue<node> que2 = new LinkedList<node>();
que2 = build_tree(maze,x,y,set,que,que2);
node tree = (node) que2.peek();
//System.out.println(tree.loc);
//System.out.println(nod[0][0].loc);
Stack s = new Stack();
DFS(tree,dec,s);
}
public static Queue build_tree(int [][] maze, int i, int j,HashSet set,Queue que,Queue que2){
String temp_loc = Integer.toString(i);
temp_loc +=(Integer.toString(j));
//System.out.println(temp_loc);
if(set.contains(temp_loc)){
//do nothing;
}
else if(que.isEmpty()==true){
set.add(temp_loc);
node x =new node();
x.loc = temp_loc;
x.value=true;
que.add(x);
check(maze,x,i,j,set,que,que2);
}
//System.out.println(que2.size());
return que2;
}
public static void check(int [][] maze , node x,int i,int j,HashSet set,Queue que,Queue que2){
//System.out.println(x.loc);
//System.out.println(maze.length);
// check adjacent locations and add adjacent nodes to the link
if(i+1<maze.length&&maze[i+1][j]==0){
node y = new node();
y.loc = Integer.toString(i+1);
y.loc+= Integer.toString(j);
//System.out.println(y.loc);
if(set.contains(y.loc)){
//do nothing
}
else{
que.add(y);
//x.value=true;
x.links[x.val] = y;
x.val++;
y.value = true;
set.add(y.loc);
}
}
if(j+1<maze[0].length&&maze[i][j+1]==0){
node q = new node();
q.loc = Integer.toString(i);
q.loc+= Integer.toString(j+1);
//System.out.println(q.loc);
//System.out.println(temp);
if(set.contains(q.loc)){
//do nothing
}
else{
que.add(q);
x.links[x.val] = q;
x.val++;
q.value = true;
set.add(q.loc);
//System.out.println(q.loc);
}
}
if(i-1>=0&&maze[i-1][j]==0){
node p = new node();
p.loc = Integer.toString(i-1);
p.loc+= Integer.toString(j);
if(set.contains(p.loc)){/*do nothing*/}
else{
que.add(p);
//x.value=true;
x.links[x.val] = p;
x.val++;
p.value = true;
set.add(p.loc);
}
}
if(j-1>=0&&maze[i][j-1]==0){
node z = new node();
z.loc = Integer.toString(i);
z.loc+= Integer.toString(j-1);
if(set.contains(z.loc)){/*do nothing*/}
else{
que.add(z);
//x.value=true;
x.links[x.val] = z;
z.value=true;
x.val++;
set.add(z.loc);
}
}
//System.out.println(x.loc);
que2.add(que.remove());
//System.out.println(que.peek());
node t = (node) que.peek();
if(t==null)return;
char temp = t.loc.charAt(0);
int s = (int) (temp-'0');
//System.out.println(s);
char temp2 = t.loc.charAt(1);
int d = (int)(temp2-'0');
//System.out.println(d);
node g = (node) que.peek();
//System.out.println(g.loc);
if(que.isEmpty()!=true){
check(maze,g,s,d,set,que,que2);
}
//return que2;
}
public static void DFS(node x,String dec,Stack s){
//System.out.println(x.val);
if(x.loc.equals(dec)) {
System.out.println("found");
System.out.println("Shortest Distance is : "+ s.size());
String nodeloc ="";
while(s.size()!=0){
node temp = (node) s.pop();
//System.out.print(temp.loc + "<-");
nodeloc = temp.loc + " -> " + nodeloc;
}
System.out.print(nodeloc.substring(0, nodeloc.length()-3));
System.out.println("-> " + dec);
//System.out.println(nodeloc.length());
System.exit(0);
return;
}
//System.out.println(x.loc);
for(int i =0 ; i<x.val;i++)
{
//System.out.println(x.loc);
s.add(x);
node a = (node) s.peek();
if(x.links[i]!=null) {
// System.out.println("hi");
DFS(x.links[i],dec,s);
}
if(x.loc==a.loc){
s.remove(a);
}
}
}
}