-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01Matrix.cpp
More file actions
31 lines (31 loc) · 1008 Bytes
/
01Matrix.cpp
File metadata and controls
31 lines (31 loc) · 1008 Bytes
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
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
queue<pair<int,int>> q;
vector<vector<int>> res(m,vector<int>(n,INT_MAX));
for (int i = 0 ; i < m; ++i){
for (int j = 0 ; j < n ; ++j){
if (matrix[i][j] == 0){
res[i][j] = 0;
q.push({i,j});
}
}
}
vector<vector<int>> dir = {{0,1},{1,0},{-1,0},{0,-1}};
while(!q.empty()){
auto s = q.front();
q.pop();
int candidate = res[s.first][s.second]+1;
for (auto x:dir){
int nx = s.first+x[0];
int ny = s.second+x[1];
if (nx>=0 && ny >=0 && nx < m && ny < n ){
if (candidate < res[nx][ny]) res[nx][ny] = candidate,q.push({nx,ny});
}
}
}
return res;
}
};