开始我是直接用的bfs模板进行的,没有过,好像是内存过线了....
以下是没过的代码:
#include <iostream>
#include<queue>
#include<string>
using namespace std;
string a[1001];
int dir[]={0,1,0,-1,0};
struct node
{
int x,y;
int step;
};
int main()
{
int n,m;
char ch;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='1')
{
cout<<"0"<<" ";
continue;
}
int flag=1;
queue<node> q;
node start; start.x=i,start.y=j,start.step=0;
q.push(start);
while(!q.empty()&&flag)
{
node temp=q.front();
q.pop();
int xx,yy;
for(int k=0;k<4;k++)
{
node obj=temp;
xx=obj.x+dir[k]; yy=obj.y+dir[k+1];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
{
if(a[xx][yy]=='1')
{
cout<<temp.step+1<<" ";
flag=0;
break;
}
else
{
obj.x=xx; obj.y=yy; obj.step=temp.step+1;
q.push(obj);
}
}
}</node></string></queue></iostream>

        }

    }
    cout<<endl;
}
return 0;

}