Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions src/ExplorerSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,66 @@ public class ExplorerSearch {
* @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
return -1;
int[] start = findStart(island);
boolean[][] visited = new boolean[island.length][island[0].length];
return reachableArea(island,start,visited);
}

public static int reachableArea(int[][] island, int[] start, boolean[][] visited) {
int currROW = start[0];
int currCOL = start[1];
if (visited[currROW][currCOL]) return 0;
if (currROW<0||
currROW>island.length||
currCOL<0||
currCOL>island[currROW].length) {
return 0;
};
int count = 1;
visited[currROW][currCOL] = true;
for (int[] neighbor : moves(island, start)) {
count+= reachableArea(island, neighbor, visited);
}
return count;
}

public static List<int[]> moves(int[][] island, int[] curr) {
List<int[]> moves = new ArrayList<>();
int currROW = curr[0];
int currCOL = curr[1];
//up
int newROW = currROW - 1;
int newCOL = currCOL;
if (newROW>=0&&island[newROW][newCOL]!=2&&island[newROW][newCOL]!=3) {
moves.add(new int[]{newROW,newCOL});
}
//down
newROW = currROW + 1;
newCOL = currCOL;
if (newROW<island.length&&island[newROW][newCOL]!=2&&island[newROW][newCOL]!=3) {
moves.add(new int[]{newROW,newCOL});
}
//right
newROW = currROW;
newCOL = currCOL + 1;
if (newCOL<island[newROW].length&&island[newROW][newCOL]!=2&&island[newROW][newCOL]!=3) {
moves.add(new int[]{newROW,newCOL});
}
//left
newROW = currROW;
newCOL = currCOL - 1;
if (newCOL>=0&&island[newROW][newCOL]!=2&&island[newROW][newCOL]!=3) {
moves.add(new int[]{newROW,newCOL});
}
return moves;
}

public static int[] findStart(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};
}
}
return new int[]{};
}
}
58 changes: 58 additions & 0 deletions src/ExplorerSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,64 @@ public void testReachableArea_someUnreachable() {
int actual = ExplorerSearch.reachableArea(island);
assertEquals(14, actual);
}
@Test
public void testReachableArea_allReachable() {
int[][] island = {
{1,1,1,1,1,1},
{1,1,1,1,1,1},
{1,1,1,1,1,1},
{1,1,1,1,0,1},
{1,1,1,1,1,1},
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(30, actual);
}
@Test
public void testReachableArea_onlyStartReachable() {
int[][] island = {
{3,3,3,3,3,3},
{3,3,3,3,3,3},
{3,3,3,3,3,3},
{3,3,3,3,0,3},
{3,3,3,3,3,3},
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(1, actual);
}
@Test
public void testReachableArea_onlyTwoReachable() {
int[][] island = {
{3,3,3,3,3,3},
{3,3,3,3,3,3},
{3,3,3,3,1,3},
{3,3,3,3,0,3},
{3,3,3,3,3,3},
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(2, actual);
}
@Test
public void testReachableArea_terribleArea() {
int[][] island = {
{3,3,3},
{3,0,3},
{3,3,3}
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(1, actual);
}
@Test
public void testReachableArea_thisGuyIsLiterallyTrapped() {
int[][] island = {
{1,1,1,1,1},
{1,3,3,3,1},
{1,3,0,3,1},
{1,3,3,3,1},
{1,1,1,1,1},
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(1, actual);
}

// Add more tests here!
// Come up with varied cases
Expand Down