-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG - Find shortest safe route in a matrix.cpp
95 lines (68 loc) · 2.52 KB
/
GFG - Find shortest safe route in a 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
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
// Given a path in the form of a rectangular matrix having few landmines arbitrarily placed (marked as 0),
// calculate length of the shortest safe route possible from any cell in the first column to any cell in the last column of the matrix.
// We have to avoid landmines and their four adjacent cells (left, right, above and below) as they are also unsafe. We are allowed to move to only adjacent cells which are not landmines. i.e. the route cannot contains any diagonal moves.
class Solution
{
bool isValid(int n, int m, int row, int col){
if(row>=0 && row<n && col>=0 && col<m){
return true;
}
return false;
}
public:
int findShortestPath(vector<vector<int>> &mat)
{
int n=mat.size();
int m=mat[0].size();
int delr[]={-1, 0, +1, 0};
int delc[]={0, -1, 0, +1};
queue<pair<int, int>> q;
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(mat[i][j]==0){
q.push({i, j});
}
}
}
while(!q.empty()){
auto curr = q.front();
int row = curr.first;
int col = curr.second;
q.pop();
for(int i=0;i<4;++i){
int newr = row+delr[i];
int newc = col+delc[i];
if(isValid(n, m, newr, newc)){
mat[newr][newc]=0;
}
}
}
vector<vector<int>> vis(n, vector<int>(m, 0));
queue<pair<int, pair<int, int>>> pq;
for(int i=0;i<n;++i){
if(mat[i][0]==1) {
pq.push({1, {i, 0}});
vis[i][0]=1;
}
}
int del_r[]={-1, +1, 0};
int del_c[]={0, 0, +1};
while(!pq.empty()){
auto it= pq.front();
int steps=it.first;
int row = it.second.first;
int col = it.second.second;
if(col==m-1) return steps;
pq.pop();
for(int ind=0;ind<4;++ind){
int newr = row+del_r[ind];
int newc = col+del_c[ind];
if(isValid(n, m, newr, newc) && vis[newr][newc]!=1){
if(mat[newr][newc]==1) pq.push({steps+1,{newr, newc}});
vis[newr][newc]=1;
}
}
}
return -1;
}
};