HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

angel_kitty 2017-07-21 19:14:00 浏览641

# Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 125945    Accepted Submission(s): 33969

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

``` 1 inline int DFS(int x,int y,int T)
2 {
3     if(mp[x][y]!='.'&&mp[x][y]!='S')//碰到X即为边界返回
4         return 0;
5     if(T==1)//剩一步时即可判断是否为出口,找到返回1
6     {
7         if(mp[x-1][y]=='D')
8             return 1;
9         if(mp[x+1][y]=='D')
10             return 1;
11         if(mp[x][y-1]=='D')
12             return 1;
13         if(mp[x][y+1]=='D')
14             return 1;
15         return 0;
16     }
17     else
18     {
19         mp[x][y]='X';//标记走过
20         if(mp[x-1][y]=='.'&&DFS(x-1,y,T-1))
21             return 1;
22         if(mp[x+1][y]=='.'&&DFS(x+1,y,T-1))
23             return 1;
24         if(mp[x][y-1]=='.'&&DFS(x,y-1,T-1))
25             return 1;
26         if(mp[x][y+1]=='.'&&DFS(x,y+1,T-1))
27             return 1;
28         mp[x][y]='.';//还原走过
29         return 0;
30     }
31     return 0;
32 }```

```S... .... .... ...D```

```S.XX X.XX ...X ...D```

``` 1 int main()
2 {
3     int sx,sy,dx,dy;
4     int N,M,T;
5     while(scanf("%d%d%d",&N,&M,&T)&&N&&M&&T)
6     {
7         getchar();
8         memset(mp,'X',sizeof(mp));//把周围边界全部变成X
9         for(int i=1;i<=N;i++)//从1开始读,留出边界位置
10         {
11             for(int j=1;j<=M;j++)
12             {
13                 scanf("%c",&mp[i][j]);
14                 if(mp[i][j]=='S')
15                 {
16                     sx=i;
17                     sy=j;
18                 }
19                 else if(mp[i][j]=='D')
20                 {
21                     dx=i;
22                     dy=j;
23                 }
24             }
25             getchar();
26         }
27         if((abs(sx-dx)+abs(sy-dy)-T)&1)//奇偶剪枝,对1用按位与运算求奇偶
28             printf("NO\n");
29         else if(DFS(sx,sy,T)==1)
30             printf("YES\n");
31         else printf("NO\n");
32     }
33     return 0;
34 }```

``` 1 #include <bits/stdc++.h>
2 using namespace std;
3 char mp[15][15];
4 inline int DFS(int x,int y,int T)
5 {
6     if(mp[x][y]!='.'&&mp[x][y]!='S')//碰到X即为边界返回
7         return 0;
8     if(T==1)//剩一步时即可判断是否为出口,找到返回1
9     {
10         if(mp[x-1][y]=='D')
11             return 1;
12         if(mp[x+1][y]=='D')
13             return 1;
14         if(mp[x][y-1]=='D')
15             return 1;
16         if(mp[x][y+1]=='D')
17             return 1;
18         return 0;
19     }
20     else
21     {
22         mp[x][y]='X';//标记走过
23         if(mp[x-1][y]=='.'&&DFS(x-1,y,T-1))
24             return 1;
25         if(mp[x+1][y]=='.'&&DFS(x+1,y,T-1))
26             return 1;
27         if(mp[x][y-1]=='.'&&DFS(x,y-1,T-1))
28             return 1;
29         if(mp[x][y+1]=='.'&&DFS(x,y+1,T-1))
30             return 1;
31         mp[x][y]='.';//还原走过
32         return 0;
33     }
34     return 0;
35 }
36 int main()
37 {
38     int sx,sy,dx,dy;
39     int N,M,T;
40     while(scanf("%d%d%d",&N,&M,&T)&&N&&M&&T)
41     {
42         getchar();
43         memset(mp,'X',sizeof(mp));//把周围边界全部变成X
44         for(int i=1;i<=N;i++)//从1开始读,留出边界位置
45         {
46             for(int j=1;j<=M;j++)
47             {
48                 scanf("%c",&mp[i][j]);
49                 if(mp[i][j]=='S')
50                 {
51                     sx=i;
52                     sy=j;
53                 }
54                 else if(mp[i][j]=='D')
55                 {
56                     dx=i;
57                     dy=j;
58                 }
59             }
60             getchar();
61         }
62         if((abs(sx-dx)+abs(sy-dy)-T)&1)//奇偶剪枝,对1用按位与运算求奇偶
63             printf("NO\n");
64         else if(DFS(sx,sy,T)==1)
65             printf("YES\n");
66         else printf("NO\n");
67     }
68     return 0;
69 }```

angel_kitty
+ 关注