forked from auberonedu/explorer
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathExplorerSearch.java
More file actions
122 lines (100 loc) · 4.1 KB
/
ExplorerSearch.java
File metadata and controls
122 lines (100 loc) · 4.1 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
import java.util.ArrayList;
import java.util.List;
public class ExplorerSearch {
/**
* Returns how much land area an explorer can reach on a rectangular island.
*
* The island is represented by a rectangular int[][] that contains
* ONLY the following nunbers:
*
* '0': represents the starting location of the explorer
* '1': represents a field the explorer can walk through
* '2': represents a body of water the explorer cannot cross
* '3': represents a mountain the explorer cannot cross
*
* The explorer can move one square at a time: up, down, left, or right.
* They CANNOT move diagonally.
* They CANNOT move off the edge of the island.
* They CANNOT move onto a a body of water or mountain.
*
* This method should return the total number of spaces the explorer is able
* to reach from their starting location. It should include the starting
* location of the explorer.
*
* For example
*
* @param island the locations on the island
* @return the number of spaces the explorer can reach
*/
public static int reachableArea(int[][] island) {
// Implement your method here!
// Please also make more test cases
// I STRONGLY RECOMMEND testing some helpers you might make too
int[] start = explorerLocation(island);
boolean[][] visited = new boolean[island.length][island[0].length];
return reachableArea(island, start, visited);
}
public static int reachableArea(int[][] island, int[] current, boolean[][] visited) {
int curR = current[0];
int curC = current[1];
// if we've been here before, return 0
if (visited[curR][curC]) return 0;
// if we haven't, count the current space and set visited to true
int spaces = 1;
visited[curR][curC] = true;
// find the list of where we can get to from here
List<int[]> neighbors = possibleMoves(island, current);
// go through the list
for (int[] neighbor : neighbors) {
//add the results to our number of valid spaces
spaces += reachableArea(island, neighbor, visited);
}
// send the list back to top
// after we run out of valid spaces, this should return the final result to the top level
return spaces;
}
public static int[] explorerLocation(int[][] island) {
//go through the area and find the explorer
for (int r=0; r < island.length; r++) {
for (int c = 0; c < island[r].length; c++) {
if (island[r][c] == 0) {
int[] location = new int[] {r, c};
return location;
}
}
}
// you didn't give explicit instructions for a missing explorer so I am doing it the same as on the live code assignment
// if this was an undesirable result, a missing explorer could also set the start location out of bounds {-1, -1} with a check that returns 0 total moves for that starting location (if there's no explorer on the island, 0 "squares" are explorable)
throw new IllegalArgumentException("No explorer on the island.");
}
public static List<int[]> possibleMoves(int[][] island, int[] current) {
List<int[]> moves = new ArrayList<>();
int curR = current[0];
int curC = current[1];
// up
int newR = curR - 1;
int newC = curC;
if (newR >= 0 && island[newR][newC] == 1) {
moves.add(new int[]{newR, newC});
}
// down
newR = curR + 1;
newC = curC;
if (newR < island.length && island[newR][newC] == 1) {
moves.add(new int[]{newR, newC});
}
// left
newR = curR;
newC = curC - 1;
if (newC >= 0 && island[newR][newC] == 1) {
moves.add(new int[] {newR, newC});
}
// right
newR = curR;
newC = curC + 1;
if (newC < island[newR].length && island[newR][newC] == 1) {
moves.add(new int[] {newR, newC});
}
return moves;
}
}