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 ...
随机推荐
- #2718. 「NOI2018」归程 kruskal重构树
链接 https://loj.ac/problem/2718 思路 我们希望x所在的连通块尽量的大,而且尽量走高处 离线的话可以询问排序,kruskal过程中更新答案 在线就要用kruskal重构树 ...
- JQ插入节点方法
1.append() appendTo() prepend() prependTo() 2. before() insertBefore() after() insertAfter()
- centos7 mail
For anyone wondering how to read these messages one by one, you can just use 'mail' $ mail Then type ...
- sql 之 INSERT IGNORE
INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以保留 ...
- [JavaScript] - 判断时间是否是当天
https://www.codewars.com/kata/is-the-date-today/train/javascript function isToday(date) { return new ...
- Derek解读Bytom源码-P2P网络 upnp端口映射
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...
- PTA 7-3 jmu-ds-单链表的基本运算(15 分)
jmu-ds-单链表的基本运算(15 分) 实现单链表的基本运算:初始化.插入.删除.求表的长度.判空.释放.(1)初始化单链表L,输出L->next的值:(2)依次采用尾插法插入元素:输入分两 ...
- 17秋 软件工程 团队第三次作业 预则立&他山之石
题目:团队作业-预则立&&他山之石 团队: 我说嘻(xì)哈(hà)你说侠 17秋 软件工程 团队第三次作业 预则立&他山之石 1.确立团队选题,建立和初步熟悉团队git的协作 ...
- CentOS7使用firewalld和selinux
转载自莫小安的博客:https://www.cnblogs.com/moxiaoan/p/5683743.html 如何查看和使用selinux https://blog.csdn.net/edide ...
- 【译】第24节---Fluent API - 属性映射
原文:http://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api.as ...