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): 49637    Accepted Submission(s): 13320

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
 
 
题目大意:给你一个迷宫,从起点位置开始,问能否恰好在第T时刻到达迷宫的出口。
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 
 5 bool flag;
 6 int  si, sj;
 7 int  ei, ej;
 8 int  N, M, T;
 9 char maze[10][10];
10 int  dir[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
11 
12 void dfs(int curX, int curY, int curT)
13 {
14     if(flag)return ;       //如果找到出口,直接返回
15     if(curT > T)return ;   //如果当前花费的时间大于T,就返回
16     
17     //花费的时间加上剩余需要的时间若大于T,返回
18     if(curT + abs(ei-curX) + abs(ej-curY) > T)return ;
19 
20     //若到达出口最短距离和剩余时间的奇偶性不同,返回
21     if( (abs(ei-curX) + abs(ej-curY))%2 != abs(T-curT)%2 )return;
22 
23     if(curX == ei && curY == ej && curT == T)//找到出口
24     {
25         printf("YES\n");
26         flag = true;
27         return ;
28     }
29 
30     for(int k = 0; k < 4; k++)
31     {
32         int nextX = curX + dir[k][0];
33         int nextY = curY + dir[k][1];
34 
35         if(nextX >= 0 && nextX < N && nextY >= 0 && nextY < M)//在迷宫的范围内
36         {
37             if(maze[nextX][nextY] != 'X')
38             {
39                 maze[nextX][nextY] = 'X';  //使其不可达到
40                 dfs(nextX, nextY, curT + 1);
41                 maze[nextX][nextY] = '.';  //回溯时,恢复原来的状态
42             }
43         }
44     }
45 }
46 
47 int main()
48 {
49     while(scanf("%d %d %d", &N, &M, &T) && M+N+T)
50     {
51         int k = 0;
52         getchar();
53         flag = false;
54         memset(maze, 0, sizeof(maze));
55         for(int i = 0; i < N; i++)
56         {
57             for(int j = 0; j < M; j++)
58             {
59                 scanf("%c", &maze[i][j]);
60                 if(maze[i][j] == 'S')
61                 {
62                     si = i; sj = j; 
63                     maze[i][j] = 'X';  //将开始位置置为不可达状态
64                 }
65                 if(maze[i][j] == 'D')
66                 {
67                     ei = i; ej = j;
68                 }
69                 if(maze[i][j] != 'X')
70                     k++;
71             }
72             getchar();
73         }
74         if(k < T)            //若可走的点数小于T,则输出NO
75         {
76             printf("NO\n");
77             continue;
78         }
79 
80         dfs(si, sj, 0);
81 
82         if(!flag)
83             printf("NO\n");
84     }
85     return 0;
86 }

 

总结:一开始因为没有将开始位置置为不可达状态,导致WA了很多次。

posted @ 2013-05-25 19:46  Dreamcaihao  阅读(362)  评论(0编辑  收藏  举报