[HDU]1728逃离迷宫

http://acm.hdu.edu.cn/showproblem.php?pid=1728

*代表墙, .代表路。问是否能从给定的一点走另一点。采取广度优先搜索。广度搜索从起点开始,依次搜索可以气扩展的每一个结点,当一层结点全部被搜索完后,再依次搜索第一个可扩展结点可以扩展的所有结点,直到找到或结点全被访问。

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
int hash[110][110],k;
int step[4][2]={{0,1},{1,0},{0,-1},{-1,0}};  
struct coord
{
    int x,y;
}qd,zd,c1,c2,que[110000];
int bfs()
{
    int head,tail,i;
    que[1]=qd;
    head=0;
    tail=1;
    if (zd.x==qd.x&&zd.y==qd.y) return 1;    //起点坐标和终点坐标一样 ,成功 
    if (hash[zd.x][zd.y]==-1) return 0;      //若终点坐标是*,即走不动,失败, 
    while(head<tail)                         //队列不为空 
    {
         c1=que[++head];                   
         if (hash[c1.x][c1.y]-1>=k) return 0;   //因为有限制转弯数,而第一次方向任意.        
                for(i=0;i<4;i++)               //分比尝试4种方向 
                {
                     c2=c1;
                     do
                     {
                          c2.x+=step[i][0];
                          c2.y+=step[i][1];
                          if (c2.x==zd.x && c2.y==zd.y) return 1;
                          if (!hash[c2.x][c2.y])        
                          {
                              que[++tail]=c2;
                              hash[c2.x][c2.y]=hash[c1.x][c1.y]+1;
                              
                          }
                     }while(hash[c2.x][c2.y]!=-1);
                }
    }
    return 0;
}
int main()
{
     int t,n,m,i,j;
     char s[110][110];
     scanf("%d",&t);
     while(t--)
     {
           scanf("%d%d",&n,&m);
           for(i=0;i<=n+1;i++)            //虽然只有n行,m列,但是需要在外围弄一层"墙" 
            for(j=0;j<=m+1;j++)
             hash[i][j]=-1;               //-1代表墙,0代表路 
           for(i=1;i<=n;i++)
           {
                 scanf("%s",s[i]);
                 for(j=1;j<=m;j++)
                 if(s[i][j-1]=='.') 
                 hash[i][j]=0;
                 else hash[i][j]=-1;
           }        
           scanf("%d%d%d%d%d",&k,&qd.y,&qd.x,&zd.y,&zd.x);   //是先输入列,再输入行 
           if(bfs())  printf("yes\n");
           else  printf("no\n");
     }
}

 

posted @ 2013-07-31 15:43  天际。  阅读(397)  评论(0编辑  收藏  举报