-
Notifications
You must be signed in to change notification settings - Fork 0
/
1091. Shortest Path in Binary Matrix.cpp
82 lines (79 loc) · 2.8 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
using pathType = pair<int, int>;
class Solution {
int startMark = -1, endMark = -2;
queue<pathType> q1{{{0, 0}}}, q2;
void markAndAdd(vector<vector<int>> &grid, int x, int y) {
grid[y][x] = startMark;
q1.push({x, y});
}
public:
int shortestPathBinaryMatrix(vector<vector<int>> &grid) {
int t = grid.size() - 1, res = bool(t), qLen = 1, cx, cy, x, y, currVal;
if (grid[0][0] || grid[t][t]) return -1;
q2.push({t, t});
grid[0][0] = startMark;
grid[t][t] = endMark;
while (qLen) {
res++;
while (qLen--) {
auto &front = q1.front();
cx = front.first;
cy = front.second;
if (grid[cy][cx] == endMark) return res;
q1.pop();
x = cx - 1, y = cy - 1;
if (x >= 0 && y >= 0) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
x = cx + 1;
if (x <= t && y >= 0) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
y = cy + 1;
if (x <= t && y <= t) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
x = cx - 1;
if (x >= 0 && y <= t) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
y = cy;
if (x >= 0) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
x = cx + 1;
if (x <= t) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
x = cx, y = cy - 1;
if (y >= 0) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
y = cy + 1;
if (y <= t) {
currVal = grid[y][x];
if (currVal == endMark) return res;
if (!currVal) markAndAdd(grid, x, y);
}
}
swap(q1, q2);
swap(startMark, endMark);
qLen = q1.size();
}
return -1;
}
};