-
Notifications
You must be signed in to change notification settings - Fork 0
/
1091.shortest-path-in-binary-matrix.cpp
115 lines (113 loc) · 2.77 KB
/
1091.shortest-path-in-binary-matrix.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* @lc app=leetcode id=1091 lang=cpp
*
* [1091] Shortest Path in Binary Matrix
*
* https://leetcode.com/problems/shortest-path-in-binary-matrix/description/
*
* algorithms
* Medium (38.95%)
* Likes: 917
* Dislikes: 65
* Total Accepted: 65.3K
* Total Submissions: 164.5K
* Testcase Example: '[[0,1],[1,0]]'
*
* In an N by N square grid, each cell is either empty (0) or blocked (1).
*
* A clear path from top-left to bottom-right has length k if and only if it is
* composed of cells C_1, C_2, ..., C_k such that:
*
*
* Adjacent cells C_i and C_{i+1} are connected 8-directionally (ie., they are
* different and share an edge or corner)
* C_1 is at location (0, 0) (ie. has value grid[0][0])
* C_k is at location (N-1, N-1) (ie. has value grid[N-1][N-1])
* If C_i is located at (r, c), then grid[r][c] is empty (ie. grid[r][c] ==
* 0).
*
*
* Return the length of the shortest such clear path from top-left to
* bottom-right. If such a path does not exist, return -1.
*
*
*
* Example 1:
*
*
* Input: [[0,1],[1,0]]
*
*
* Output: 2
*
*
*
*
* Example 2:
*
*
* Input: [[0,0,0],[1,1,0],[1,1,0]]
*
*
* Output: 4
*
*
*
*
*
*
* Note:
*
*
* 1 <= grid.length == grid[0].length <= 100
* grid[r][c] is 0 or 1
*
*
*/
// @lc code=start
class Solution {
public:
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
if (grid.size() == 0 || grid[0][0])
return -1;
if (grid.size() == 1 && grid[0].size() == 1 && !grid[0][0])
return 1;
int rows = grid.size();
int cols = grid[0].size();
vector<vector<int>> dir = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0},
{1, 1},
{1, -1},
{-1, 1},
{-1, -1}
};
int result = 0;
queue<pair<int, int>> que;
que.push(make_pair(0, 0));
while (!que.empty()) {
int size = que.size();
result++;
for (int i = 0; i < size; ++i) {
auto cur = que.front();
que.pop();
for (int d = 0; d < dir.size(); ++d) {
int newRow = cur.first + dir[d][0];
int newCol = cur.second + dir[d][1];
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && !grid[newRow][newCol]) {
que.push(make_pair(newRow, newCol));
grid[newRow][newCol] = 1;
if (newRow == rows - 1 && newCol == cols - 1)
return ++result;
}
}
}
}
return -1;
}
}
};
// @lc code=end