zoukankan      html  css  js  c++  java
  • hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)

    Tempter of the Bone

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


    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   |   We have carefully selected several similar problems for you:  1241 1242 1072 1312 1026 
     


      这道题是一道搜索基本题,做法是DFS+剪枝。要注意剪枝要用到奇偶剪枝,否则提交会超时。
      虽然是道基本题,但是也做了近2个小时,主要时间都花在了剪枝的处理上。通过这道题学到了奇偶剪枝,不知道奇偶剪枝的可以猛戳后面的链接:
    题意:
      输入一个n*m的矩阵以及时间T(0<n,m<7,0 < T < 50)。
      矩阵中有'X':墙,不能走;'.':通路,可以走;'S':开始点。'D':门,结束点。
      要求从开始点开始走,每秒一步,走过的路不能再走,走到D的时候正好花费时间T。
      输出“YES”或“NO”表示能否在时间T的时候走到D。
    代码:
     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 char maze[8][8];
     5 int isw[8][8];
     6 int dx[4]={0,1,0,-1};
     7 int dy[4]={1,0,-1,0};
     8 int N,M,T;
     9 int curx,cury,endx,endy;
    10 int abs(int n)
    11 {
    12     return n>=0?n:-n;
    13 }
    14 int dfs(int curx,int cury,int curt) //判断从当前位置(curx,cury)能否用刚好curt时间到达结束点
    15 {
    16     if(curt==0){    //当时间耗尽的时候,判断是否到达了结束点
    17         if(curx==endx && cury==endy)
    18             return 1;
    19         else
    20             return 0;
    21     }
    22     //剪枝 1 :奇偶剪枝
    23     int m = abs(curx-endx) + abs(cury-endy);    //理想情况下,开始点到终点的最小步数
    24     int t = curt;   //要求走t步正好走到终点
    25     //当 t<m 时,一定不能到达
    26     //当 t>=m 时,要用t步从开始点正好走到终点,分两部分。
    27     //  一部分为最小步数 m ,另一部分是为了凑够t步而余外多走的几步,设为 a。
    28     //  而走出去就一定要走回来,所以走出去的步数和回来的步数一定是相等的,为b步。
    29     //  a=2b,所以多走的a步一定是偶数。
    30     //  这里的奇偶剪枝就是判断 a 是否为偶数。如果不是偶数,一定不能到达。
    31     if( t<m || (t-m)&1 )
    32         return 0;
    33     for(int i=0;i<4;i++){   //剪枝 2
    34         if( 1<=curx+dx[i] && curx+dx[i]<=N && 1<=cury+dy[i] && cury+dy[i]<=M //如果没有越界
    35             && !isw[curx+dx[i]][cury+dy[i]]  //如果下一步没有走过
    36             && maze[curx+dx[i]][cury+dy[i]]!='X' ){   //如果下一步不是墙
    37                 isw[curx+dx[i]][cury+dy[i]]=true;
    38                 if(dfs(curx+dx[i],cury+dy[i],curt-1))   //如果下一步这样走可以生存,则返回1
    39                     return 1;
    40                 isw[curx+dx[i]][cury+dy[i]]=false;
    41         }
    42     }
    43     return 0;
    44 }
    45 
    46 int main()
    47 {
    48     while(cin>>N>>M>>T){
    49         if(N==0 && M==0 && T==0) break;
    50         memset(isw,0,sizeof(isw));  //将isw[][]数组初始化为false,表示还没有走过。
    51         for(int i=1;i<=N;i++)
    52             for(int j=1;j<=M;j++){
    53                 cin>>maze[i][j];
    54                 if(maze[i][j]=='S'){    //记录开始点的位置
    55                     curx=i;
    56                     cury=j;
    57                 }
    58                 else if(maze[i][j]=='D'){   //记录终点的位置
    59                     endx=i;
    60                     endy=j;
    61                 }
    62             }
    63         isw[curx][cury]=true;   //开始位置赋true
    64         if(dfs(curx,cury,T))    //如果能够生存,输出YES,否则输出NO
    65             cout<<"YES"<<endl;
    66         else
    67             cout<<"NO"<<endl;
    68     }
    69     return 0;
    70 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    8088汇编跳转和PSW状态字寄存器
    Delphi的函数指针
    服务器系统及软件常见漏洞
    TGraphiControl响应WM_MOUSEMOVE的过程(以TPaintBox为例)good
    两个奇怪的取地址符号
    把x指针指向的4个字节次序颠倒过来
    DELPHI中的消息处理机制(三种消息处理方法的比较,如何截断消息)
    探索C++的底层机制
    setprecision、fixed、showpoint的用法总结(经典!!超经典!!)
    段寄存器和8种地址寻址方式
  • 原文地址:https://www.cnblogs.com/yym2013/p/3496352.html
Copyright © 2011-2022 走看看