Tempter of the Bone HDU - 1010
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.
InputThe 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.
OutputFor 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 思路:很容易想到DFS,但第一次提交超时,说明需要剪枝,通过路径剪枝,奇偶剪枝,通过。
AC Code:
#include<iostream>
#include<utility>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
int INF=0x3f3f3f3f;
int N,M,T,sx,sy,gx,gy,flag,f;
char maze[][];
int vis[][];
int dx[]={,,-,},dy[]={,,,-};
void dfs(P p){
if(p.first ==gx&&p.second ==gy&&vis[p.first][p.second]==T){
flag=;
return;
}
if(vis[p.first ][p.second]>=T) return;//当前的步数超过要求的步数,退出
int mins=T-vis[p.first ][p.second]-abs(p.first-gx)-abs(p.second-gy);//最短距离
if(mins%||mins<) return; // 剩余步数小于最短距离或者满足奇偶剪枝条件
for(int i=;i<;i++){
int nx=p.first +dx[i],ny=p.second +dy[i];
if(nx>=&&nx<N&&ny>=&&ny<M&&maze[nx][ny]!='X'&&vis[nx][ny]==INF){
vis[nx][ny]=vis[p.first][p.second]+;
dfs(P(nx,ny));
vis[nx][ny]=INF;
}
} }
int main(){
while(~scanf("%d%d%d",&N,&M,&T),N+M+T){
int holdback=;flag=;
for(int i=;i<N;i++)
for(int j=;j<M;j++){
cin>>maze[i][j];
vis[i][j]=INF;
if(maze[i][j]=='S') sx=i,sy=j,vis[sx][sy]=;
if(maze[i][j]=='D') gx=i,gy=j;
if(maze[i][j]=='X') holdback++;//记录阻碍 X
}
if(N*M-holdback>T) dfs(P(sx,sy));//可通行的点必须大于要求的步数 路径剪枝
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl; }
}
Tempter of the Bone HDU - 1010的更多相关文章
- Tempter of the Bone HDU - 1010(dfs)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone HDU 1010(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- (转)开源项目miaosha(上)
石墨文档:https://shimo.im/docs/iTDoZs4CVfICgSfV/ (二期)19.开源秒杀项目miaosha解读(上) [课程19]几张图.xmind0.6MB [课程19]开源 ...
- 160CrackMe练手 001
peid判断无壳,打开输入伪码注册,根据报错od查找字符串 接下来定位到字符串周边代码 0042FA15 |. 8D55 F0 lea edx,[local.4] 0042FA18 |. 8B83 D ...
- Linux command: grep
How to use grep to match multiple strings in the same line? grep 'string1\|string2' filename grep -E ...
- qvalue: Check that you have valid p-values or use a different range of lambda
ERROR: The estimated pi0 <= 0. Check that you have valid p-values or use a different range of lam ...
- JQuery的ajax方法获取返回值得到了值还包含了不要的html源码 (Ajax相关知识)
因为后台使用了response.Write():这个方法,当输出完了以后,没有结束掉会继续输出,所以需要用到response.End():这样问题就解决了 jquery的ajax 除了通过url传值, ...
- 洛谷P1679神奇的四次方数--DP
原题请戳>>https://www.luogu.org/problem/show?pid=1679<< 题目描述 在你的帮助下,v神终于帮同学找到了最合适的大学,接下来就要通知 ...
- 构造函数中用:base
如果person中有自己写的构造函数 则Student类的构造方法必须要加:base继承这个构造方法!因为默认的Student:base(空)构造方法已经被干掉了 构造方法的调用//base里的参数, ...
- Linux 中 MySQL常用命令
一. 数据库登录mysql -uroot -p二..退出数据库quit 和 exit或ctrl + d三.数据库操作1. 查看所有数据库 show databases;2. 查看当前使用的数据库sel ...
- CSS段落对齐方式
CSS段落对齐有两种方式:水平对齐和垂直对齐. 1.水平对齐: (1).text-align:left; //左对齐 (2).text-align:right; //右对齐 ...
- Python 列表复制
Python 列表复制 关于Python 列表的复制,有多种方法,常用的 有 赋值.切片.list.copy,copy.deepcopy等. 但是 实际用时 又有区别,不小心可能就用错了. 接下来 简 ...