-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest_distance_binary_maze.cpp
68 lines (58 loc) · 1.9 KB
/
shortest_distance_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
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
int shortestPath(vector<vector<int>> &grid, pair<int, int> source,
pair<int, int> destination) {
int n=grid.size();
int m=grid[0].size();
queue<pair<int,pair<int,int>>>q;
vector<vector<int>>dist(n,vector<int>(m,1e9));
dist[source.first][source.second]=0;
q.push({0,source});
int drow[]={-1,0,1,0};
int dcol[]={0,1,0,-1};
while(!q.empty()){
int d=q.front().first;
int r=q.front().second.first;
int c=q.front().second.second;
q.pop();
for(int i=0;i<4;i++){
int nrow=r+drow[i];
int ncol=c+dcol[i];
if(nrow>=0&&ncol>=0&&nrow<n&&ncol<m&&grid[nrow][ncol]==1&&d+1<dist[nrow][ncol]){
dist[nrow][ncol]=d+1;
if(nrow==destination.first&&ncol==destination.second)return d+1;
q.push({d+1,{nrow,ncol}});
}
}
}
if(dist[destination.first][destination.second]==1e9)return -1;
else return dist[destination.first][destination.second];
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
pair<int, int> source, destination;
cin >> source.first >> source.second;
cin >> destination.first >> destination.second;
Solution obj;
cout << obj.shortestPath(grid, source, destination) << endl;
}
}
// } Driver Code Ends