HDU 1010 Tempter of the Bone
\(\color{CornflowerBlue}{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.
\(\color{CornflowerBlue}{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.
\(\color{CornflowerBlue}{Output}\)
For each test case, print in one line "\(YES\)" if the doggie can survive, or "\(NO\)" otherwise.
\(\color{CornflowerBlue}{Sample\;Input}\)
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
\(\color{CornflowerBlue}{Sample\;Output}\)
NO
YES
\(\color{CornflowerBlue}{Author}\)
ZHANG, Zheng
\(\color{CornflowerBlue}{Source}\)
\(\color{CornflowerBlue}{Recommend}\)
JGShining | We have carefully selected several similar problems for you: 1242 1072 1312 1026 1240
题目描述
汪汪在一个古老的迷宫里找到了一块骨头,这让他很着迷。 然而,当他捡起它时,迷宫开始摇晃,汪汪可以感觉到地面下沉。 他意识到骨头是个陷阱,他拼命地想离开这个迷宫。
迷宫是一个大小为\(N\)的矩形。迷宫里有一扇门。 开始时,门是关闭的,它将在第\(T\)秒打开一段短时间(不到\(1\)秒)。 所以狗只必须在第\(T\)秒到达门口。 在每一秒,他可以移动一个街区到一个上,下,左和右相邻的街区。 一旦他进入一个街区,这个街区的地面就会在下一秒开始下沉并消失。 他不能在一个地方停留超过一秒钟,也不能前往一个到过的地方。 可怜的汪汪能活下来吗? 请帮帮他。
输入
输入由多个测试用例组成。 每个测试用例的第一行包含三个整数\(N, M,\)和\(T (1 < N, M < 7; 0 < T < 50)\),分别表示迷宫的大小和门打开的时间。 接下来的\(N\)行给出迷宫布局,每一行包含\(M\)个字符。 一个字符是以下内容之一:
‘\(X\)’:一堵墙,狗进不去;
'\(S\)':汪汪的起点;
'\(D\)':门;或者
'.': 一块空地。
输入以三个\(0\)的结束。 此测试用例不被处理。
输出
对于每个测试用例,如果汪汪可以生存,打印一行“\(YES\)”,,否则打印"\(NO\)"。
Solution
思路借鉴于Love风吟的HDU1010 dfs + 剪枝
1.题意十分好理解,但关键问题在于剪枝;
2.首先可以想到的剪枝便是曼哈顿距离,如果当前点到终点的曼哈顿距离大于剩余的时间,那么必然从此点怎么走都走不出去;
3.第二个优化就很巧,可以对曼哈顿距离进行二次利用,如果曼哈顿距离小于剩余的时间,说明需要在出口前逗留一段时间,这段时间必须不停地移动,那么一旦逗留时间是奇数,则不可能走出去,
因为考虑最小的情况S.D
,就需要\(2\)秒,假设可以在空地上反复逗留(但必须走过去),那么能到出口的时间分别为\(2,4,6,8,\cdots,2*n\),不可能出现奇数的情况,将地图往大里推广也是如此。
- TAG:搜索;剪枝
std.cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,t,ex,ey;
int fx[]={0,0,1,-1};
int fy[]={1,-1,0,0};
char mp[10][10];
bool flag,vis[10][10];
bool check(int x,int y){ return 1<=x&&x<=n&&1<=y&&y<=m; }
void dfs(int x,int y,int tim){
if(tim+1>t) return;
int res=t-abs(ex-x)-abs(ey-y)-tim;
if(res<0||res&1==1) return;
//对于当前点来说,消耗了tim秒
//运行到下面的循环,则就要消耗tim+1秒了
//所以循环语句往上使用tim处理,循环内使用tim+1处理
for(int i=0;i<4;++i){
int nx=x+fx[i],ny=y+fy[i];
if(vis[nx][ny]||mp[nx][ny]=='X'||!check(nx,ny)) continue;
vis[nx][ny]=1;
if(mp[nx][ny]=='D'&&tim+1==t){ flag=1; return; }
dfs(nx,ny,tim+1);
if(flag) return;
vis[nx][ny]=0;
}
}
int main(){
while(scanf("%d %d %d",&n,&m,&t)&&n!=0){
memset(vis,0,sizeof(vis));
flag=0;
int sx,sy;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j){
cin>>mp[i][j];
if(mp[i][j]=='S'){ sx=i; sy=j; vis[sx][sy]=1; }
if(mp[i][j]=='D'){ ex=i; ey=j; }
}
dfs(sx,sy,0);
if(flag) puts("YES");
else puts("NO");
}
return 0;
}