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
94 lines (77 loc) · 2.96 KB
/
ExplorerSearch.java
File metadata and controls
94 lines (77 loc) · 2.96 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
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) {
int[] start = explorerLocation(island);
boolean[][] visited = new boolean[island.length][island[0].length];
return reachableArea(island, start, visited);
}
//overloaded reachableArea
public static int reachableArea(int[][] island, int[] current, boolean[][] visited) {
int curR = current[0];
int curC = current[1];
if(visited[curR][curC]) return 0;
visited[curR][curC] = true;
List<int[]> moves = possibleMoves(island, current);
int total = 1;
for (int[] move : moves) total += reachableArea(island, move, visited);
return total;
}
//location
public static int[] explorerLocation(int[][] island) {
for (int r = 0; r < island.length; r++) {
for (int c = 0; c < island[r].length; c++) {
if (island[r][c] == 0) return new int[] {r, c};
}
}
throw new IllegalArgumentException("No explorer present");
}
//possibleMoves
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;
}
}