-
Notifications
You must be signed in to change notification settings - Fork 3
/
1091.cpp
28 lines (26 loc) · 1001 Bytes
/
1091.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
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
if (grid[0][0] == 1) return -1;
int n = grid.size();
queue<pair<int, pair<int, int>>> q;
q.push(make_pair(1, make_pair(0, 0)));
grid[0][0] = 1;
while (!q.empty()) {
auto [level, position] = q.front();
auto [x, y] = position;
if (x == n - 1 && y == n - 1) return level;
q.pop();
for (auto direction : directions) {
int _x = x + direction.first;
int _y = y + direction.second;
if (_x < 0 || _x >= n || _y < 0 || _y >= n) continue;
if (grid[_x][_y] != 0) continue;
q.push(make_pair(level + 1, make_pair(_x, _y)));
grid[_x][_y] = 1;
}
}
return -1;
}
};