-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotd_28_04.cpp
42 lines (42 loc) · 1.33 KB
/
potd_28_04.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
class Solution{
public:
vector<vector<int>> chefAndWells(int n,int m,vector<vector<char>> &c){
// Code here
vector<vector<int>> rc = {{1,-1,0,0},{0,0,1,-1}};
vector<vector<bool>> vis(n,vector<bool>(m,false));
queue<pair<int,int>> q;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(c[i][j]=='W'){
q.push({i,j});
vis[i][j] = true;
}
}
}
vector<vector<int>> ans(n,vector<int>(m,0));
int stp = 0;
while(q.size()){
int size = q.size();
while(size--){
int i = q.front().first;
int j = q.front().second;
if(c[i][j]=='H') ans[i][j] = 2*stp;
q.pop();
for(int k=0;k<4;k++){
int row = rc[0][k] + i;
int col = rc[1][k] + j;
if(row<0 || row==n || col<0 || col==m || vis[row][col] || c[row][col]=='N') continue;
vis[row][col] = true;
q.push({row,col});
}
}
stp++;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(c[i][j]=='H' && ans[i][j]==0) ans[i][j] = -1;
}
}
return ans;
}
};