From e4c31b2250be69fe43a300c1037fc00e348ef31c Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Tue, 21 Apr 2026 14:50:20 -0700 Subject: [PATCH 1/2] livecode --- src/Location.java | 3 ++ src/Search.java | 74 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/Location.java diff --git a/src/Location.java b/src/Location.java new file mode 100644 index 0000000..e860e86 --- /dev/null +++ b/src/Location.java @@ -0,0 +1,3 @@ +public record Location(int row, int column) { + +} diff --git a/src/Search.java b/src/Search.java index cebb278..77af6c9 100644 --- a/src/Search.java +++ b/src/Search.java @@ -1,3 +1,10 @@ +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 { /** * Finds the location of the nearest reachable cheese from the rat's position. @@ -28,7 +35,70 @@ 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.column()] == 'c') return current; + + visited.add(current); + + for(Location neighbor: neighbors(maze, current)) { + queue.add(neighbor); + } + } + + throw new HungryRatException(); + } + + public static List neighbors(char[][] maze, Location current) { + List result = new ArrayList<>(); + + int[][] moves = new int[][] { + {-1, 0}, // up + {1, 0}, // down + {0, -1}, // left + {0, 1} // right + }; + + for(int[] move : moves) { + int newR = current.row() + move[0]; + int newC = current.column() + move[1]; + + if(newR >= 0 && newR < maze.length && + newC >= 0 && newC < maze[0].length && + maze[newR][newC] != 'w') { + result.add(new Location(newR, newC)); + } + } + + return result; + } + public static Location ratLocation(char[][] maze) throws EscapedRatException, CrowdedMazeException { + Location location = null; + for(int r = 0; r < maze.length; r++) { + for(int c = 0; c < maze[0].length; c++) { + if(maze[r][c] == 'R') { + if(location == null) { + location = new Location(r, c); + } else { + throw new CrowdedMazeException(); + } + } + } + } + if(location == null) { + throw new EscapedRatException(); + } + + return location; } } \ No newline at end of file From 805acd4f4396ad02dd0bd3cb7f0116659c131c25 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Tue, 28 Apr 2026 14:50:48 -0700 Subject: [PATCH 2/2] livecode day 2 --- src/Search.java | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Search.java b/src/Search.java index 77af6c9..5ee1176 100644 --- a/src/Search.java +++ b/src/Search.java @@ -1,11 +1,25 @@ 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) { + 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)); + } /** * 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 @@ -43,16 +57,32 @@ public static Location nearestCheese(char[][] maze) throws EscapedRatException, Set visited = new HashSet<>(); + Map prevs = new HashMap<>(); + while(!queue.isEmpty()) { Location current = queue.poll(); - if(visited.contains(current)) continue; - if(maze[current.row()][current.column()] == 'c') return current; + if(maze[current.row()][current.column()] == 'c') { + List path = new ArrayList(); - visited.add(current); + Location pointer = current; + while(!pointer.equals(start)) { + path.add(pointer); + pointer = prevs.get(pointer); + } + + path.add(start); + System.out.println(path.reversed()); + + return current; + } for(Location neighbor: neighbors(maze, current)) { - queue.add(neighbor); + if(!visited.contains(neighbor)) { + queue.add(neighbor); + visited.add(neighbor); + prevs.put(neighbor, current); + } } }