forked from Chayandas07/LeetCode-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1091. Shortest Path in Binary Matrix
More file actions
29 lines (26 loc) · 951 Bytes
/
1091. Shortest Path in Binary Matrix
File metadata and controls
29 lines (26 loc) · 951 Bytes
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] != 0 || grid[n-1][n-1] != 0) return -1;
queue<pair<int,int>> q;
q.push({0,0});
pair<int,int>dirs[8] = {{0,1},{1,0},{0,-1},{-1,0},{-1,-1},{1,1},{1,-1},{-1,1}};
int dx,dy;
grid[0][0] = 1;
while(!q.empty()){
pair<int,int> curr = q.front();
q.pop();
if(curr.first == n - 1 && curr.second == n-1) return grid[curr.first][curr.second];
for(auto x:dirs){
dx = x.first + curr.first;
dy = x.second + curr.second;
if(dx >= 0 && dy >= 0 && dx < n && dy < n && grid[dx][dy] == 0){
q.push({dx,dy});
grid[dx][dy]=grid[curr.first][curr.second]+1;
}
}
}
return -1;
}
};