给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。
两个相邻元素间的距离为 1 。
示例 1:
输入:
0 0 0 0 1 0 0 0 0
输出:
0 0 0 0 1 0 0 0 0
示例 2:
输入:
0 0 0 0 1 0 1 1 1
输出:
0 0 0 0 1 0 1 2 1
注意:
- 给定矩阵的元素个数不超过 10000。
- 给定矩阵中至少有一个元素是 0。
- 矩阵中的元素只在四个方向上相邻: 上、下、左、右。
思路:
BFS每个点
坑:
- 不需要用visited数组,因为每一层遍历完了之后,下一次还是可能会过来
- 可以先上下再左右
class Solution {
public:
struct Point
{
public:
int X;
int Y;
};
bool testpoint(Point po,int maxx,int maxy)
{
if (po.X>=0&&po.X<=maxx)
if (po.Y>=0&&po.Y<=maxy)
return true;
return false;
}
int BFS(int line,int row,vector<vector<int>>& matrix)
{
if(matrix[line][row]==0)
return 0;
//
int lindex = matrix.size() - 1;//line
int rindex=matrix[0].size()-1; //row
int len=1;
queue<Point> q;
Point po;
po.X=line;
po.Y=row;
q.push(po);
while (!q.empty())
{
Point tmppo,nextpo;
nextpo=tmppo = q.front();
q.pop();
nextpo.X=tmppo.X+1;//下
nextpo.Y=tmppo.Y;
if (testpoint(nextpo, lindex,rindex) )
{
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line-nextpo.X)+abs(row-nextpo.Y);
}
nextpo.X=tmppo.X-1;//上
nextpo.Y=tmppo.Y;
if (testpoint(nextpo, lindex, rindex))
{
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line - nextpo.X) + abs(row - nextpo.Y);
}
nextpo.X = tmppo.X;
nextpo.Y = tmppo.Y - 1;//左
if (testpoint(nextpo, lindex, rindex))
{
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line - nextpo.X) + abs(row - nextpo.Y);
}
nextpo.X=tmppo.X;
nextpo.Y=tmppo.Y+1;//右
if (testpoint(nextpo, lindex, rindex))
{
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line - nextpo.X) + abs(row - nextpo.Y);
}
len++;
}
return len;
}
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
vector<vector<int>> res;
int lenl=matrix.size();//line
int lenr=matrix[0].size();//row
for (int i=0;i<lenl;i++)
{
vector<int> tmpv;
for (int j=0;j<lenr;j++)
{
tmpv.push_back(BFS(i, j, matrix));
}
res.push_back(tmpv);
}
return res;
}
};
调试代码:
加上了visited数组的,可是这样就超时了。。。
#include <iostream>
#include <vector>
#include <queue>
#include <stdlib.h>
using namespace std;
struct Point
{
public:
int X;
int Y;
};
bool testpoint(Point po,int maxx,int maxy)
{
if (po.X>=0&&po.X<=maxx)
if (po.Y>=0&&po.Y<=maxy)
return true;
return false;
}
int BFS(int line,int row,vector<vector<int>>& matrix)
{
if(matrix[line][row]==0)
return 0;
int visited[10000][10000] = { 0 };
int lindex = matrix.size() - 1;//line
int rindex=matrix[0].size()-1; //row
int len=1;
queue<Point> q;
Point po;
po.X=line;
po.Y=row;
q.push(po);
visited[line][row] = 1;
while (!q.empty())
{
Point tmppo,nextpo;
nextpo=tmppo = q.front();
q.pop();
nextpo.X=tmppo.X+1;//下
nextpo.Y=tmppo.Y;
if (testpoint(nextpo, lindex, rindex) && !visited[nextpo.X][nextpo.Y])
{
visited[nextpo.X][nextpo.Y] = 1;
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line-nextpo.X)+abs(row-nextpo.Y);
}
nextpo.X=tmppo.X-1;//上
nextpo.Y=tmppo.Y;
if (testpoint(nextpo, lindex, rindex) && !visited[nextpo.X][nextpo.Y])
{
visited[nextpo.X][nextpo.Y] = 1;
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line - nextpo.X) + abs(row - nextpo.Y);
}
nextpo.X = tmppo.X;
nextpo.Y = tmppo.Y - 1;//左
if (testpoint(nextpo, lindex, rindex) && !visited[nextpo.X][nextpo.Y])
{
visited[nextpo.X][nextpo.Y] = 1;
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line - nextpo.X) + abs(row - nextpo.Y);
}
nextpo.X=tmppo.X;
nextpo.Y=tmppo.Y+1;//右
if (testpoint(nextpo, lindex, rindex) && !visited[nextpo.X][nextpo.Y])
{
visited[nextpo.X][nextpo.Y] = 1;
q.push(nextpo);
if(!matrix[nextpo.X][nextpo.Y])
return abs(line - nextpo.X) + abs(row - nextpo.Y);
}
len++;
}
return len;
}
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
vector<vector<int>> res;
int lenl=matrix.size();//line
int lenr=matrix[0].size();//row
for (int i=0;i<lenl;i++)
{
vector<int> tmpv;
for (int j=0;j<lenr;j++)
{
tmpv.push_back(BFS(i, j, matrix));
}
res.push_back(tmpv);
}
return res;
}
int main()
{
vector<vector<int>> v;
char c;
int i=0;
int j=0;
int a[100][100]={0};
vector<int> tmpv;
while ((c=getchar())!=EOF){
if(c!='\n'){
ungetc(c,stdin);
cin>>a[i][j];
tmpv.push_back(a[i][j++]);
}
else{
i++;j=0;
v.push_back(tmpv);
tmpv.clear();
}
}
vector<vector<int>> res=updateMatrix(v);
i=i;
return 0;
}