• HDU 1010 Tempter of the Bone(DFS)


    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

    The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
     
    Input
    The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

    'X': a block of wall, which the doggie cannot enter;
    'S': the start point of the doggie;
    'D': the Door; or
    '.': an empty block.

    The input is terminated with three 0's. This test case is not to be processed.
     
    Output
    For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
     
    Sample Input
    4 4 5
    S.X.
    ..X.
    ..XD
    ....
    3 4 5
    S.X.
    ..X.
    ...D
    0 0 0
    Sample Output
    NO YES
    Author
    ZHANG, Zheng
     
    Source
    //一道搜索题目,看起来很简单,不剪枝果断TLM!囧、、、

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    int v[9][9];
    int dx,dy,t;
    int flag;
    void dfs(int x,int y,int c)
    {
       int temp;
        if(flag)
         return ;
       temp=abs(1.0*dy-y)+abs(1.0*dx-x);
       if(temp>t-c)      //剪枝
          return ;
        temp=t-c-temp;
       if(temp%2||temp<0)//重要的奇偶剪枝
          return ;
       if(c==t)       //理论上这里应该(x==dx&&y==dy&&c==t)的,不知道为什么还是过了
         {flag=1;return ;}
       if(c>t)
         return ;
        v[x][y]=0;
        if(v[x+1][y])
          dfs(x+1,y,c+1);
        if(v[x-1][y])
          dfs(x-1,y,c+1);
        if(v[x][y+1])
          dfs(x,y+1,c+1);
        if(v[x][y-1])
          dfs(x,y-1,c+1);
        v[x][y]=1;
    }
    int main()
    {
      freopen("in.txt","r",stdin);
      int i,j,n,m,sx,sy;
      char c;
      while(scanf("%d%d%d",&n,&m,&t),n||m||t)
      {   memset(v,0,sizeof(v));
            getchar();
        for(i=1;i<=n;i++)
           { for(j=1;j<=m;j++)
              {
                  c=getchar();
                  switch(c)
                  {
                     case '.':v[i][j]=1;break;
                     case 'S':v[i][j]=1;sx=i;sy=j;break;
                     case 'D':v[i][j]=1;dx=i;dy=j;break;
                  }
              }
             getchar();
           }
         flag=0;
         dfs(sx,sy,0);
        if(flag)
          printf("YES\n");
         else
          printf("NO\n");
      }
      return 0;
    }

    //据说这道搜索题具有里程碑意义,看到自己的代码500多Ms,不爽,看了下别人的,居然有大牛0Ms,

    于是简单的修改了下,就150多Ms了,如果来个BFS估计还会更快,体验到了搜索剪枝的强大了!
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    int
     v[9][9];
    int
     dx,dy,t;
    int
     flag;
    void
     dfs(int x,int y,int c)
    {

       int
     temp;
       if
    (flag)
         return
     ;
      temp=t-c-abs(1.0*dy-y)-abs(1.0*dx-x);
        if
    (temp%2||temp<0)
           return
     ;
        if
    (x==dx&&y==dy&&c==t)
          {
    flag=1;return ;}
        v[x][y]=0;
        if
    (v[x+1][y])
          dfs(x+1,y,c+1);
        if
    (v[x-1][y])
          dfs(x-1,y,c+1);
        if
    (v[x][y+1])
          dfs(x,y+1,c+1);
        if
    (v[x][y-1])
          dfs(x,y-1,c+1);
        v[x][y]=1;
    }

    int
     main()
    {
      int i,j,n,m,sx,sy,nt;
      char
     c;
      while
    (scanf("%d%d%d",&n,&m,&t),n||m||t)
      {
       memset(v,0,sizeof(v));
            getchar();nt=0;
        for
    (i=1;i<=n;i++)
           {
     for(j=1;j<=m;j++)
              {

                  c=getchar();
                  switch
    (c)
                  {

                     case
     '.':v[i][j]=1;nt++;break;
                     case
     'S':v[i][j]=1;sx=i;sy=j;break;
                     case
     'D':v[i][j]=1;dx=i;dy=j;nt++;break;
                  }
              }

             getchar();
           }


        if
    (nt<t||abs(1.0*dy-sy)+abs(1.0*dx-sx)>t)//就加这点,就快了几百Ms,唉,问题看来就在这,
            printf("NO\n");     //这里的nt用BFS遍历求最短的话,会更快,大牛0Ms估计就这么来的
         else

         {
       flag=0;
             dfs(sx,sy,0);
           if
    (flag)
             printf("YES\n");
           else

             printf("NO\n");
         }
      }

      return
     0;
    }

        

                                                           --------江财小子

  • 相关阅读:
    小程序隐藏或自定义 scroll-view滚动条
    小程序携带参数(单个或多个)跳转页面(实例)
    小程序接收from表单数据(实例)
    js返回上一页
    项目部署到线上后台进不去
    微信小程序取消button边框线
    阿里iconfont图库官网网址
    php 发送邮件(实例)
    PHP 数组序列化,转为字符串
    面向对象的设计原则
  • 原文地址:https://www.cnblogs.com/372465774y/p/2421669.html
Copyright © 2020-2023  润新知