BFS迷宫
题目:http://www.我利用队列实现,但是不管怎么输入输出都是No Answer!请各位高手帮我看看
#include <stdio.h>
#include <stdlib.h>
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int ans=0;
int judge(int x,int y,int n,int m,int map[100][100])
{
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]==0)
return 1;
return 0;
}
void dfs(int map[100][100],int n,int m,int startx,int starty,int endx,int endy)
{
int queue[1000][3];
int head=0,tail=1,x,y,xx,yy,i;
queue[0][0]=startx;queue[0][1]=starty;
queue[0][3]=0;
while(head<tail)
{
head++;
x=queue[head][0];
y=queue[head][1];
ans=queue[head][3];
for(i=0;i<4;i++)
{
xx=x+dx[i];
yy=y+dy[i];
if(judge(xx,yy,n,m,map)==1)
{
tail++;
queue[tail][0]=xx;
queue[tail][1]=yy;
queue[tail][3]=ans++;
}
}
if(xx==endx&&yy==endy)
printf("%d\n",ans);
}
printf("No Answer\n");
}
int main()
{
int map[100][100],n,m,startx,starty,endx,endy;
int i,j;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&map[i][j]);
scanf("%d %d",&startx,&starty);
scanf("%d %d",&endx,&endy);
startx--;starty--;
endx--;endy--;
dfs(map,n,m,startx,starty,endx,endy);
system("pause");
return 0;
}