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): 82702    Accepted Submission(s): 22531

    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
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 int n , m , T ;
     4 bool vis[10][10] , flag;
     5 char map[10][10] ;
     6 int move[][2] = {{1,0} , {-1,0} , {0,1} , {0,-1}} ;
     7 int step = 0;
     8 
     9 void dfs (int sx , int sy , int step )
    10 {
    11     if (step == T + 1 && map[sx][sy] == 'D') {
    12         flag = 1 ;
    13         return ;
    14     }
    15   //  printf ("(%d,%d) %c step=%d
    "  , sx , sy , map[sx][sy] , step) ;
    16     if (map[sx][sy] == 'D') return ;
    17     if (step > T) return ;
    18     for (int i = 0 ; i < 4 ; i ++) {
    19         int x , y ;
    20         x = sx + move[i][0] ;
    21         y = sy + move[i][1] ;
    22         if (x < 0 || y < 0 || x >= n || y >= m) continue ;
    23         if (map[x][y] == 'X') continue ;
    24         if (vis[x][y]) continue ;
    25         vis[x][y] = 1 ;
    26         dfs (x , y , step + 1) ;
    27         if (flag) return ;
    28         vis[x][y] = 0 ;
    29     }
    30 }
    31 
    32 int main ()
    33 {
    34     //freopen ("a.txt" , "r" , stdin ) ;
    35     while (~ scanf ("%d%d%d" , &n , &m , &T)) {
    36         if (n == 0 &&m == 0 && T == 0) break ;
    37         for (int i = 0 ; i < n; i ++) scanf ("%s" , map[i]) ;
    38         flag = 0 ;
    39         step = 0 ;
    40         memset (vis , 0 , sizeof(vis)) ;
    41         for (int i = 0 ; i < n; i ++) {
    42             for (int j = 0 ; j < m ; j ++) {
    43                 if (map[i][j] == 'S') {
    44                     vis[i][j] = 1 ;
    45                     dfs (i , j , 1) ;
    46                 }
    47             }
    48         }
    49         if (flag) puts ("YES") ;
    50         else puts ("NO") ;
    51     }
    52     return 0 ;
    53 }
    View Code

    这道题是要奇偶剪枝的,但我没剪也过了。(虽说压线,23333333333333333)
    转载:http://www.2cto.com/kf/201307/229103.html

    奇偶剪枝:根据题目,doggie必须在第t秒到达门口。也就是需要走t-1步。设doggie开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+abs(ey-y)为偶数,则abs(ex-x)和abs(ey-y)奇偶性相同,所以需要走偶数步; 
    当abs(ex-x)+abs(ey-y)为奇数时,则abs(ex-x)和abs(ey-y)奇偶性不同,到达目标位置就需要走奇数步。先判断奇偶性再搜索可以节省很多时间,不然的话容易超时。t-sum为到达目标位置还需要多少步。因为题目要求doggie必须在第t秒到达门的位置,所以(t-step)和abs(ex-x)+abs(ey-y)的奇偶性必然相同。因此temp=(t-step)-abs(ex-x)+abs(ey-y)必然为偶数。
  • 相关阅读:
    Web Components 是什么
    HAL_RTC_MspInit Msp指代什么?
    C 枚举 相同的值
    EntityFramework Core并发深挖详解,一纸长文,你准备好看完了吗?
    ASP.NET Core MVC之ViewComponents(视图组件)知多少?
    .NET Core 1.1日期解析无APi、SQL Server数据转换JSON
    SQL Server-字字珠玑,一纸详文,完全理解SERIALIZABLE最高隔离级别(基础系列收尾篇)
    SQL Server-聚焦NOLOCK、UPDLOCK、HOLDLOCK、READPAST你弄懂多少?(三十四)
    SQL Server-聚焦深入理解死锁以及避免死锁建议(三十三)
    ASP.NET Core MVC上传、导入、导出知多少
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4451392.html
Copyright © 2011-2022 走看看