-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
371 lines (331 loc) · 13.5 KB
/
Map.java
File metadata and controls
371 lines (331 loc) · 13.5 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
*@author Steeve Johan Otoka Eyota
*@version 1.0
*@since 2020-12-01
*/
import java.util.*;
/**
*@author Steeve Johan Otoka Eyota
*@version 1.0
*@since 2020-12-01
*/
public class Map {
boolean[][] map;
private int row;
private int column;
/**
* This is the constructor that constructs the city map, which is a grid of row
* by column.
*
* @param row is the number of east-west streets of the city
* @param column is the number of north-south streets of the city
*/
public Map(int row, int column) {
// Please implement the constructor
this.row = row;
this.column = column;
this.map = new boolean[row][column];
}
/**
* This method checks the correctness of the input parameters. If the
* preconditions are not met an exception is thrown, otherwise depending to the
* direction, it calls one of the four recursive functions of goSouthWest,
* goSouthEast, goNorthWest and goNorthEast.
*
* @param startRow is the starting row of the path
* @param startCol is the starting column of the path
* @param destRow is the destination row
* @param destCol is the destination column
* @param path is the path that is constructed while the recursive method is
* called. In first round, it will be "".
* @return returns a string representing the path to the destination. The format
* of the output is (x1,y1) (x2,y2) (x3,y3)...
* @pre the integer parameters should be in the range of the city grid.(i.e. [0,
* N) if N is the number of east-west streets and [0, M) if M is the number
* of north-south streets.)
* @exception IllegalArgumentException if any of the precondition did not meet.
*/
public String getPath(int startRow, int startCol, int destRow, int destCol, String path) {
// Please complete this method
// you should decide on what should be returned. This return statement is here
// to avoid compiler error.
// Check if the parameters are in the range of the city grid
if ((startRow >= 0) && (startRow < this.row) && (startCol >= 0) && (startCol < this.column) && (destRow >= 0)
&& (destRow < this.row) && (destCol >= 0) && (destCol < this.column)) {
// Check condition for the car to go South West
if ((startRow >= destRow) && (startCol >= destCol)) {
return goSouthWest(startRow, startCol, destRow, destCol, path);
// Check condition for the car to go South East
} else if ((startRow >= destRow) && (startCol <= destCol)) {
return goSouthEast(startRow, startCol, destRow, destCol, path);
// Check condition for the car to go North West
} else if ((startRow <= destRow) && (startCol >= destCol)) {
return goNorthWest(startRow, startCol, destRow, destCol, path);
// Else the car goes North East
} else {
return goNorthEast(startRow, startCol, destRow, destCol, path);
}
}
// Throw an exception if the parameters are not on the range of the city grid
else {
throw new IllegalArgumentException();
}
}
/**
* This method returns a path from the source (startRow, startCol) to the
* destination (destRow, destCol). Please note that the returning path does not
* include the starting point.
*
* @param startRow is the starting row of the path
* @param startCol is the starting column of the path
* @param destRow is the destination row
* @param destCol is the destination column
* @param path is the path that is constructed while the recursive method is
* called. In first round, it will be "".
* @return returns a string representing the path to the destination. The format
* of the output is (x1,y1) (x2,y2) (x3,y3)...
* @pre <code> startRow >= destRow </code> and
* <code> startCol >= destCol </code>
*/
private String goSouthWest(int startRow, int startCol, int destRow, int destCol, String path) {
// Please complete this method
// you should decide on what should be returned. This return statement is here
// to avoid compiler error.
// base case: the starting position is equal to the final position
if ((startRow == destRow) && (startCol == destCol)) {
return "";
}
// recursive step
// decrement startCol if it is greater than destCol
else if ((startRow == destRow) && (startCol > destCol)) {
startCol--;
return "(" + startRow + "," + startCol + ") " + goSouthWest(startRow, startCol, destRow, destCol, path);
}
// decrement startRow if it is greater than destRow
else if ((startRow > destRow) && (startCol == destCol)) {
startRow--;
return "(" + startRow + "," + startCol + ") " + goSouthWest(startRow, startCol, destRow, destCol, path);
}
// decrement startRow if it is greater than destRow and startCol is greater than
// destCol
else {
startRow--;
return "(" + startRow + "," + startCol + ") " + goSouthWest(startRow, startCol, destRow, destCol, path);
}
}
/**
* This method returns a path from the source (startRow, startCol) to the
* destination (destRow, destCol). Please note that the returning path does not
* include the starting point.
*
* @param startRow is the starting row of the path
* @param startCol is the starting column of the path
* @param destRow is the destination row
* @param destCol is the destination column
* @param path is the path that is constructed while the recursive method is
* called. In first round, it will be "".
* @return returns a string representing the path to the destination. The format
* of the output is (x1,y1) (x2,y2) (x3,y3)...
* @pre <code> startRow >= destRow </code> and
* <code> startCol <= destCol </code>
*/
private String goSouthEast(int startRow, int startCol, int destRow, int destCol, String path) {
// Please complete this method
// you should decide on what should be returned. This return statement is here
// to avoid compiler error.
// base case: the starting position is equal to the final position
if ((startRow == destRow) && (startCol == destCol)) {
return "";
}
// recursive step
// increment startCol if it is less than destCol
else if ((startRow == destRow) && (startCol < destCol)) {
startCol++;
return "(" + startRow + "," + startCol + ") " + goSouthEast(startRow, startCol, destRow, destCol, path);
}
// decrement startRow if it is greater than destRow
else if ((startRow > destRow) && (startCol == destCol)) {
startRow--;
return "(" + startRow + "," + startCol + ") " + goSouthEast(startRow, startCol, destRow, destCol, path);
}
// decrement startRow if it is greater than destRow and startCol is less than
// destCol
else {
startRow--;
return "(" + startRow + "," + startCol + ") " + goSouthEast(startRow, startCol, destRow, destCol, path);
}
}
/**
* This method returns a path from the source (startRow, startCol) to the
* destination (destRow, destCol). Please note that the returning path does not
* include the starting point.
*
* @param startRow is the starting row of the path
* @param startCol is the starting column of the path
* @param destRow is the destination row
* @param destCol is the destination column
* @param path is the path that is constructed while the recursive method is
* called. In first round, it will be "".
* @return returns a string representing the path to the destination. The format
* of the output is (x1,y1) (x2,y2) (x3,y3)...
* @pre <code> startRow <= destRow </code> and
* <code> startCol >= destCol </code>
*/
private String goNorthEast(int startRow, int startCol, int destRow, int destCol, String path) {
// Please complete this method
// you should decide on what should be returned. This return statement is here
// to avoid compiler error.
// base case: the starting position is equal to the final position
if ((startRow == destRow) && (startCol == destCol)) {
return "";
}
// recursive step
// increment startCol if it is less than destCol
else if ((startRow == destRow) && (startCol < destCol)) {
startCol++;
return "(" + startRow + "," + startCol + ") " + goNorthEast(startRow, startCol, destRow, destCol, path);
}
// increment startRow if it is less than destRow
else if ((startRow < destRow) && (startCol == destCol)) {
startRow++;
return "(" + startRow + "," + startCol + ") " + goNorthEast(startRow, startCol, destRow, destCol, path);
}
// decrement startRow if it is less than destRow and startCol is less than
// destCol
else {
startRow++;
return "(" + startRow + "," + startCol + ") " + goNorthEast(startRow, startCol, destRow, destCol, path);
}
}
/**
* This method returns a path from the source (startRow, startCol) to the
* destination (destRow, destCol). Please note that the returning path does not
* include the starting point.
*
* @param startRow is the starting row of the path
* @param startCol is the starting column of the path
* @param destRow is the destination row
* @param destCol is the destination column
* @param path is the path that is constructed while the recursive method is
* called. In first round, it will be "".
* @return returns a string representing the path to the destination. The format
* of the output is (x1,y1) (x2,y2) (x3,y3)...
* @pre <code> startRow <= destRow </code> and
* <code> startCol >= destCol </code>
*/
private String goNorthWest(int startRow, int startCol, int destRow, int destCol, String path) {
// Please complete this method
// you should decide on what should be returned. This return statement is here
// to avoid compiler error.
// base case: the starting position is equal to the final position
if ((startRow == destRow) && (startCol == destCol)) {
return "";
}
// recursive step
// decrement startCol if it is greater than destCol
else if ((startRow == destRow) && (startCol > destCol)) {
startCol--;
return "(" + startRow + "," + startCol + ") " + goNorthWest(startRow, startCol, destRow, destCol, path);
}
// increment startRow if it is less than destRow
else if ((startRow < destRow) && (startCol == destCol)) {
startRow++;
return "(" + startRow + "," + startCol + ") " + goNorthWest(startRow, startCol, destRow, destCol, path);
}
// increment startRow if it is less than destRow and startCol is greater than
// destCol
else {
startRow++;
return "(" + startRow + "," + startCol + ") " + goNorthWest(startRow, startCol, destRow, destCol, path);
}
}
/**
* This method find a path from (startRow, startCol) to a border point of the
* city. Please note that the starting point should be included in the path.
*
* @param startRow is the starting row of the path
* @param startCol is the starting column of the path
* @return is a path from (starting row, starting col) to a border point of the
* city. The format of the output is (x1,y1) (x2,y2) (x3,y3)...
*/
public String findPath(int startRow, int startCol) {
// Please complete this method
// you should decide on what should be returned. This return statement is here
// to avoid compiler error.
// the idea is to solve the problem using indirect recursion
// so we will need a helper method
// we may forget our starting position
// let's keep it in variables as a respawn position
int respawnRow = startRow;
int respawnCol = startCol;
// using the map provided
// we set all the elements to true
for (int r = 0; r < row; r++) {
for (int c = 0; c < column; c++) {
map[r][c] = true;
}
}
// we set the starting position to false, since it is considered already visited
map[startRow][startCol] = false;
// pahtHistory is the returning String of the path taken by the car
String pathHistory = "(" + startRow + "," + startCol + ") ";
pathHistory = pathHistory + findPathHelper(startRow, startCol, pathHistory);
if (pathHistory.contains("POLICE")) {
return findPath(respawnRow, respawnCol);
} else {
return pathHistory;
}
}
// Helper method
/**
* This method is the helper method for findPath(). It returns an empty String
* if the car has reached the border of the city, otherwise it return the path
* taken by the car and also indicates if it came across a police car
*
* @param startRow is the starting row of the path
* @param startCol is the starting column of the path
* @param pathHistory is the path taken by the car
*
* @return is a path from (starting row, starting col) to a border point of the
* city. The format of the output is (x1,y1) (x2,y2) (x3,y3)...
*/
private String findPathHelper(int startRow, int startCol, String pathHistory) {
if ((startRow == 0) || (startRow == row - 1) || (startCol == 0) || (startCol == column - 1)) {
return "";
} else {
// destRow is the destination row
// destCol is the destination column
int destRow = 0;
int destCol = 0;
// Generating a random number between 1 to 4
Random rand = new Random();
int direction = rand.nextInt(4) + 1;
// the car randomly choose any of these directions
switch (direction) {
case 1:
destRow = startRow + 1;
destCol = startCol;
break;
case 2:
destRow = startRow - 1;
destCol = startCol;
break;
case 3:
destRow = startRow;
destCol = startCol + 1;
break;
case 4:
destRow = startRow;
destCol = startCol - 1;
break;
}
if (map[destRow][destCol] == false) {
return pathHistory + "POLICE";
} else {
pathHistory = getPath(startRow, startCol, destRow, destCol, "");
map[destRow][destCol] = false;
return pathHistory + findPathHelper(destRow, destCol, pathHistory);
}
}
}
} // end of class