-
Notifications
You must be signed in to change notification settings - Fork 127
/
Shortest Distance in a Binary Maze.cpp
69 lines (52 loc) · 1.46 KB
/
Shortest Distance in a Binary Maze.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
class Solution {
public:
int shortestPath(vector<vector<int>> &grid, pair<int, int> source,
pair<int, int> d) {
// code here
if(grid[d.first][d.second]==0)
return -1;
int n=grid.size();
int m=grid[0].size();
int dp[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
dp[i][j]=-1;
}
}
queue<pair<int,int>> q;
dp[source.first][source.second]=0;
q.push(source);
int ans=-1;
while(!q.empty())
{
pair<int,int> t=q.front();
q.pop();
int i=t.first;
int j=t.second;
if(i-1>=0&&dp[i-1][j]==-1&&grid[i-1][j]==1){
dp[i-1][j]=dp[i][j]+1;
q.push({i-1,j});
}
if(j-1>=0&&dp[i][j-1]==-1&&grid[i][j-1]==1){
dp[i][j-1]=dp[i][j]+1;
q.push({i,j-1});
}
if(i+1<n&&dp[i+1][j]==-1&&grid[i+1][j]==1){
dp[i+1][j]=dp[i][j]+1;
q.push({i+1,j});
}
if(j+1<m&&dp[i][j+1]==-1&&grid[i][j+1]==1){
dp[i][j+1]=dp[i][j]+1;
q.push({i,j+1});
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i==d.first&&j==d.second){
ans=dp[i][j];
}
}
}
return ans;
}
};