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
102 changes: 101 additions & 1 deletion src/ExplorerSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,106 @@ 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 = explorableLocations(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)
{
//current = {1,1}

// vistied =
// 0 1 2
//0 false, false, false
//1 false, false, false -> value would grab the middle of this row
//2 false, false, false

//island =
// 1, 1, 1
// 1, 0, 1
// 1, 1, 1

int curR = current[0];
int curC = current[1];

if(curR < 0 || curR >= island.length || curC < 0 || curC >= island[0].length) return 0;
if(visited[curR][curC] || (island[curR][curC] != 1 && island[curR][curC] != 0)) return 0;

visited[curR][curC] = true;
int count = 1;


count += reachableArea(island, new int[]{curR - 1, curC}, visited); // up
count += reachableArea(island, new int[]{curR + 1, curC}, visited); // down
count += reachableArea(island, new int[]{curR, curC - 1}, visited); // left
count += reachableArea(island, new int[]{curR, curC + 1}, visited); // right

return count;
}


public static List<int[]> explorableMoves(int[][] island, int[] current)
{
//Create List
List<int[]> moves = new ArrayList<>();

//Init rows and columns
int curR = current[0];
int curC = current[1];

//Up
int newR = curR - 1;
int newC = curC;
if(newR >= 0 && island[newR][newC] != 2 && island[newR][newC] != 3)
{
moves.add(new int[]{newR, newC});
}

//Down
newR = curR + 1;
newC = curC;
if(newR < island.length && island[newR][newC] != 2 && island[newR][newC] != 3)
{
moves.add(new int[]{newR, newC});
}

//Left
newR = curR;
newC = curC - 1;
if(newC >= 0 && island[newR][newC] != 2 && island[newR][newC] != 3)
{
moves.add(new int[]{newR, newC});
}

//Right
newR = curR;
newC = curC + 1;
if(newC < island[newR].length && island[newR][newC] != 2 && island[newR][newC] != 3)
{
moves.add(new int[]{newR, newC});
}


return moves;
}

public static int[] explorableLocations(int[][] island)
{
for(int r = 0; r < island.length; r++)
{
for(int c = 0; c < island[0].length; c++)
{
if(island[r][c] == 0)
{
int[] location = new int[]{r, c};
return location;
}
}
}
throw new IllegalArgumentException("No Explorer found");
}
}
227 changes: 227 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 @@ -15,6 +20,228 @@ public void testReachableArea_someUnreachable() {
assertEquals(14, actual);
}

/*
* Reachable Area
* Tests: 3
* Author: Tim Williams
*/

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

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

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

/*
* Explore Moves
* Tests: 4
* Author: Tim Williams
*/

@Test
public void testExplorableMoves_AllDirections()
{
int[][] island = {
{1,1,1},
{1,0,1},
{1,1,1}
};

int[] location = {1,1};
List<int[]> moves = ExplorerSearch.explorableMoves(island, location);
Set<String> moveSet = toSet(moves);

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

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

int[] location = {1,1};
List<int[]> moves = ExplorerSearch.explorableMoves(island, location);
Set<String> moveSet = toSet(moves);

assertEquals(2, moves.size());
assertTrue(moveSet.contains("2,1"));
assertTrue(moveSet.contains("1,2"));
}

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

int[] location = {0,0};
List<int[]> moves = ExplorerSearch.explorableMoves(island, location);
Set<String> moveSet = toSet(moves);

assertEquals(2, moves.size());
assertTrue(moveSet.contains("0,1"));
assertTrue(moveSet.contains("1,0"));
}

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

int[] location = {0,0};
List<int[]> moves = ExplorerSearch.explorableMoves(island, location);
assertTrue(moves.isEmpty());
}


/*
* Explore location
* Tests: 5
* Author: Tim Williams
*/

@Test
public void testExploreLocation_MiddleOfGrid()
{
int[][] island = {
{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,1,1,1},
{1,1,1,1,1,1,1},
};
int[] actual = ExplorerSearch.explorableLocations(island);
int[] expected = {2,3};
assertArrayEquals(expected, actual);
}

@Test
public void testExploreLocation_TopMiddleOfGrid()
{
int[][] island = {
{1,1,1,0,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
};
int[] actual = ExplorerSearch.explorableLocations(island);
int[] expected = {0,3};
assertArrayEquals(expected, actual);
}

@Test
public void testExploreLocation_BottomRightOfGrid()
{
int[][] island = {
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,0},
};
int[] actual = ExplorerSearch.explorableLocations(island);
int[] expected = {4,6};
assertArrayEquals(expected, actual);
}

@Test
public void testExploreLocation_TopRightOfGrid()
{
int[][] island = {
{1,1,1,1,1,1,0},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
};
int[] actual = ExplorerSearch.explorableLocations(island);
int[] expected = {0,6};
assertArrayEquals(expected, actual);
}


@Test
public void testExploreLocation_NoValidLocation()
{
int[][] island = {
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
};
assertThrows(IllegalArgumentException.class, () -> {
ExplorerSearch.explorableLocations(island);
});
}



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



//Method I noticed in the testing part of salamander on our introduction to this type of DSA.
/*
* Method returns a set, utilizes a (List) parameter.
* intiliazes set,
* loops through values of given values in list
* added to the set as a (String) in the format (Example,Example) (1,1)
* returns set for us to compare with our assetEquals
*/
private Set<String> toSet(List<int[]> list) {
Set<String> set = new HashSet<>();
for (int[] arr : list) {
set.add(arr[0] + "," + arr[1]);
}
return set;
}
}