Tempter of the Bone HDU - 1010

题目

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

1
2
3
4
5
6
7
8
9
10
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

1
2
NO
YES

思路

这道题原先每看清题意,以为是在规定的时间内到门那里就行了,一直用BFS求最短路径,WA到自闭~
实际上是要准时在门那里出现才有一线生机。
用DFS写就要考虑时间复杂度了。到网上查了别人的题解,用奇偶减枝来优化代码。
剪枝是关键,奇偶剪枝.
奇偶剪枝原理:

要理解奇偶剪枝,先了解一下曼哈顿距离,从一个点到达另外一个点的最短路径长度(时间)可以根据两点坐标求出,
路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数!举个例子,就像两个偶数的差差是偶数,两个个数的差也是偶数.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
const int MAXSIZE=7;
string str[MAXSIZE];
int N,M,K;
int dx[]={1,0,0,-1};
int dy[]={0,1,-1,0};
bool flag=false;
int ex,ey;
int visit[MAXSIZE][MAXSIZE]={0};
void DFS(int x,int y,int cnt){
if(flag==true) return;
if(ex==x&&ey==y)
if(cnt==K){
flag=true;
return;
}
int temp=(K-cnt)-(fabs(x-ex)+fabs(y-ey)); //奇偶减枝判断
if(temp<0||temp%2!=0) return;
for(int i=0;i<4;i++){
int tx=x+dx[i];
int ty=y+dy[i];
if(tx>=0&&tx<N&&ty>=0&&ty<M&&visit[tx][ty]==0&&str[tx][ty]!='X'){
visit[tx][ty]=1;
DFS(tx,ty,cnt+1);
visit[tx][ty]=0;
}
}
}
int main(){

while(cin>>N>>M>>K){
if(N==0&&M==0&&K==0) break;
int startX=0;
int startY=0;
flag=false;
int sum=0;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++){
cin>>str[i][j];
visit[i][j]=0;
if(str[i][j]=='X') sum++;
if(str[i][j]=='S'){
startX=i;
startY=j;
str[i][j]='X';
}
if(str[i][j]=='D'){
ex=i;
ey=j;
}
}
if(N*M-sum<=K){
cout<<"NO"<<endl;
continue;
}
DFS(startX,startY,0);
if(flag)cout<<"YES";
else cout<<"NO";
cout<<endl;

}
return 0;
}
-------------本文结束感谢您的阅读-------------
0%