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
3 changes: 3 additions & 0 deletions src/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public record Location(int row, int col) {

}
98 changes: 96 additions & 2 deletions src/Search.java
Original file line number Diff line number Diff line change
@@ -1,5 +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.Queue;
import java.util.Set;
import java.util.Map;
public class Search {
/**
public static void main(String[] args) {
// 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.
* 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
Expand Down Expand Up @@ -29,6 +49,80 @@ 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<Location> queue = new LinkedList<>();
queue.add(startLoc);
Set<Location> visited = new HashSet<>();

Map<Location, Location> prevs = new HashMap<>();
while(!queue.isEmpty()) {
Location current = queue.poll();
if (maze[current.row()][current.col()] == 'c') {
List<Location> 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()};
};


for (Location neighbor : neighbors(maze, current)) {
if (visited.contains(neighbor)) continue;
visited.add(neighbor);
queue.add(neighbor);
prevs.put(neighbor, current);
};
}

throw new HungryRatException();
}

public static List<Location> neighbors(char[][] maze, Location currentLoc) {
List<Location> 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];

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 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(location == null){
throw new EscapedRatException();
}

return location;
}
}