542. 01 Matrix[Medium]
Description
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:
1.The number of elements of the given matrix will not exceed 10,000.
2.There are at least one 0 in the given matrix.
3.The cells are adjacent in only four directions: up, down, left and right.
Solution
这道题期中考试的时候做过。
给出一个只有0和1的矩阵,求每一个1到最近的0的距离。理所当然想着动态规划,考试的时候只考虑了左上开始扫一遍,然后WA了…..
发现这样不可取,因为有些点在0周围但是比较靠后,所以更新值的时候就会错误。但是当时也没时间仔细想动态规划的正解是什么,就用了一个比较暴力的方法。先找出所有的0,然后用当下循环到的1和每一个0求距离,三重循环求最小的那个值。
考试的时候过了,上leetcode发现TLE……然后只好老老实实想动态规划,从左上扫一遍不行,要加上从右下开始扫,一共扫描两遍,就可以正确更新所有的距离(没有证明,只是感觉)
【本来还想着要是不能过就左下右上扫四遍,这样肯定能过,然后发现AC了,这个正确性的证明留着以后再想吧】
看了看Discussion,发现可以用BFS求距离场,用一个queue存所有的0,然后以0为起点,遍历上下左右,如果有更新的必要,就将该点(上下左右中遍历到的那个)的值更新为当前值加1,然后把这个点加入queue。这样就可以保证0周围的点首先得到更新,不会影响到后序点的更新。【其实三重循环和这个的思路差不多,就是比较质朴地枚举了所有情况然后取最小值,多花一重循环所以超时;这里迭代更新,不需要花费额外的比较时间】
Complexity analysis
O(mn)
Code
先放个过不了的三重循环
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<vector<int>> result(m, vector<int>(n, INT_MAX));
vector<pair<int, int>> vec;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
vec.push_back(make_pair(i, j));
result[i][j] = 0;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (result[i][j] != 0) {
int min = INT_MAX;
for (int k = 0; k < vec.size(); k++) {
int x = abs(i - vec[k].first);
int y = abs(j - vec[k].second);
if (x+y < min) {
min = x+y;
}
}
result[i][j] = min;
}
}
}
return result;
}
};
左上右下两次扫描:
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> res(m, vector<int>(n, INT_MAX - 1));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == 0) res[i][j] = 0;
else {
if (i > 0) res[i][j] = min(res[i][j], res[i - 1][j] + 1);
if (j > 0) res[i][j] = min(res[i][j], res[i][j - 1] + 1);
}
}
}
for (int i = m - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
if (res[i][j] != 0 && res[i][j] != 1) {
if (i < m - 1) res[i][j] = min(res[i][j], res[i + 1][j] + 1);
if (j < n - 1) res[i][j] = min(res[i][j], res[i][j + 1] + 1);
}
}
}
return res;
}
};
参考别人写的BFS:
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> dirs{{0,-1},{-1,0},{0,1},{1,0}};
queue<pair<int, int>> q;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == 0) q.push({i, j});
else matrix[i][j] = INT_MAX;
}
}
while (!q.empty()) {
auto t = q.front(); q.pop();
for (auto dir : dirs) {
int x = t.first + dir[0], y = t.second + dir[1];
if (x < 0 || x >= m || y < 0 || y >= n ||
matrix[x][y] <= matrix[t.first][t.second]) continue;
matrix[x][y] = matrix[t.first][t.second] + 1;
q.push({x, y});
}
}
return matrix;
}
};