您的位置:首页 > 其它

HDU 1010 Tempter of the Bone(DFS,奇偶剪枝)

2016-04-26 23:30 489 查看


Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 101688 Accepted Submission(s): 27539



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

ZJCPC2004

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

题意:根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,

问:在指定的时间,是否能到达'门'的位置.

注意:路不可以重复经过,时间也要刚好是 t ,不能少.

思路:还是DFS,不能用BFS,因为BFS求的是最短路径,而此题的路径不一定最短.

剪枝是关键,奇偶剪枝.

奇偶剪枝原理:

要理解奇偶剪枝,先了解一下曼哈顿距离,从一个点到达另外一个点的最短路径长度(时间)可以根据两点坐标求出,

路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数!举个例子,就像两个偶数的差差是偶数,两个个数的差也是偶数.

本题还有一个剪枝:n*m-wall与t的关系,wall为'X'的数量,解释一下,n*m为区域总数,

所以m*n-wall<=t 一定不到到达终点,注意,少时等号在杭电上运行时间为546MS,而加上等号运行时间才为78MS!

eg:

3 3 4

SXX

.XX

X.D

上面的例子满足m*n-wall=t,确实不能到达,但不能找到合理的解释......



AC代码:

#include <iostream>
#include <cstring>
using namespace std;
char a[10][10];
int n,m,t;
int sx,sy,ex,ey;
bool flag;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int abs(int x)
{
return x<0?-x:x;
}
void DFS(int x,int y,int time)//当前位置坐标(x,y),到目前位置消耗时间
{
if(x<0||x>=n||y<0||y>=m)
return;
if(x==ex&&y==ey&&time==t)
{
flag =true;
return ;
}
if(flag)
return;
int temp=(t-time)-(abs(x-ex)+abs(y-ey));
if(temp<0||temp&1)
return;
for(int i=0;i<4;i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(a[xx][yy]!='X')
{
a[xx][yy]='X';
DFS(xx,yy,time+1);
a[xx][yy]='.';
if(flag)
return ;
}
}
}
int main()
{
while(cin>>n>>m>>t)
{
if(n==0&&m==0)
break;
int wall=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
cin>>a[i][j];
if(a[i][j]=='S')
sx=i,sy=j;
else if(a[i][j]=='D')
ex=i,ey=j;
else if(a[i][j]=='X')
wall++;
}
if(n*m-wall<=t)
{
cout<<"NO"<<endl;
continue;
}
flag=false;
a[sx][sy]='X';
DFS(sx,sy,0);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: