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
72 changes: 71 additions & 1 deletion src/ExplorerSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,76 @@ 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[] explorer = getExplorer(island);
boolean[][] visited = new boolean[island.length][island[0].length];
return reachableAreaHelper(island, explorer, visited);
}

public static int reachableAreaHelper(int[][] island, int[] explorer, boolean[][] visited) {
int curR = explorer[0];
int curC = explorer[1];

if (visited[curR][curC]) return 0;

visited[curR][curC] = true;

int tiles = 1;

List<int[]> moves = possibleMoves(island, explorer);
for (int[] move : moves) {
tiles += reachableAreaHelper(island, move, visited);
}

return tiles;
}

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;
}

public static int[] getExplorer(int[][] island) {
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;
}
}
}

throw new IllegalArgumentException("Explorer not found");
}
}
191 changes: 191 additions & 0 deletions src/ExplorerSearchTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;

public class ExplorerSearchTest {
Expand All @@ -17,4 +22,190 @@ public void testReachableArea_someUnreachable() {

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

@Test
public void testReachableArea_allReachable() {
int[][] island = {
{0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1}
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(12, actual);
}

@Test
public void testReachableArea_noneReachable() {
int[][] island = {
{2, 1, 2, 1, 1, 1},
{1, 2, 0, 3, 1, 1},
{2, 3, 3, 2, 3, 1}
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(1, actual);
}

@Test
public void testGetExplorer_smallIsland() {
int[][] island = {
{1, 1, 0},
{2, 2, 3}
};
int[] location = ExplorerSearch.getExplorer(island);
assertEquals(0, location[0]);
assertEquals(2, location[1]);
}

@Test
public void testGetExplorer_smallIslandExplorerAtStart() {
int[][] island = {
{0, 1, 1},
{3, 2, 3}
};
int[] location = ExplorerSearch.getExplorer(island);
assertEquals(0, location[0]);
assertEquals(0, location[1]);
}

@Test
public void testGetExplorer_largeIsland() {
int[][] island = {
{1, 1, 2, 3, 1},
{2, 2, 3, 1, 1},
{3, 2, 1, 1, 1},
{2, 3, 2, 1, 1},
{2, 3, 1, 0, 1}
};
int[] location = ExplorerSearch.getExplorer(island);
assertEquals(4, location[0]);
assertEquals(3, location[1]);
}

@Test
public void testGetExplorer_largeIslandExplorerAtEnd() {
int[][] island = {
{2, 1, 2, 3, 1},
{2, 2, 3, 3, 1},
{3, 2, 2, 1, 2},
{2, 3, 2, 1, 1},
{2, 3, 1, 3, 0}
};
int[] location = ExplorerSearch.getExplorer(island);
assertEquals(4, location[0]);
assertEquals(4, location[1]);
}

@Test
public void testGetExplorer_noExplorer() {
int[][] island = {
{1, 1, 3},
{2, 2, 3},
{3, 1, 2}
};
assertThrows(IllegalArgumentException.class, () -> {
ExplorerSearch.getExplorer(island);
});
}

@Test
public void testPossibleMoves_noBlocks() {
int[][] island = {
{1, 1, 1},
{1, 0, 1},
{1, 1, 1}
};
int[] start = {1, 1};
List<int[]> moves = ExplorerSearch.possibleMoves(island, start);
Set<String> convertedList = toSet(moves);

assertEquals(4, convertedList.size());
assertTrue(convertedList.contains("0, 1"));
assertTrue(convertedList.contains("2, 1"));
assertTrue(convertedList.contains("1, 0"));
assertTrue(convertedList.contains("1, 2"));
}

@Test
public void testPossibleMoves_upBlock() {
int[][] island = {
{1, 2, 1},
{1, 0, 1},
{1, 1, 1}
};
int[] start = {1, 1};
List<int[]> moves = ExplorerSearch.possibleMoves(island, start);
Set<String> convertedList = toSet(moves);

assertEquals(3, convertedList.size());
assertFalse(convertedList.contains("0, 1"));
assertTrue(convertedList.contains("2, 1"));
assertTrue(convertedList.contains("1, 0"));
assertTrue(convertedList.contains("1, 2"));
}

@Test
public void testPossibleMoves_upAndDownBlocks() {
int[][] island = {
{1, 2, 1, 2, 3},
{1, 1, 2, 1, 1},
{1, 1, 0, 1, 1},
{1, 1, 3, 1, 1},
{1, 1, 1, 1, 1}
};
int[] start = {2, 2};
List<int[]> moves = ExplorerSearch.possibleMoves(island, start);
Set<String> convertedList = toSet(moves);

assertEquals(2, convertedList.size());
assertFalse(convertedList.contains("1, 2"));
assertFalse(convertedList.contains("3, 2"));
assertTrue(convertedList.contains("2, 1"));
assertTrue(convertedList.contains("2, 3"));
}

@Test
public void testPossibleMoves_upAndDownAndLeftBlocks() {
int[][] island = {
{1, 2, 1, 2, 3},
{1, 1, 2, 1, 1},
{1, 2, 0, 1, 1},
{1, 1, 3, 1, 1},
{1, 1, 1, 1, 1}
};
int[] start = {2, 2};
List<int[]> moves = ExplorerSearch.possibleMoves(island, start);
Set<String> convertedList = toSet(moves);

assertEquals(1, convertedList.size());
assertFalse(convertedList.contains("1, 2"));
assertFalse(convertedList.contains("3, 2"));
assertFalse(convertedList.contains("2, 1"));
assertTrue(convertedList.contains("2, 3"));
}

@Test
public void testPossibleMoves_AllBlocked() {
int[][] island = {
{1, 2, 2, 2, 3},
{1, 2, 0, 3, 1},
{1, 2, 3, 1, 1}
};
int[] start = {1, 2};
List<int[]> moves = ExplorerSearch.possibleMoves(island, start);
Set<String> convertedList = toSet(moves);

assertEquals(0, convertedList.size());
assertFalse(convertedList.contains("0, 2"));
assertFalse(convertedList.contains("2, 2"));
assertFalse(convertedList.contains("1, 1"));
assertFalse(convertedList.contains("1, 3"));
}

// Convert List to Set for better testing on what the list contains
private Set<String> toSet(List<int[]> list) {
Set<String> converted = new HashSet<>();
for (int[] arr : list) {
converted.add(arr[0] + ", " + arr[1]);
}
return converted;
}
}