HDU_1010——小狗走迷宫DFS

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 <math.h>

 3 char map[10][10];

 4 int N,M,S_i,S_j,D_i,D_j,Flag;

 5 void found(int,int,int);

 6 

 7 int main()

 8 {

 9    int i,j,T;

10    while(scanf("%d%d%d",&N,&M,&T)!=EOF)

11       {

12          Flag=0;

13          if(N==0 && M==0 && T==0)   break;

14          for(i=0;i<N;i++)

15             scanf("%s",map[i]);

16          for(i=0;i<N;i++)

17             for(j=0;j<M;j++)

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

19                   {S_i=i;S_j=j;}

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

21                   {D_i=i;D_j=j;}

22          map[D_i][D_j]='.';

23          if((int)fabs((double)((S_i+S_j)%2-(D_i+D_j)%2)) != T%2)   //奇偶性剪枝 

24             {

25                puts("NO");

26                continue; 

27             }                            //0->1或1->0 必然是奇数步 

28          found(S_i,S_j,T);

29          printf("%s\n",Flag?"YES":"NO");                                      //0->0或1->1 必然是偶数步

30       }                                              

31 }

32 void found(int x,int y,int T)

33 { 

34    map[x][y]='*';

35    if(T==0 && x==D_i && y==D_j)

36       {

37          Flag=1;

38          /*

39          printf("\nshow:\n");

40          for(int i=0;i<N;i++)

41             printf("%s\n",map[i]);

42          */

43       }

44    if(T>0)

45       {

46       if(x+1<N&&map[x+1][y]=='.')  found(x+1,y,T-1);//这里只能 

47       if(y+1<M&&map[x][y+1]=='.')  found(x,y+1,T-1);

48       if(x-1>=0&&map[x-1][y]=='.')  found(x-1,y,T-1);

49       if(y-1>=0&&map[x][y-1]=='.')  found(x,y-1,T-1);

50       }

51       map[x][y]='.'; 

52 }

 

你可能感兴趣的:(HDU)