题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 79233 Accepted Submission(s): 21596
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
ZHANG, Zheng
Source
题意:输入一个n*m的迷宫,和一个T:可以在迷宫中生存的最大时间。S为起点,D为终点。并且,每个格子只能踩一次,且只能维持一秒,
然后该块地板就会塌陷。所以你必须每秒走一步,且到D点时,所用时间为T。用深搜。
思路:剪枝+dfs
第一个剪枝我们可以想到,当剩下的步数大于剩下的时间的时候,狗是不能走到的;
接下来我们来第二个剪枝:
我们把map的奇偶性以01编号:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
我们发现从0走一步一定走到1,从1走一步一定走到0。
也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。
同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。
也就是说,狗的坐标x、y和对2取余是它的奇偶性,Dxy和对2取余是D的奇偶性。
两个奇偶性一加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可。
AC代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int L=1005;
char Map[L][L];
bool vis[L][L],flag;
int m,n,t,sx,sy,ex,ey;
int dir[][2]={
{0,1},{-1,0},{0,-1},{1,0}
};
void dfs(int x,int y,int d)
{
if(d<0) return ;
if(d<(abs(ex-x)+abs(ey-y))||d%2!=(abs(ex-x)+abs(ey-y))%2) return ;
if(x==ex&&y==ey)
{
if(d==0) flag=true;
return ;
}
for(int i=0;i<4&&!flag;i++)
{
int nx=dir[i][0]+x,ny=dir[i][1]+y;
if(!flag&&0<=nx&&nx<n&&0<=ny&&ny<m&&!vis[nx][ny]&&Map[nx][ny]!='X')
{
vis[nx][ny]=1;
// printf("nx=%d,ny=%d,d=%d\n",nx,ny,d+1);
dfs(nx,ny,d-1);
vis[nx][ny]=0;
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&t)&&(n+m+t))
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%s",Map[i]);
for(int j=0;j<m;j++)
if(Map[i][j]=='S') sx=i,sy=j;//cout<<i<<" "<<j<<endl;
else if(Map[i][j]=='D') ex=i,ey=j;//cout<<i<<" "<<j<<endl;
}
flag=false;
vis[sx][sy]=1;
dfs(sx,sy,t);
if(!flag) printf("NO\n");
else printf("YES\n");
}
return 0;
}