请有相似经历的同学说说
下面是代码 其实没什么好看的
#include<stdio.h>
#include<string.h>
struct node
{
int x,y,t;
};
node queue[10000001];
int L,sx,sy,nx,ny;
int map[1001][1001];
int hash[1001][1001];
int go[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
void init()
{
int i,j;
char temp,s,ss[10];
memset(map,-1,sizeof(map));
memset(hash,0,sizeof(hash));
scanf("%d",&L);
getchar;
for(i=1;i<=L;i++)
{
gets(ss);
for(j=1;j<=L;j++)
{
scanf("%c",&temp);
if(temp=='0') map[i][j]=0;
if(temp=='1') map[i][j]=1;
}
}
scanf("%d%d",&sx,&sy);
scanf("%d%d",&nx,&ny);
}
void bfs()
{
long int f=0,r=0,ans=-1,i,j;
node now,next;
now.x=sx;
now.y=sy;
now.t=0;
queue[++r]=now;
hash[now.x][now.y]=1;
while(f<r)
{
now=queue[++f];
if(now.x==nx&&now.y==ny)
{
ans=now.t;
break;
}
for(i=0;i<4;i++)
{
next.x=now.x+go[i][0];
next.y=now.y+go[i][1];
next.t=now.t+1;
if(map[next.x][next.y]!=0) continue;
if(hash[next.x][next.y]==1) continue;
queue[++r]=next;
hash[next.x][next.y]=1;
}
}
if(ans!=-1)
printf("%d\n",now.t);
}
int main()
{
init();
bfs();
return 0;
}