#include<bits/stdc++.h>
using namespace std;
int b[
1010][
1010],l[
1010],c[
5]={
0,-
1,
1,
0,
0},d[
5]={
0,
0,
0,-
1,
1},n,m,dx,dy,tx,ty,i,j,xx,yy;
char p[
1010];
struct
JF{
int x,y,step;
}YY[10000001];
int main()
{
cin>>
n;
for(i=
0;i<=n;i++
)
{
cin.getline(p,1008);
for(j=
0;j<=n;j++
)
{
if(p[j-
1]==
'0')
b[i][j]=
0;
if(p[j-
1]==
'1')
b[i][j]=
1;
}
}
cin>>dx>>dy>>tx>>
ty;
int head=
0,tail=
1;
b[dx][dy]=
1;
YY[1].x=
dx;
YY[1].y=
dy;
while(head<
tail)
{
head++
;
for(
int i=
1;i<=
4;i++
)
{
xx=YY[head].x+
c[i];
yy=YY[head].y+
d[i];
if(xx>
0&&xx<=n&&yy>
0&&yy<=n&&b[xx][yy]!=
1)
{
tail++
;
b[xx][yy]=
1;
YY[tail].x=
xx;
YY[tail].y=
yy;
YY[tail].step=YY[head].step+
1;
if(xx==tx&&yy==
ty)
{
cout<<
YY[tail].step;
return 0;
}
}
}
}
}
这道题目还是写了很久的,差不多写了一天,但是对于广搜的定义和理解也从这道题开始深入了
转载于:https://www.cnblogs.com/5t2y0/p/9458622.html
相关资源:营救公主 编程C