HDU 1010 Temper of the bone(深搜+剪枝)

              Tempter of the Bone

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<iostream>

 2 #include<stdio.h>

 3 #include<stdlib.h>

 4 using namespace std;

 5 

 6 int a,b;

 7 bool flag;

 8 int start_x,start_y,end_x,end_y;

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

10 char map[10][10];

11 

12 void DFS(int x,int y,int t)

13 {

14     int i;

15     if(flag==true) return;

16     if(t<abs(end_x-x)+abs(end_y-y)||(t-abs(end_x-x)+abs(end_y-y))%2) return;

17     else if(t==0)

18     {

19         if(x==end_x&&y==end_y)

20         {

21             flag=true;

22             return;

23         }

24         else return;

25     }

26     else

27     {

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

29         {

30             int nx=x+d[i][0],ny=y+d[i][1];

31             if(nx>=0&&nx<a&&ny>=0&&ny<b&&(map[nx][ny]=='.'||map[nx][ny]=='D'))

32             {

33                 map[nx][ny]='X';

34                 DFS(nx,ny,t-1);

35                 map[nx][ny]='.';

36             }

37         }

38         return;

39     }

40 }

41 

42 int main()

43 {

44     int i,j,t;

45     while(cin>>a>>b>>t&&(a||b||t))

46     {

47         for(i=0;i<a;i++)

48         cin>>map[i];

49         for(i=0;i<a;i++)

50         for(j=0;j<b;j++)

51         {

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

53             {

54                 start_x=i;

55                 start_y=j;    

56             }

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

58             {

59                 end_x=i;

60                 end_y=j;

61             }

62             else continue;

63         }

64         flag=false;

65         DFS(start_x,start_y,t);    

66         if(flag==false)cout<<"NO"<<endl;

67         else cout<<"YES"<<endl;

68     }

69     return 0;

70 }

 

你可能感兴趣的:(HDU)