-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGuardInABank.cpp
More file actions
70 lines (64 loc) · 1.86 KB
/
Copy pathGuardInABank.cpp
File metadata and controls
70 lines (64 loc) · 1.86 KB
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
/* GFG Tested - DSA Workshop Week 9
Given an M x N character matrix that is filled with the characters ‘O’, ‘G’, and ‘W’ where:
‘O’ represents an open space.
‘G’ represents a guards
‘W’ represents a wall.
You have to replace all of the O’s in the matrix with their shortest distance from a guard without being able to go through any walls.
Also, replace the guards with 0 and walls with -1 in the resultant matrix. If no path exists replace 'O' with -1.
*/
// BFS Approach
// left,right,up,down
int r[]={0,0,-1,1};
int c[]={-1,1,0,0};
typedef pair<int,int> pii;
vector<vector<int>> findDistance(vector<vector<char>> &matrix, int M, int N)
{
vector<vector<int>> ans(M,vector<int>(N,-2)); // -2 is for unvisited rows
vector<vector<int>> visited(M,vector<int>(N,0));
queue<pii> q;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
if(matrix[i][j]=='G')
{
q.push(make_pair(i,j));
visited[i][j]=1;
ans[i][j]=0;
}
}
}
while(!q.empty())
{
pii p=q.front();
q.pop();
int i=p.first,j=p.second;
for(int k=0;k<4;k++)
{
int i_temp=i+r[k];
int j_temp=j+c[k];
if(i_temp>=0&&i_temp<M&&j_temp>=0&&j_temp<N&&visited[i_temp][j_temp]==0)
{
visited[i_temp][j_temp]=1;
if(matrix[i_temp][j_temp]=='W')
ans[i_temp][j_temp]=-1;
else
{
ans[i_temp][j_temp]=ans[i][j]+1;
q.push(make_pair(i_temp,j_temp));
}
}
}
}
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
if(ans[i][j]==-2)
{
ans[i][j]=-1;
}
}
}
return ans;
}