-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestPathBinaryMatrix.cpp
More file actions
29 lines (29 loc) · 1.04 KB
/
shortestPathBinaryMatrix.cpp
File metadata and controls
29 lines (29 loc) · 1.04 KB
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
class Solution {
public:
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
int n = grid.size();
if (grid[0][0] || grid[n-1][n-1]) return -1;
queue<pair<int, int> > q;
q.push({0, 0});
int ans = 1, dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
vector<vector<bool> > visited(n, vector<bool>(n, false));
visited[0][0] = true;
for (; !q.empty(); ++ans) {
int len = q.size();
while(len--) {
auto [x, y] = q.front();
q.pop();
if (x == n - 1 && y == n - 1)
return ans;
for (int j = 0; j < 8; ++j) {
int nx = x + dir[j][0], ny = y + dir[j][1];
if (nx >= 0 && nx < n && ny >= 0 && ny < n && grid[nx][ny] == 0 && !visited[nx][ny]) {
q.push({nx, ny});
visited[nx][ny] = true;
}
}
}
}
return -1;
}
};