Tempter of the Bone
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
题意:输入N,M,T,表示一个N*M的迷宫,然后N行,每行M个字符,S表示初始位置,X表示墙,"."表示路,D表示门,求能否刚好在T秒的时候到达D点。
分析:一开始以为是一道BFS的题目,后来写着写着,要么是WA要么是TLE,感觉不对劲,看了一下网上的题解,这道题只是求是否存在某一种的状态,这样只需要用DFS来解决是否存在这种状态,如果是BFS的话,是判断走一步所能走的最多的点,这样的话可能会忽略为了在T秒达到D点,特意延长了路径,而且还需要两个重要的剪枝,不然的话老是TLE,其中一个判断曼哈顿距离与剩余的时间,另一个是奇偶剪枝(这个可以百度来查看)
代码:
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int n,m,t;
char str[10][10];
int id[10][10];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
int x1,y5,x2,y2;
int flag;
void dfs(int x,int y,int step)
{
if(flag==true)
return ;
if(x==x2&&y==y2&&step==t)
{
flag=true;
return ;
}
else
{
int a=abs(x-x2)+abs(y-y2);//曼哈顿距离
int b=t-step;//剩余的步数
if(a>b||(b-a)%2!=0)
{
flag=false;
return ;
}
id[x][y]=1;
for(int i=0;i<4;i++)
{
int x3=x+dir[i][0];
int y3=y+dir[i][1];
if(x3>=0&&x3<n&&y3>=0&&y3<m&&id[x3][y3]==0&&str[x3][y3]!='X')
{
id[x3][y3]=1;
dfs(x3,y3,step+1);
id[x3][y3]=0;
}
}
id[x][y]=0;
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t)&&n&&m&&t)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf(" %c",&str[i][j]);
if(str[i][j]=='S')
{
x1=i;
y5=j;
}
if(str[i][j]=='D')
{
x2=i;
y2=j;
}
}
}
memset(id,0,sizeof(id));
flag=false;
dfs(x1,y5,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1010