From 20f7c810c18f6cc7f51bdcc52c73cf540f92354f Mon Sep 17 00:00:00 2001 From: Ahmed Ihsan Date: Tue, 21 Apr 2026 14:50:22 -0700 Subject: [PATCH 1/2] Finished 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..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..ece5976 100644 --- a/src/Search.java +++ b/src/Search.java @@ -1,5 +1,15 @@ +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 myLoc = new Location(5, 88); + System.out.println(myLoc); + } + /** * 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 @@ -29,6 +39,66 @@ public class Search { * @throws HungryRatException if there is no reachable cheese */ public static int[] nearestCheese(char[][] maze) throws EscapedRatException, CrowdedMazeException, HungryRatException { - return null; + Location startLoc = findRat(maze); + Queue queue = new LinkedList<>(); + queue.add(startLoc); + Set visited = new HashSet<>(); + + while(!queue.isEmpty()) { + Location current = queue.poll(); + if (visited.contains(current)) continue; + if (maze[current.row()][current.col()] == 'c') { + return new int[] {current.row(), current.row()}; + }; + visited.add(current); + + for (Location neighbor : neighbors(maze, current)) { + queue.add(neighbor); + }; + } + + throw new HungryRatException(); + } + + public static List neighbors(char[][] maze, Location currentLoc) { + 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 = currentLoc.row() + move[0]; + int newC = currentLoc.col() + move[1]; + char loc = maze[newR][newC]; + if ( + (newR >= 0 && newR < maze.length) && + (newC >= 0 && newC < maze[0].length) && + (loc != 'w')) { + result.add(new Location(newR, newC)); + } + } + + return result; + } + + public static Location findRat(char[][] maze) throws EscapedRatException, CrowdedMazeException { + Location ratLocation = null; + for (int r = 0; r < maze.length; r++) { + for (int c = 0; c < maze[r][c]; c++) { + if (maze[r][c] == 'R') { + if (ratLocation == null) { + ratLocation = new Location(r, c); + } else { + throw new CrowdedMazeException(); + }; + }; + } + } + if (ratLocation == null) throw new EscapedRatException(); + return ratLocation; } } \ No newline at end of file From 954bb45d181df27091be6093c6f3cd4c0fdcf2ec Mon Sep 17 00:00:00 2001 From: Ahmed Ihsan Date: Tue, 28 Apr 2026 20:03:46 -0700 Subject: [PATCH 2/2] Finished livecode --- src/Search.java | 80 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/src/Search.java b/src/Search.java index ece5976..0561ba1 100644 --- a/src/Search.java +++ b/src/Search.java @@ -1,13 +1,23 @@ import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Set; +import java.util.Map; public class Search { public static void main(String[] args) { - Location myLoc = new Location(5, 88); - System.out.println(myLoc); + // Location myLoc = new Location(5, 88); + + 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. @@ -44,16 +54,29 @@ public static int[] nearestCheese(char[][] maze) throws EscapedRatException, Cro queue.add(startLoc); 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.col()] == 'c') { + List path = new ArrayList<>(); + Location pointer = current; + while(pointer.equals(startLoc)) { + path.add(pointer); + pointer = prevs.get(pointer); + } + + path.add(startLoc); + + System.out.println(path.reversed()); return new int[] {current.row(), current.row()}; }; - visited.add(current); + for (Location neighbor : neighbors(maze, current)) { + if (visited.contains(neighbor)) continue; + visited.add(neighbor); queue.add(neighbor); + prevs.put(neighbor, current); }; } @@ -62,43 +85,44 @@ public static int[] nearestCheese(char[][] maze) throws EscapedRatException, Cro public static List neighbors(char[][] maze, Location currentLoc) { List result = new ArrayList<>(); + + int[] [] moves = new int[][] { + { -1, 0 }, //up + { 1, 0 }, //down + { 0, -1 }, //left + { 0, 1 }, //right - int[][] moves = new int[][] { - {-1, 0}, // up - {1, 0}, // down - {0, -1}, // left - {0, 1} // right }; - for (int[] move : moves) { + for(int[] move : moves) { int newR = currentLoc.row() + move[0]; int newC = currentLoc.col() + move[1]; - char loc = maze[newR][newC]; - if ( - (newR >= 0 && newR < maze.length) && - (newC >= 0 && newC < maze[0].length) && - (loc != 'w')) { - result.add(new Location(newR, newC)); + + 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 findRat(char[][] maze) throws EscapedRatException, CrowdedMazeException { - Location ratLocation = null; - for (int r = 0; r < maze.length; r++) { - for (int c = 0; c < maze[r][c]; c++) { - if (maze[r][c] == 'R') { - if (ratLocation == null) { - ratLocation = new Location(r, c); - } else { + Location location = null; + for(int row = 0; row < maze.length; row++){ + for(int col=0; col < maze[row].length; col++){ + if(maze[row][col] == 'R'){ + if(location == null){ + location = new Location(row, col); + } else{ throw new CrowdedMazeException(); - }; - }; + } + } } } - if (ratLocation == null) throw new EscapedRatException(); - return ratLocation; + + if(location == null){ + throw new EscapedRatException(); + } + + return location; } } \ No newline at end of file