forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
64 lines (49 loc) · 1.53 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// Source : https://leetcode.com/problems/shortest-path-in-binary-matrix/
/// Author : liuyubobobo
/// Time : 2019-06-15
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/// BFS
/// Time Complexity: O(n * m)
/// Space Complexity: O(n * m)
class Solution {
private:
const int d[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int n, m;
public:
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
if(grid[0][0]) return -1;
n = grid.size(), m = grid[0].size();
vector<vector<int>> res(n, vector<int>(m, -2));
queue<int> q;
res[0][0] = 0;
q.push(0);
while(!q.empty()){
int x = q.front() / m;
int y = q.front() % m;
q.pop();
for(int i = 0; i < 8; i ++){
int newX = x + d[i][0], newY = y + d[i][1];
if(newX >= 0 && newX < n && newY >= 0 && newY < m &&
grid[newX][newY] == 0 && res[newX][newY] == -2){
res[newX][newY] = res[x][y] + 1;
q.push(newX * m + newY);
}
}
}
// for(int i = 0; i < n; i ++){
// for(int j = 0; j < m; j ++)
// cout << res[i][j] << " ";
// cout << endl;
// }
return res[n - 1][m - 1] + 1;
}
};
int main() {
vector<vector<int>> grid = {{0,0,0},{1,1,0},{1,1,0}};
cout << Solution().shortestPathBinaryMatrix(grid) << endl;
return 0;
}