hdu1010Tempter 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): 151147    Accepted Submission(s): 40285


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

题意:走迷宫,X表示墙,.表示通路,S起点,D终点。问是否能刚好在T秒的时候走到终点(每秒只能走一步,不能往回走),可以往上下左右四个方向走

题解:典型dfs,但要注意剪枝,看上去数据量不大,但不剪枝会T到怀疑人生。有两个地方可以剪枝,第一个是从当前点走到终点的最短距离(曼哈顿距离)比剩余时间大,那么就不可能到达终点了。第二个是剩余时间减去当前点到终点的最短距离是奇数的时候,不可能到达终点。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m,t;
 4 char s[10][10];
 5 int dirx[4]= {1,-1,0,0};
 6 int diry[4]= {0,0,-1,1};
 7 int sx,sy,ex,ey;
 8 bool dfs(int x,int y,int time)//t表示剩余多少时间
 9 {
10     if(time==0)
11     {
12         if(x==ex&&y==ey)return true;
13         return false;
14     }
15     //剩余时间连理想最短路径都不够走
16     if(time<(abs(ex-x)+abs(ey-y))||(time-abs(ex-x)-abs(ey-y))%2==1)
17         return false;
18     for(int i=0; i<4; i++)
19     {
20         int xx=x+dirx[i];
21         int yy=y+diry[i];
22         if(xx<0||yy<0||xx>=n||yy>=m||s[xx][yy]=='X')continue;
23         s[xx][yy]='X';
24         if(dfs(xx,yy,time-1))return true;
25         s[xx][yy]='.';
26         
27     }
28     return false;
29     
30 }
31 int main()
32 {
33     while(~scanf("%d%d%d",&n,&m,&t),n+m+t)
34     {
35         memset(s,'',sizeof(s));
36         for(int i=0; i<n; i++)
37         {
38             getchar();
39             for(int j=0; j<m; j++)
40             {
41                 scanf(" %c",&s[i][j]);
42                 if(s[i][j]=='S')
43                 {
44                     sx=i;
45                     sy=j;
46                     s[i][j]='.';
47                 }
48                 if(s[i][j]=='D')
49                 {
50                     ex=i;
51                     ey=j;
52                     s[i][j]='.';
53                 }
54             }
55         }
56 
57         s[sx][sy]='X';
58         if(dfs(sx,sy,t))printf("YES
");
59         else printf("NO
");
60 
61     }
62     return 0;
63 }
原文地址:https://www.cnblogs.com/fqfzs/p/9935360.html