HDU 1010 Tempter of the Bone

关于DFS如何学好,先援引大牛的几点建议:

(1)先理解搜索的原理,区别好bfs和dfs的优先搜索方向以及应用,例如bfs一般用来求最优解如最短路径.
(2)掌握好所用的数据结构,特别是队列.
(3)牢记dfs和bfs的常用模式,它是基本固定的,考试时信手拈来!
(4)注意状态的压缩+判重,学会用简单的hash函数.
(5)多练一些bfs和dfs经典题目,如(数码问题),  (八皇后问题),  (骑士巡游)  (马拦过河卒).....注意跟着提解的思路,理解是关键!
(6)重视一提多解,拓宽解提思路!如noip2001的(采药问题)可用01背包,也可用搜索,类似的还很多.

 

 

Tempter of the Bone 
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 116   Accepted Submission(s) : 37
Font: Times New Roman | Verdana | Georgia 
Font Size: ← →
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
ZJCPC2004 
--------------------------------------------------------------------------------

 

/*-------------源代码--------------------*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int n,m,t,sx,sy,ex,ey,f;
int maz[10][10],des[10][10];
void DFS( int y,int x,int num )
{
     if( des[y][x] ||f|| maz[y][x] == 'X'||num > ( n * m ) )
         return ;
     if( maz[y][x] == 'D'&&num == t )
     {
         f = 1;//看是否找到
         return ;
     }
     if( abs( t- num ) % 2 != abs(abs( y - ey ) + abs( x - ex )) % 2 )
        return ; /*奇偶剪枝:任意时刻,abs(ex-x)+abs(ey-y)都表示当前点p
        和逃生点ep之间的二维距离,可以证明,这时当前点p到逃生点ep之间的
        最短距离!记此最短距离长度为s。如果,此最短距离上有一些障碍物不
        能走,那么移动会偏移最短距离s,但是不管偏移几个点,偏移的
        距离都是最短距离s加上一个偶数距离,如果剩余时间减去最短路径s,
        得到一个奇数,那么显然是无法在t时刻准时到达逃生门ep的*/
     des[y][x] = 1;
     DFS( y - 1,x,num+1 );
     DFS( y,x - 1, num+1 );
     DFS( y + 1, x, num+1 );
     DFS( y,x + 1, num + 1 );
     des[y][x] = 0;
 }
int main( )
{
    while( scanf( "%d%d%d%*c",&n,&m,&t ),n||m||t )
    {
          int ans = 0;
           memset( des,0,sizeof( des ) );
           for( int i = 0; i < 10; ++i )
                for( int j = 0; j < 10; ++j )
                     maz[i][j] = 'X';
           for( int i = 1; i <= n; ++i )
           {
                for( int j = 1; j <= m; ++j )
                {
                     scanf( "%c",&maz[i][j] );
                     if( maz[i][j] == 'S' )
                         sy = i,sx = j;
                    else if( maz[i][j] == 'D' )
                         ey = i,ex = j,ans ++;
                    else if(maz[i][j]=='.')
                         ans++;//标记当前可走的点
                     }
                getchar();
           }
           f = 0;
           if( ans < t )
               printf("NO\n");
           else
           {
               DFS( sy,sx,0 );
               puts( f ? "YES":"NO" );
           }
           }
    return 0;
}



 

你可能感兴趣的:(HDU 1010 Tempter of the Bone)