-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShortest Source to Destination Path.cpp
101 lines (77 loc) · 1.54 KB
/
Shortest Source to Destination Path.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// #include<iostream>
// #include<cstdio>
// #include<queue>
#include<bits/stdc++.h>
using namespace std;
// int n,m;
#define n 20
#define m 20
struct Node{
int x, y, dist;
};
int row[] = {-1,1,0,0};
int col[] = {0,0,1,-1};
bool isSafe(int mat[][m], bool visited[][m], int x, int y){
return (x >=0) and (x < n) and (y>=0) and (y < m) and (mat[x][y] == 1) and (!visited[x][y]);
}
void BFS(int mat[][m], int i,int j, int x, int y){
if(mat[i][j] == 0 or mat[x][y] == 0){
// Imp Trivial case
cout << -1 <<endl;
return;
}
bool visited[n][m];
memset(visited, false, sizeof(visited));
queue<Node> q;
q.push({i,j,0});
visited[i][j] = true;
int ans = -1;
while (!q.empty()) {
/* code */
Node t = q.front();
q.pop();
// terminating condition
if(t.x == x and t.y == y){
ans = t.dist;
break;
}
for(int k = 0; k < 4; k ++){
if(isSafe(mat, visited, t.x + row[k], t.y + col[k])){
q.push({t.x + row[k], t.y + col[k], t.dist + 1});
visited[t.x + row[k]][t.y + col[k]] = true;
}
}
}//
if(ans != -1){
cout << ans <<endl;// min steps
}else
cout << -1 <<endl;// no path
}
void solve(){
int r,u;
cin>>r>>u;
int mat[n][m];
memset(mat,0,sizeof(mat));
for (size_t i = 0; i < r; i++) {
/* code */
for (size_t j = 0; j < u; j++) {
/* code */
int x;
cin>>x;
mat[i][j] = x;
}
}
int a,b;
cin>>a>>b;
BFS(mat, 0, 0,a,b);
}
int main()
{
//code
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}