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
80 changes: 79 additions & 1 deletion src/Search.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Search {
// to find nearest or closest use BFS
/**
* 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
Expand Down Expand Up @@ -29,6 +35,78 @@ public class Search {
* @throws HungryRatException if there is no reachable cheese
*/
public static int[] nearestCheese(char[][] maze) throws EscapedRatException, CrowdedMazeException, HungryRatException {
return null;
int[] start = ratLocation(maze);

Queue<int[]> queue = new LinkedList<>();

queue.add(start);

boolean[][] visited = new boolean[maze.length][maze[0].length];

while(!queue.isEmpty()) {
int[] current = queue.poll();

int curR = current[0];
int curC = current[1];

if(maze[curR][curC] == 'c') {
return current;
}

if(visited[curR][curC]) {
continue;
}

visited[curR][curC] = true;

List<int[]> nextMoves = possibleMoves(maze, current);

queue.addAll(nextMoves);
}

throw new HungryRatException();
}

public static List<int[]> possibleMoves(char[][] maze, int[] currentLocation) {
List<int[]> moves = new ArrayList<>();

int[][] steps = {
{-1, 0},
{1, 0},
{0, -1},
{0, 1}
};

int curR = currentLocation[0];
int curC = currentLocation[1];

for(int[] step : steps) {
int newR = curR + step[0];
int newC = curC + step[1];

if(newR >= 0 && newR < maze.length &&
newC >= 0 && newC < maze[0].length &&
maze[newR][newC] != 'w') {
moves.add(new int[]{newR, newC});
}
}
return moves;
}

public static int[] ratLocation(char[][] maze) throws CrowdedMazeException, EscapedRatException {
int ratCount = 0;
int[] ratLocation = null;

for(int r = 0; r < maze.length; r++) {
for(int c = 0; c < maze[0].length; c++) {
if(maze[r][c] == 'R') {
ratCount++;
ratLocation = new int[]{r, c};
}
}
}
if(ratCount > 1) throw new CrowdedMazeException();
if(ratCount == 0) throw new EscapedRatException();
return ratLocation;
}
}
65 changes: 63 additions & 2 deletions src/SearchTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class SearchTest {

}

@Test
public void testNearestCheese() throws Exception {
char[][] maze = {
{'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'}
};

int[] result = Search.nearestCheese(maze);
assertArrayEquals(new int[]{0, 4}, result);
}

@Test
public void testThrowsCrowdedMazeException() {
char[][] maze = {
{'R','o','o','o','c','w','c','o'},
{'w','R','o','w','w','c','w','R'},
{'o','o','o','o','o','w','o','o'},
{'o','o','w','w','w','o','o','o'},
{'R','o','o','o','c','o','R','o'}
};

assertThrows(CrowdedMazeException.class, () -> {
Search.nearestCheese(maze);
});
}

@Test
public void testThrowsEscapedRatException() {
char[][] maze = {
{'o','o','o','o','c','w','c','o'},
{'w','o','o','w','w','c','w','o'},
{'o','o','o','o','o','w','o','o'},
{'w','o','w','w','w','o','o','o'},
{'w','w','o','o','c','o','o','o'}
};

assertThrows(EscapedRatException.class, () -> {
Search.nearestCheese(maze);
});
}

@Test
public void testThrowsHungryRatException() {
char[][] maze = {
{'w','w','w','w','c','w','c','w'},
{'w','w','w','c','w','c','w','w'},
{'w','w','w','w','R','w','w','w'},
{'w','w','w','c','w','c','w','w'},
{'w','w','w','w','c','w','w','w'}
};

assertThrows(HungryRatException.class, () -> {
Search.nearestCheese(maze);
});
}
}