-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
37 lines (36 loc) · 1.13 KB
/
Solution.cpp
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
class Solution {
public:
const static inline vector<int> dirs = {-1, 0, 1, 0, -1};
int getFood(vector<vector<char>>& grid) {
int m = grid.size(), n = grid[0].size();
queue<pair<int, int>> q;
for (int i = 0, x = 1; i < m && x == 1; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '*') {
q.emplace(i, j);
x = 0;
break;
}
}
}
int ans = 0;
while (!q.empty()) {
++ans;
for (int t = q.size(); t; --t) {
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
if (grid[x][y] == '#') return ans;
if (grid[x][y] == 'O') {
grid[x][y] = 'X';
q.emplace(x, y);
}
}
}
}
}
return -1;
}
};