-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortestPathToGetFood.java
84 lines (74 loc) · 3.22 KB
/
ShortestPathToGetFood.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package solutions;
import java.util.LinkedList;
import java.util.Queue;
// [Problem] https://leetcode.com/problems/shortest-path-to-get-food
class ShortestPathToGetFood {
private static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // up, down, left, right
// BFS
// O(m * n) time, O(m * n) space
// where m = rowSize and n = colSize
public int getFood(char[][] grid) {
int rowSize = grid.length, colSize = grid[0].length;
boolean[][] visited = new boolean[rowSize][colSize];
int[] startingLocation = findStartingLocation(grid, rowSize, colSize);
Queue<int[]> locationsToVisit = new LinkedList<>();
locationsToVisit.add(startingLocation);
visited[startingLocation[0]][startingLocation[1]] = true;
int distanceToFood = 0;
while (!locationsToVisit.isEmpty()) {
int nextLocationSize = locationsToVisit.size();
for (int i = 0; i < nextLocationSize; i++) {
int[] location = locationsToVisit.poll();
int row = location[0], col = location[1];
if (grid[row][col] == '#') {
return distanceToFood;
}
for (int[] direction : DIRECTIONS) {
int newRow = row + direction[0], newCol = col + direction[1];
if (isValidLocation(newRow, newCol, rowSize, colSize) && !visited[newRow][newCol]) {
if (grid[newRow][newCol] != 'X') {
locationsToVisit.add(new int[]{newRow, newCol});
}
visited[newRow][newCol] = true;
}
}
}
distanceToFood++;
}
return -1;
}
private int[] findStartingLocation(char[][] grid, int rowSize, int colSize) {
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < colSize; col++) {
char currentSpot = grid[row][col];
if (currentSpot == '*') {
return new int[]{row, col};
}
}
}
throw new RuntimeException();
}
private boolean isValidLocation(int newRow, int newCol, int rowSize, int colSize) {
return newRow >= 0 && newRow < rowSize && newCol >= 0 && newCol < colSize;
}
// Test
public static void main(String[] args) {
ShortestPathToGetFood solution = new ShortestPathToGetFood();
char[][] input1 = new char[][]{
{'X', 'X', 'X', 'X', 'X', 'X'},
{'X', '*', 'O', 'O', 'O', 'X'},
{'X', 'O', 'O', '#', 'O', 'X'},
{'X', 'X', 'X', 'X', 'X', 'X'}};
int expectedOutput1 = 3;
int actualOutput1 = solution.getFood(input1);
System.out.println("Test 1 passed? " + (expectedOutput1 == actualOutput1));
char[][] input2 = new char[][]{
{'X', 'X', 'X', 'X', 'X'},
{'X', '*', 'X', 'O', 'X'},
{'X', 'O', 'X', '#', 'X'},
{'X', 'X', 'X', 'X', 'X'}};
int expectedOutput2 = -1;
int actualOutput2 = solution.getFood(input2);
System.out.println("Test 2 passed? " + (expectedOutput2 == actualOutput2));
}
}