-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshortest_path_binary_matrix.cpp
64 lines (53 loc) · 2.12 KB
/
shortest_path_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
class Solution {
public:
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
int r = grid.size(), c = grid[0].size(), path_steps = 0;
vector<vector<bool>> visited(r, vector(c, false));
queue<pair<int, int>> q;
if(grid[0][0] == 1 || grid[r-1][c-1]== 1) //indicating blocked state
return -1;
int deviations[8][2] = {
{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}
};
q.push({0,0});
visited[0][0] = true;
while(!q.empty()){
int sz = q.size();
path_steps++;
for(int i=0; i<sz; i++){
int x = q.front().first;
int y = q.front().second;
q.pop();
if(x == r - 1 && y == c - 1) //reached end state
return path_steps;
for(int j=0; j<8; j++){
int newx = x + deviations[j][0];
int newy = y + deviations[j][1];
if(newx >=0 && newx < r && newy >=0 && newy < c && !visited[newx][newy] && grid[newx][newy] == 0){
q.push({newx, newy});
visited[newx][newy] = true;
}
}
}
}
return -1;
}
};
/*
class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
r, c = len(grid), len(grid[0])
if grid[0][0] == 1 or grid[r-1][c-1] == 1: #blocked state
return -1
queue = collections.deque([(0,0,1)])
while queue:
x, y, count = queue.popleft()
if x == r - 1 and y == c - 1: #end state
return count
for ddx, ddy in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)):
dx, dy = x + ddx, y + ddy
if dx >= 0 and dx < r and dy >= 0 and dy < c and not grid[dx][dy]:
queue.append((dx, dy, count + 1))
grid[dx][dy] = 1
return -1
*/