-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortest_path_in_binary_matrix.cpp
More file actions
35 lines (32 loc) · 1007 Bytes
/
shortest_path_in_binary_matrix.cpp
File metadata and controls
35 lines (32 loc) · 1007 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
30
31
32
33
34
35
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int shortestPathBinaryMatrix(vector<vector<int>>& g) {
int n = g.size();
int dd[] = {1, 0, -1, 0, 1, -1, -1, 1, 1};
queue<pair<int,int> > q;
if(!g[0][0])
q.push({0,0});
int d = 0;
while(!q.empty()) {
int sz = q.size();
d++;
for(int i = 0; i < sz; ++i) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if(x == n - 1 && y == n - 1) return d;
for(int i = 0; i < 8; ++i) {
int dx = x + dd[i];
int dy = y + dd[i + 1];
if(dx < n && dy < n && dx >= 0 && dy >= 0) {
if(!g[dx][dy])
g[dx][dy] = 1, q.push({dx, dy});
}
}
}
}
return -1;
}
};