-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1091.) Shortest Path in Binary Matrix.cpp
41 lines (34 loc) · 1.38 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
class Solution {
public:
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
if(grid[0].size() == 0 || grid[0][0] == 1 || grid[grid.size() - 1][grid[0].size() - 1] == 1)
return -1;
vector<int> delRow{-1,-1,-1,1,1,1,0,0};
vector<int> delCol{0,-1,1,0,-1,1,-1,1};
vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size(), false));
queue<pair<pair<int, int>, int>> q;
q.push({{0,0}, 1});
visited[0][0] = true;
while(!q.empty())
{
auto current = q.front();
int currentRow = current.first.first;
int currentCol = current.first.second;
int currentDist = current.second;
q.pop();
if(currentRow == grid.size() - 1 && currentCol == grid[0].size() - 1)
return currentDist;
for(int i=0; i<8; i++)
{
int tempRow = currentRow + delRow[i];
int tempCol = currentCol + delCol[i];
if(tempRow >= 0 && tempRow < grid.size() && tempCol >= 0 && tempCol < grid[0].size() && grid[tempRow][tempCol] == 0 && !visited[tempRow][tempCol])
{
q.push({{tempRow, tempCol}, currentDist + 1});
visited[tempRow][tempCol] = true;
}
}
}
return -1;
}
};