forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01 Matrix.cpp
49 lines (47 loc) · 1.31 KB
/
01 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
// Runtime: 192 ms (Top 16.37%) | Memory: 30 MB (Top 73.15%)
class Solution {
bool isValid(vector<vector<int>>& grid,int r,int c,int nr,int nc,int m,int n){
if(nr>=0 && nc>=0 && nr<m && nc<n && grid[nr][nc]==-1){
if(grid[r][c]==0)
grid[nr][nc]=1;
else
grid[nr][nc]=grid[r][c]+1;
return 1;
}
return 0;
}
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
queue<pair<int,int>> q;
int m = mat.size();
int n = mat[0].size();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(mat[i][j]==0)
q.push({i,j});
else
mat[i][j]=-1;
}
}
while(!q.empty()){
auto per = q.front();
int r = per.first;
int c = per.second;
// if()
q.pop();
if(isValid(mat,r,c,r-1,c,m,n)){
q.push({r-1,c});
}
if(isValid(mat,r,c,r+1,c,m,n)){
q.push({r+1,c});
}
if(isValid(mat,r,c,r,c-1,m,n)){
q.push({r,c-1});
}
if(isValid(mat,r,c,r,c+1,m,n)){
q.push({r,c+1});
}
}
return mat;
}
};