Tempter of the Bone
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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.
'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
Source
ZJCPC2004
解题思路:
在一个坐标内,给定起点和终点,问能否恰好在T时间内到达终点,要求不能走已经走过的路。
明显深搜,大概是好久不做题的缘故了吧,以前做搜索的时候,基本上不怎么担心,形成了一种随机化的惯性思维,
这么久不碰,突然又开始做这样的题目竟然有点有心无力的感觉。本来以为很简单,第一次,竟被一个莫名的输入失误卡死,
两个小时不出结果。再后来,超时,于是,剪枝,起初剪枝+奇偶性剪枝,
所谓奇偶性剪枝就是指的当前剩下的时间与还需要走的步数奇偶性必须相同,
否则没有必要继续搜索,直接返回上一层。这是本题中最关键的剪枝。废话不多说,多练习,找回当初的感觉,上代码。
源代码:
#include <iostream>
#include <string.h>
#include <queue>
#define MAX 10
using namespace std;
char map[MAX][MAX];
int n,m,t,flag,ex,ey;
int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int abs(int x)
{
if(x<0)
return (-x);
return x;
}
void dfs(int sx,int sy,int cnt)
{
int temp,x,y;
if(sx>=n||sy>=m||sx<0||sy<0) return;
if(flag==1) return;
if(sx==ex&&sy==ey&&cnt==t)
{
flag=1;
return ;
}
temp=abs(sx-ex)+abs(sy-ey); //奇偶性剪枝 && 最优性剪枝
if(temp>t-cnt||(temp+t-cnt)%2!=0) return; //剩下的步数应该与剩余时间奇偶性相同
for(int i=0;i<4;i++)
{
x=sx+dir[i][0];
y=sy+dir[i][1];
if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='X') //深搜
{
map[x][y]='X';
dfs(x,y,cnt+1);
map[x][y]='.';
}
}
}
int main()
{
int sx,sy,wall;
while(cin>>n>>m>>t)
{
if(n==0&&m==0&&t==0)
break;
wall=0;
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='X')
wall++;
else if(map[i][j]=='S')
{
sx=i;
sy=j;
}
else if(map[i][j]=='D')
{
ex=i;
ey=j;
}
}
if(n*m-wall<=t)
{
cout<<"NO"<<endl;
continue;
}
flag=0;
map[sx][sy]='X';
dfs(sx,sy,0);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}