From 3f5269d93a6c4ca4cfc860fc3cd9faa775761228 Mon Sep 17 00:00:00 2001 From: Connor Date: Tue, 21 Apr 2026 14:50:33 -0700 Subject: [PATCH 1/2] Livecode finished --- src/Location.java | 3 ++ src/Search.java | 95 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/Location.java diff --git a/src/Location.java b/src/Location.java new file mode 100644 index 0000000..77ef752 --- /dev/null +++ b/src/Location.java @@ -0,0 +1,3 @@ +public record Location(int row, int col) { + +} diff --git a/src/Search.java b/src/Search.java index cebb278..27d8277 100644 --- a/src/Search.java +++ b/src/Search.java @@ -1,5 +1,19 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Set; + public class Search { - /** + public static void main(String[] args) { + Location myLocations = new Location(5,88); + + System.out.println(myLocations); + } + + + /** * Finds the location of the nearest reachable cheese from the rat's position. * Returns a 2-element int array: [row, col] of the closest 'c'. If there are multiple * cheeses that are tied for the same shortest distance to reach, return @@ -28,7 +42,82 @@ public class Search { * @throws CrowdedMazeException if there is more than one rat in the maze * @throws HungryRatException if there is no reachable cheese */ - public static int[] nearestCheese(char[][] maze) throws EscapedRatException, CrowdedMazeException, HungryRatException { - return null; + public static Location nearestCheese(char[][] maze) throws EscapedRatException, CrowdedMazeException, HungryRatException { + Location start = ratLocation(maze); + + Queue queue = new LinkedList<>(); + + queue.add(start); + + Set visited = new HashSet<>(); + + + + while(!queue.isEmpty()){ + Location current = queue.poll(); + if(visited.contains(current)) continue; + + if(maze[current.row()][current.col()] == 'c'){ + return current; + } + visited.add(current); + + for(Location neighbor : neighbors(maze, current)){ + queue.add(neighbor); + } + + } + + throw new HungryRatException(); + + } + + private static List neighbors(char[][] maze, Location current){ + List result = new ArrayList<>(); + + int[][] moves = new int[][]{ + {-1,0}, //up + {1,0}, //down + {0,-1}, //left + {1,0} //right + }; + + for (int[] move : moves) { + int newRow = current.row() + move[0]; + int newCol = current.col() + move[1]; + + if(newRow >= 0 && newRow < maze.length + && newCol >= 0 && newCol < maze[0].length + && maze[newRow][newCol] != 'w'){ + result.add(new Location(newRow, newCol)); + } + } + + + + return result; + } + + private static Location ratLocation(char[][] maze)throws EscapedRatException, CrowdedMazeException{ + Location cordinates = null; + + for(int row = 0; row < maze.length;row++){ + for(int col = 0; col < maze[row].length;col++){ + if(maze[row][col] == 'R'){ + if(cordinates == null){ + cordinates = new Location(row, col); + } else { + throw new CrowdedMazeException(); + } + + } + } + } + + if(cordinates == null){ + throw new EscapedRatException(); + } + + return cordinates; } } \ No newline at end of file From 5a43b7817b67e4a1bc88d82212bb8925c70c4ded Mon Sep 17 00:00:00 2001 From: Connor Date: Tue, 28 Apr 2026 14:51:40 -0700 Subject: [PATCH 2/2] BFS livecode day 2 --- src/Search.java | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Search.java b/src/Search.java index 27d8277..9ee6f0e 100644 --- a/src/Search.java +++ b/src/Search.java @@ -1,15 +1,27 @@ import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Queue; import java.util.Set; public class Search { public static void main(String[] args) { - Location myLocations = new Location(5,88); + // Location myLocations = new Location(5,88); - System.out.println(myLocations); + + // System.out.println(myLocations); + char[][] grid = { + {'o', 'o', 'o', 'o', 'c', 'w', 'c', 'o'}, + {'w', 'o', 'o', 'w', 'w', 'c', 'w', 'o'}, + {'o', 'o', 'o', 'o', 'R', 'w', 'o', 'o'}, + {'o', 'o', 'w', 'w', 'w', 'o', 'o', 'o'}, + {'o', 'o', 'o', 'o', 'c', 'o', 'o', 'o'} + }; + + System.out.println(nearestCheese(grid)); } @@ -50,24 +62,42 @@ public static Location nearestCheese(char[][] maze) throws EscapedRatException, queue.add(start); Set visited = new HashSet<>(); - + Map prevs = new HashMap<>(); + while(!queue.isEmpty()){ Location current = queue.poll(); - if(visited.contains(current)) continue; + visited.add(current); if(maze[current.row()][current.col()] == 'c'){ + List path = new ArrayList<>(); + + Location pointer = current; + while(!pointer.equals(start)){ + path.add(pointer); + pointer = prevs.get(pointer); + } + + path.add(start); + + System.out.println(path.reversed()); + return current; } - visited.add(current); for(Location neighbor : neighbors(maze, current)){ - queue.add(neighbor); + if(!visited.contains(neighbor)){ + visited.add(neighbor); + queue.add(neighbor); + prevs.put(neighbor, current); + } + } } - + // System.out.println(start); + throw new HungryRatException(); } @@ -79,7 +109,7 @@ private static List neighbors(char[][] maze, Location current){ {-1,0}, //up {1,0}, //down {0,-1}, //left - {1,0} //right + {0,1} //right }; for (int[] move : moves) {