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

211 changes: 209 additions & 2 deletions src/ExplorerSearchTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;

public class ExplorerSearchTest {
@Test
Expand All @@ -15,6 +16,212 @@ public void testReachableArea_someUnreachable() {
assertEquals(14, actual);
}

// Add more tests here!
// Come up with varied cases
@Test
public void testReachableArea_allReachable() {
int[][] island = {
{2,3,2,3,2,3},
{3,2,3,1,3,2},
{1,1,1,1,3,3},
{3,1,2,1,0,1},
{1,1,1,2,1,1},
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(14, actual);
}

@Test
public void testReachableArea_allPossibleReachable() {
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_noneReachable() {
int[][] island = {
{1,1,1,3,1,1},
{3,2,3,1,3,1},
{1,1,1,1,3,3},
{3,1,2,2,0,2},
{1,1,1,2,3,1},
};
int actual = ExplorerSearch.reachableArea(island);
assertEquals(1, actual);
}

// Test explorerLocation
@Test
public void testExplorerLocation_validLocation() {
int[][] island = {
{1,1,1,3,1,1},
{3,2,3,1,3,1},
{1,1,1,1,3,3},
{3,1,2,1,0,1},
{1,1,1,2,1,1},
};
int[] actual = ExplorerSearch.explorerLocation(island);
assertEquals(3, actual[0]);
assertEquals(4, actual[1]);
}

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

@Test
public void testExplorerLocation_endingLocation() {
int[][] island = {
{1,1,1,3,1,1},
{3,2,3,1,3,1},
{1,1,1,1,3,3},
{3,1,2,1,1,1},
{1,1,1,2,1,0},
};
int[] actual = ExplorerSearch.explorerLocation(island);
assertEquals(4, actual[0]);
assertEquals(5, actual[1]);
}

@Test
public void testExplorerLocation_invalidLocation() {
int[][] island = {
{1,1,1,3,1,1},
{3,2,3,1,3,1},
{1,1,1,1,3,3},
{3,1,2,1,1,1},
{1,1,1,2,1,1},
};

IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class, () -> { ExplorerSearch.explorerLocation(island); }
);

assertEquals("No explorer present", exception.getMessage());
}

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

assertEquals(4, moves.size());
assertTrue(moveSet.contains("0,1")); // up
assertTrue(moveSet.contains("2,1")); // down
assertTrue(moveSet.contains("1,0")); // left
assertTrue(moveSet.contains("1,2")); // right
}

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

assertEquals(3, moves.size());
assertTrue(moveSet.contains("0,1")); // up
assertTrue(moveSet.contains("1,0")); // left
assertTrue(moveSet.contains("1,2")); // right
}

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

assertEquals(3, moves.size());
assertTrue(moveSet.contains("2,1")); // down
assertTrue(moveSet.contains("1,0")); // left
assertTrue(moveSet.contains("1,2")); // right
}

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

assertEquals(3, moves.size());
assertTrue(moveSet.contains("0,1")); // up
assertTrue(moveSet.contains("2,1")); // down
assertTrue(moveSet.contains("1,0")); // left
}

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

assertEquals(3, moves.size());
assertTrue(moveSet.contains("0,1")); // up
assertTrue(moveSet.contains("2,1")); // down
assertTrue(moveSet.contains("1,2")); // right
}

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

assertEquals(0, moves.size());
assertTrue(moveSet.isEmpty());
}

// toSet helper method
private Set<String> toSet(List<int[]> list) {
Set<String> set = new HashSet<>();
for (int[] arr : list) {
set.add(arr[0] + "," + arr[1]);
}
return set;
}
}