-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder.java
More file actions
274 lines (226 loc) · 5.81 KB
/
PathFinder.java
File metadata and controls
274 lines (226 loc) · 5.81 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
274
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.ArrayDeque;
import java.util.Stack;
/*
* Recursive class to represent a position in a path
*/
class Position{
public int i; //column
public int j; //row
public char val; //1, 0, or 'X'
// reference to the previous position (parent) that leads to this position on a path
Position parent;
Position(int x, int y, char v){
i=x; j = y; val=v;
}
Position(int x, int y, char v, Position p){
i=x; j = y; val=v;
parent=p;
}
}
public class PathFinder {
public static void main(String[] args) throws IOException {
if(args.length<1){
System.err.println("***Usage: java PathFinder maze_file");
System.exit(-1);
}
char [][] maze;
maze = readMaze(args[0]);
printMaze(maze);
Position [] path = stackSearch(maze);
System.out.println("stackSearch Solution:");
printPath(path);
printMaze(maze);
char [][] maze2 = readMaze(args[0]);
path = queueSearch(maze2);
System.out.println("queueSearch Solution:");
printPath(path);
printMaze(maze2);
}
//add and pop for stack (FILO)
public static Position [] stackSearch(char [] [] maze){
ArrayDeque<Position> steps = new ArrayDeque<Position>();
Position current = new Position(0,0,'X',null);
Position[] p;
steps.push(current);
int x;
int y;
while(!steps.isEmpty()){
current=steps.pop();
x=current.i;
y=current.j;
if(x==maze.length-1&& y == maze[0].length - 1){
break;
}
else{
int px;
int py;
if(current.parent!=null){
px=current.parent.i;
py=current.parent.j;}
else {
px=0;
py=0;
}
//down
if( (y < maze.length - 1) &&(y+1!=py) && (maze[y+1][x]== '0')){
steps.add(new Position(x, y+1,'X', current));
}
//up
if((y > 0) &&(y-1!=py)&&(maze[y-1][x] == '0')){
steps.add(new Position(x, y-1 ,'X',current));
}
//right
if( (x < maze[0].length - 1) &&(x+1!=px)&& (maze[y][x+1] == '0')){
steps.add(new Position(x+1, y, 'X',current));
}
//left
if( (x > 0) &&(x-1!=px) &&(maze[y][x-1] == '0' )){
steps.add(new Position(x-1, y, 'X',current));
}
if(steps.isEmpty())
return null;
}
}
Stack<Position> temp = new Stack<Position>();
while (current.parent!= null){
temp.push(current);
maze[current.j][current.i] = 'X';
current = current.parent;
}
maze[0][0]='X';
p= new Position[temp.size()];
for(int o=0;o<p.length;o++)
p[o]=temp.pop();
return p;
}
//add and remove for queue: FOFO
public static Position [] queueSearch(char [] [] maze){
Queue<Position> steps = new ArrayDeque<Position>();
Position current = new Position(0,0,'X',null);
Position[] p;
steps.add(current);
int x;
int y;
while(!steps.isEmpty()){
current=steps.remove();
x=current.i;
y=current.j;
if(x==maze[0].length-1&& y == maze.length - 1){
break;
}
else{
int px;
int py;
if(current.parent!=null){
px=current.parent.i;
py=current.parent.j;}
else {
px=0;
py=0;
}
//down
if( (y < maze.length - 1) &&(y+1!=py) && (maze[y+1][x]== '0')){
steps.add(new Position(x, y+1,'X', current));
}
//up
if((y > 0) &&(y-1!=py)&&(maze[y-1][x] == '0')){
steps.add(new Position(x, y-1 ,'X',current));
}
//right
if( (x < maze[0].length - 1) &&(x+1!=px)&& (maze[y][x+1] == '0')){
steps.add(new Position(x+1, y, 'X',current));
}
//left
if( (x > 0) &&(x-1!=px) &&(maze[y][x-1] == '0' )){
steps.add(new Position(x-1, y, 'X',current));
}
if(steps.isEmpty())
return null;
}
}
Queue<Position> temp = new LinkedList<Position>();
while (current.parent!= null){
temp.add(current);
maze[current.j][current.i] = 'X';
current = current.parent;
}
maze[0][0]='X';
p= new Position[temp.size()];
for(int o=0;o<p.length;o++){
p[p.length-1-o]=temp.remove();
}
return p;
}
public static void printPath(Position [] path){
// todo: print the path to the stdout
if(path==null){
System.out.println("No solution found!");
}
else{
System.out.print("(");
for(int k=0; k<path.length;k++){
System.out.print("["+path[k].i+"]["+path[k].j+"],");
}
System.out.print(")");
}
System.out.println();
}
/**
* Reads maze file in format:
* N -- size of maze
* 0 1 0 1 0 1 -- space-separated
* @param filename
* @return
* @throws IOException
*/
public static char [][] readMaze(String filename) throws IOException{
char [][] maze;
Scanner scanner;
try{
scanner = new Scanner(new FileInputStream(filename));
}
catch(IOException ex){
System.err.println("*** Invalid filename: " + filename);
return null;
}
int N = scanner.nextInt();
scanner.nextLine();
maze = new char[N][N];
int i=0;
while(i < N && scanner.hasNext()){
String line = scanner.nextLine();
String [] tokens = line.split("\\s+");
int j = 0;
for (; j< tokens.length; j++){
maze[i][j] = tokens[j].charAt(0);
}
if(j!=N){
System.err.println("*** Invalid line: " + i + " has wrong # columns: " + j);
return null;
}
i++;
}
if(i!=N){
System.err.println("*** Invalid file: has wrong number of rows: " + i);
return null;
}
return maze;
}
public static void printMaze(char[][] maze){
if(maze==null || maze[0] == null){
System.err.println("*** Invalid maze array");
return;
}
for(int i=0; i< maze.length; i++){
for(int j = 0; j< maze[0].length; j++){
System.out.print(maze[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
}