• HDU 1010 Tempter of the Bone


    Tempter of the Bone

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 55194    Accepted Submission(s): 14895

    Problem Description
    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
     
    Recommend
    JGShining
     
    思路:DFS + 剪枝
     
    代码:

    #include <iostream>

    #include <cstdio>

    #include <cstring>

    #include <cmath>

    using namespace std;

    int hash[8][8];

    char map[8][8];

    int move[4][2] = {1,0,-1,0,0,1,0,-1};

    int n,m,t; int sx,sy,dx,dy;

    int flag;

    void DFS(int ax,int ay,int step)

    {    

    if(ax == dx && ay == dy)    

    {        

    if(step == t)           

      flag = 1;        

    return ;    

    }  

       if(step >= t)      

    return ;   

      if(flag)      

    return ;    

    for(int i = 0;i < 4;i ++)  

       {        

    int x = ax + move[i][0];int y  = ay + move[i][1];      

       if(map[x][y] != 'X' && hash[x][y] == 0 && x >= 1 && x <= n         && y <= m && y >= 1)        

    {            

    hash[x][y] = 1;            

    DFS(x,y,step + 1);            

    hash[x][y] = 0;            

    if(flag)               

    return ;        

    }    

    }

    }

    int main()

    {    

    while(scanf("%d%d%d",&n,&m,&t),n != 0 && m != 0 && t != 0)    

    {        

    memset(hash,0,sizeof(hash));        

    flag = 0;        

    for(int i = 1;i <= n;i ++)       

         for(int j = 1;j <= m;j ++)         

       {             

       scanf(" %c",&map[i][j]);      

              if(map[i][j] == 'S')               

    {                   

      sx = i;sy = j;                

         hash[i][j] = 1;           

         }             

       if(map[i][j] == 'D')          

          {                  

       dx = i;dy = j;           

         }            }     

       if(abs(dx - sx) + abs(dy - sy) > t || (sx + sy + dx + dy + t) % 2 == 1)     

       {             printf("NO ");        

         continue;        }      

      else      

      {             DFS(sx,sy,0);   

      

            if(flag == 0)            

        printf("NO ");           

      else               

    printf("YES ");    

        }    

    }    

    return 0;

    }

      

  • 相关阅读:
    显示网页加载时间
    ASP.NET MVC使用jQuery来POST数据至数据库中
    jQuery的prop和attr方法之间区别
    jQuery动态产生的铵钮怎样实现事件处理
    上传Text文档并转换为PDF
    学习jQuery的on事件
    使用ViewBag传送数据从控制器至视图
    ASP.NET MVC读取XML并使用ViewData显示
    ASP.NET MVC铵钮Click后下载文件
    如何把Json格式字符写进text文件中
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3286449.html
Copyright © 2020-2023  润新知