problem
solution
- bfs -> time out
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
48class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
// dfs seem not to work
int n = mat.size(), m = mat[0].size();
vector<vector<int>> ret(n, vector<int>(m, INT_MAX));
queue<vector<int>> q;
vector<int> d = {0,1,0,-1};
for(int i=0;i<n;++i){
for(int j =0;j<m;++j){
if(mat[i][j] == 0){
// bfs can find the shortest path
q.push({i,j});
int depth = 0;
vector<vector<bool>> visited(n, vector<bool>(m, false));
while(!q.empty()){
int size = q.size();
for(int _=0;_<size ; ++_){
vector<int> p = q.front();
q.pop();
int x = p[0], y=p[1];
if(x<0 || y<0 || x>n-1 || y>m-1 || visited[x][y]) continue;
visited[x][y] = true;
if(mat[x][y] ==1) {
ret[x][y] = min(ret[x][y], depth);
}
q.push({x-1, y});
q.push({x+1, y});
q.push({x, y-1});
q.push({x, y+1});
}
depth++;
}
ret[i][j] = 0;
}
}
}
return ret;
}
};
option 1
將 mat[i][j]=1
的地方先收集起來ret[x][y] = 1 + ret[p[0]][p[1]];
1 | class Solution { |