营救(save)
思路:本题就是一个广搜模板然而我考试的时候却写了深搜以至于T掉爆了零
AC代码:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y,t;
};//x,y表示坐标,t表示步数
int n,sx,sy,ex,ey;
bool vis[1010][1010];
string mp[1010];
queue<node> qu;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
bool in(int x,int y)
{
return 1<=x&&x<=n&&1<=y&&y<=n;
}
int BFS()
{
node tmp;
tmp.x =sx;
tmp.y=sy;
tmp.t=0;
qu.push(tmp);
vis[sx][sy]=1;
while(!qu.empty())
{
node now=qu.front();
qu.pop();
for(int i=0;i<4;i++)
{
int tx=now.x+dir[i][0];
int ty=now.y+dir[i][1];
if(in( tx,ty )&&mp[tx][ty]=='0'&&vis[tx][ty]==0)
{
if( tx==ex&&ty==ey)
{
return now.t+1;
}
node tmp2;
tmp2.x =tx;
tmp2.y =ty;
tmp2.t =now.t +1;
qu.push(tmp2);
vis[tx][ty]=1;
}
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>mp[i];
mp[i]=" "+mp[i];
}
cin>>sx>>sy>>ex>>ey;
cout<<BFS();
return 0;
}