qingtianyu @ 2019-03-12 20:44:19
#include<iostream>
#include<queue>
#include<string>
using namespace std;
int n;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int x2,y2;
string s[1005];
bool v[1005][1005];
struct node
{
int x;
int y;
int num;
};
queue<node>q;
int bfs(int a, int b)
{
node t;
t.x=a;
t.y=b;
t.num=0;
v[t.x][t.y]=true;
q.push(t);
while(!q.empty())
{
node k=q.front();
q.pop();
cout<<k.num<<endl;
for(int i=0;i<4;i++)
{
int fx=k.x+dx[i];
int fy=k.y+dy[i];
if(fx>=0&&fx<n&&fy>=0&&fy<n&&!v[fx][fy]&&s[fx][fy]!='1')
{
node w;
w.x=fx;
w.y=fy;
v[w.x][w.y]=true;
w.num=k.num+1;
if(w.x==x2&&w.y==y2)
return w.num;
q.push(w);
}
}
}
}
int main()
{
cin >> n;
getchar();
for(int i=1;i<=n;i++)
{
getline(cin,s[i]);
}
int x1,y1;
cin >>x1 >> y1 >>x2 >> y2;
x1-=1;
y1-=1;
x2-=1;
y2-=1;
cout<<bfs(x1,y1)<<endl;
return 0;
}
by qingtianyu @ 2019-03-12 20:44:55
结果的数很大。 看了半天没看出来。。。
by HearTheWindSing @ 2019-03-16 19:23:37
你在while开头pop的话会把你push的t给pop出去的