diff --git a/src/SalamanderSearch.java b/src/SalamanderSearch.java index 688cda7..40813bc 100644 --- a/src/SalamanderSearch.java +++ b/src/SalamanderSearch.java @@ -1,5 +1,5 @@ import java.util.ArrayList; -import java.util.List; +import java.util.*; public class SalamanderSearch { public static void main(String[] args) { @@ -18,6 +18,18 @@ public static void main(String[] args) { {'f','W','.','.','W','.'}, {'W','.','W','.','.','.'}, }; + + Set coordinateSet = new HashSet<>(); + int[] coord1 = new int[]{1, 5}; + int[] coord2 = new int[]{3, 7}; + int[] coord3 = new int[]{1, 5}; + + coordinateSet.add(coord1); + coordinateSet.add(coord2); + coordinateSet.add(coord3); + + System.out.println(coordinateSet.size()); + } /** @@ -43,6 +55,79 @@ public static void main(String[] args) { * @return whether the salamander can reach the food */ public static boolean canReach(char[][] enclosure) { + // find salamander + int[] start = salamanderLocation(enclosure); + // this boolean is the same size as the enclosure and starts off all false by default + boolean[][] visited = new boolean[enclosure.length][enclosure[0].length]; + + // recursive dfs + return canReach(enclosure, start, visited); + } + + /* Helper method that passes in current location and boolean record of visited locations. */ + public static boolean canReach(char[][] enclosure, int[] current, boolean[][] visited) { + int currRow = current[0]; + int currCol = current[1]; + + if (visited[currRow][currCol]) return false; + if (enclosure[currRow][currCol] == 'f') return true; + + visited[currRow][currCol] = true; + + List moves = possibleMoves(enclosure, current); + + for (var move: moves) { + if (canReach(enclosure, move, visited)) return true; + } + return false; } + + /* Helper method to find salamander starting position. + Returns an array of integers that represent the row and column of the starting position of the salamander */ + public static int[] salamanderLocation(char[][] enclosure) { + + for (int r = 0; r < enclosure.length; r++) { + for (int c = 0; c < enclosure[r].length; c++) { + if (enclosure[r][c] == 's') { + return new int[]{r, c}; + } + } + } + throw new IllegalArgumentException("No salamander present"); + } + + /* Helper method + * Given a current location, what are the possible valid moves + * Checks up, down, left, right + */ + public static List possibleMoves(char[][] enclosure, int[] currentLocation) { + int currentRow = currentLocation[0]; + int currentCol = currentLocation[1]; + + List moves = new ArrayList<>(); + + int[][] directions = new int[][] { + {-1, 0}, + {1, 0}, + {0, -1}, + {0, 1} + }; + + for (int[] direction: directions) { + int newRow = currentRow + direction[0]; + int newCol = currentCol + direction[1]; + + // Note that the below check can also be moved to an isValid helper function + + if (newRow >= 0 && newRow < enclosure.length && + newCol >= 0 && newCol < enclosure[0].length && + enclosure[newRow][newCol] != 'W') { + moves.add(new int[]{newRow, newCol}); + } + } + + return moves; + + } } diff --git a/src/SalamanderSearchTest.java b/src/SalamanderSearchTest.java index ddad9de..a8b5af2 100644 --- a/src/SalamanderSearchTest.java +++ b/src/SalamanderSearchTest.java @@ -34,203 +34,203 @@ public void canReach_NoPath() { assertFalse(actual); } - // @Test - // public void testSalamanderLocation_centerOfGrid() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] expected = {1, 1}; - // assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); - // } - - // @Test - // public void testSalamanderLocation_topLeftCorner() { - // char[][] enclosure = { - // {'s', '.', '.'}, - // {'.', '.', '.'}, - // {'.', '.', '.'} - // }; - // int[] expected = {0, 0}; - // assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); - // } - - // @Test - // public void testSalamanderLocation_notFound_throwsException() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', '.', '.'}, - // {'.', '.', '.'} - // }; - // Exception exception = assertThrows(IllegalArgumentException.class, () -> { - // SalamanderSearch.salamanderLocation(enclosure); - // }); - // assertEquals("No salamander present", exception.getMessage()); - // } - - - // @Test - // public void testSalamanderLocation_at_1_2() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', '.', 's'}, - // {'.', '.', '.'} - // }; - // int[] expected = {1, 2}; - // assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); - // } - - // @Test - // public void testPossibleMoves_allDirectionsOpen() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set 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_allDirectionsBlockedByWalls() { - // char[][] enclosure = { - // {'W', 'W', 'W'}, - // {'W', 's', 'W'}, - // {'W', 'W', 'W'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // assertTrue(moves.isEmpty()); - // } - - // @Test - // public void testPossibleMoves_partialEdge() { - // char[][] enclosure = { - // {'s', '.', '.'} - // }; - // int[] location = {0, 0}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertEquals(1, moves.size()); - // assertTrue(moveSet.contains("0,1")); // only right - // } - - // @Test - // public void testPossibleMoves_oneOpen_twoWalls_oneEdge() { - // // Salamander at (1, 1) - // // Up is W, down is wall, left is edge (0), right is open - // char[][] enclosure = { - // {'W', 'W', 'W'}, - // {'.', 's', '.'}, - // {'W', 'W', 'W'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertEquals(2, moves.size()); - // assertTrue(moveSet.contains("1,2")); - // assertTrue(moveSet.contains("1,0")); - // } - - // @Test - // public void testPossibleMoves_blockedAboveByWall() { - // char[][] enclosure = { - // {'.', 'W', '.'}, - // {'.', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertFalse(moveSet.contains("0,1")); // up blocked - // assertTrue(moveSet.contains("2,1")); // down open - // assertTrue(moveSet.contains("1,0")); // left open - // assertTrue(moveSet.contains("1,2")); // right open - // } - - // @Test - // public void testPossibleMoves_blockedBelowByWall() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', '.'}, - // {'.', 'W', '.'} - // }; - // int[] location = {1, 1}; - // Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); - - // assertTrue(moveSet.contains("0,1")); // up open - // assertFalse(moveSet.contains("2,1")); // down blocked - // assertTrue(moveSet.contains("1,0")); // left open - // assertTrue(moveSet.contains("1,2")); // right open - // } - - // @Test - // public void testPossibleMoves_blockedLeftByWall() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'W', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); - - // assertTrue(moveSet.contains("0,1")); // up open - // assertTrue(moveSet.contains("2,1")); // down open - // assertFalse(moveSet.contains("1,0")); // left blocked - // assertTrue(moveSet.contains("1,2")); // right open - // } - - // @Test - // public void testPossibleMoves_blockedRightByWall() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', 'W'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); - - // assertTrue(moveSet.contains("0,1")); // up open - // assertTrue(moveSet.contains("2,1")); // down open - // assertTrue(moveSet.contains("1,0")); // left open - // assertFalse(moveSet.contains("1,2")); // right blocked - // } - - // @Test - // public void testPossibleMoves_topLeftCornerWithOneOpen() { - // char[][] enclosure = { - // {'s', 'W'}, - // {'W', '.'} - // }; - // int[] location = {0, 0}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - - // assertTrue(moves.isEmpty()); // surrounded by walls/edges - // } - - // @Test - // public void testPossibleMoves_rightEdgeWithOpenLeft() { - // char[][] enclosure = { - // {'.', '.', 's'} - // }; - // int[] location = {0, 2}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertEquals(1, moves.size()); - // assertTrue(moveSet.contains("0,1")); - // } + @Test + public void testSalamanderLocation_centerOfGrid() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', '.'}, + {'.', '.', '.'} + }; + int[] expected = {1, 1}; + assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); + } + + @Test + public void testSalamanderLocation_topLeftCorner() { + char[][] enclosure = { + {'s', '.', '.'}, + {'.', '.', '.'}, + {'.', '.', '.'} + }; + int[] expected = {0, 0}; + assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); + } + + @Test + public void testSalamanderLocation_notFound_throwsException() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', '.', '.'}, + {'.', '.', '.'} + }; + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + SalamanderSearch.salamanderLocation(enclosure); + }); + assertEquals("No salamander present", exception.getMessage()); + } + + + @Test + public void testSalamanderLocation_at_1_2() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', '.', 's'}, + {'.', '.', '.'} + }; + int[] expected = {1, 2}; + assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); + } + + @Test + public void testPossibleMoves_allDirectionsOpen() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', '.'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set 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_allDirectionsBlockedByWalls() { + char[][] enclosure = { + {'W', 'W', 'W'}, + {'W', 's', 'W'}, + {'W', 'W', 'W'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + assertTrue(moves.isEmpty()); + } + + @Test + public void testPossibleMoves_partialEdge() { + char[][] enclosure = { + {'s', '.', '.'} + }; + int[] location = {0, 0}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertEquals(1, moves.size()); + assertTrue(moveSet.contains("0,1")); // only right + } + + @Test + public void testPossibleMoves_oneOpen_twoWalls_oneEdge() { + // Salamander at (1, 1) + // Up is W, down is wall, left is edge (0), right is open + char[][] enclosure = { + {'W', 'W', 'W'}, + {'.', 's', '.'}, + {'W', 'W', 'W'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertEquals(2, moves.size()); + assertTrue(moveSet.contains("1,2")); + assertTrue(moveSet.contains("1,0")); + } + + @Test + public void testPossibleMoves_blockedAboveByWall() { + char[][] enclosure = { + {'.', 'W', '.'}, + {'.', 's', '.'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertFalse(moveSet.contains("0,1")); // up blocked + assertTrue(moveSet.contains("2,1")); // down open + assertTrue(moveSet.contains("1,0")); // left open + assertTrue(moveSet.contains("1,2")); // right open + } + + @Test + public void testPossibleMoves_blockedBelowByWall() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', '.'}, + {'.', 'W', '.'} + }; + int[] location = {1, 1}; + Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); + + assertTrue(moveSet.contains("0,1")); // up open + assertFalse(moveSet.contains("2,1")); // down blocked + assertTrue(moveSet.contains("1,0")); // left open + assertTrue(moveSet.contains("1,2")); // right open + } + + @Test + public void testPossibleMoves_blockedLeftByWall() { + char[][] enclosure = { + {'.', '.', '.'}, + {'W', 's', '.'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); + + assertTrue(moveSet.contains("0,1")); // up open + assertTrue(moveSet.contains("2,1")); // down open + assertFalse(moveSet.contains("1,0")); // left blocked + assertTrue(moveSet.contains("1,2")); // right open + } + + @Test + public void testPossibleMoves_blockedRightByWall() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', 'W'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); + + assertTrue(moveSet.contains("0,1")); // up open + assertTrue(moveSet.contains("2,1")); // down open + assertTrue(moveSet.contains("1,0")); // left open + assertFalse(moveSet.contains("1,2")); // right blocked + } + + @Test + public void testPossibleMoves_topLeftCornerWithOneOpen() { + char[][] enclosure = { + {'s', 'W'}, + {'W', '.'} + }; + int[] location = {0, 0}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + + assertTrue(moves.isEmpty()); // surrounded by walls/edges + } + + @Test + public void testPossibleMoves_rightEdgeWithOpenLeft() { + char[][] enclosure = { + {'.', '.', 's'} + }; + int[] location = {0, 2}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertEquals(1, moves.size()); + assertTrue(moveSet.contains("0,1")); + } private Set toSet(List list) {