Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1.
Example 1: Input:
0 0 0 0 1 0 0 0 0
Output:
0 0 0 0 1 0 0 0 0
Example 2: Input:
0 0 0 0 1 0 1 1 1
Output:
0 0 0 0 1 0 1 2 1
Note:
The number of elements of the given matrix will not exceed 10,000.
There are at least one 0 in the given matrix.
The cells are adjacent in only four directions: up, down, left and right.
classSolution { voidpush_n(vector<vector<int>>& q, unordered_set<int*>& s, int i , int j){ // push neightbor if not in set (and if value <= 1? optimize for later? we may wanto avoid any values already resolved) // we want to make sure we don't check any nodes int m = v[0].size() - 1; int n = v.size() - 1;
// left if (j > 0) { if (s.find(&v[i][j-1]) == s.end()) { v.push_back([i,j-1]); } }
// right if (j < m) { if (s.find(&v[i][j+1]) == s.end()) { v.push_back([i,j+1]); } }
// up if (i > 0) { if (s.find(&v[i-1][j]) == s.end()) { v.push_back([i-1,j]); } }
// down if (i < n) { if (s.find(&v[i+1][j]) == s.end()) { v.push_back([i+1,j]); } } } public: vector<vector<int>> u pdateMatrix(vector<vector<int>>& v){ for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v[0].size(); j++) { int dist = INT_MAX; if (j > 0) dist = min(dist, v[i][j-1]); if (i > 0) dist = min(dist, v[i-1][j]); if (dist == 1) continue; int level = 0; vector<vector<int>> q; unordered_set<int*> vis; q.push_back([i,j]); vis.insert(&v[i][j]); while(!q.empty()) { if (level >= dist) break; bool done = false; for(int len = q.size();len>0; len--) { auto t = q.front(); q.pop(); int x = t[0]; int y = t[1]; if (v[x][y] == 0) { done = true; break; } vis.insert(&v[x][y]); push_n(q, vis, x, y); } if (done) break; level++; } dist = min(dist, level); v[i][j] = dist; } } return v; } };
classSolution { voidpush_n(vector<vector<int>>& v, vector<vector<int>>& q, unordered_set<int*>& s, int i , int j){ // push neightbor if not in set (and if value <= 1? optimize for later? we may wanto avoid any values already resolved) // we want to make sure we don't check any nodes int m = v[0].size() - 1; int n = v.size() - 1;
// left if (j > 0) { if (s.find(&v[i][j-1]) == s.end()) { vector<int> a = {i, j-1}; q.push_back(a); //printf("pushed left %d %d\n", i, j-1); } }
// right if (j < m) { if (s.find(&v[i][j+1]) == s.end()) { vector<int> a = {i, j+1}; q.push_back(a); } }
// up if (i > 0) { if (s.find(&v[i-1][j]) == s.end()) { vector<int> a = {i-1, j}; q.push_back(a); } }
// down if (i < n) { if (s.find(&v[i+1][j]) == s.end()) { vector<int> a = {i+1, j}; q.push_back(a); } }
}
public: vector<vector<int>> updateMatrix(vector<vector<int>>& v) { for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v[0].size(); j++) { int dist = INT_MAX; if (j > 0) dist = min(dist, v[i][j-1]+1); if (i > 0) dist = min(dist, v[i-1][j]+1); if (dist == 1) continue; int level = 0; vector<vector<int>> q; unordered_set<int*> vis; vector<int> a = {i,j}; q.push_back(a); vis.insert(&v[i][j]); while(!q.empty()) { //cout << "level = " << level << endl; if (level >= dist) break; bool done = false; for(int len = q.size();len>0; len--) {
vector<int> t = q.front(); q.erase(q.begin()); int x = t[0]; int y = t[1]; //cout << "got elem =" << v[x][y] << endl; if (v[x][y] == 0) { done = true; break; }