HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
'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
Sample Input
Sample Output
题目大意:
一扇门只能在第T秒时打开,问小狗是否能在开门时恰好到达这扇门,逃出去。
解题思路:
DFS问题。
| s | ||||
| | | ||||
| | | ||||
| | | ||||
| + | — | — | — | e |
数, 记做step,此处step1=8;
| s | — | — | — | |
| — | — | + | ||
| | | + | |||
| | | ||||
| + | — | — | — | e |
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #define N 10
- using namespace std;
- bool flag,ans,visited[N][N];
- int n,m,t,xe,ye;
- char map0[N][N];
- void dfs(int x,int y,int timen){
- if(flag) return ;
- if(timen>t) return ;
- if(x<0||x>n-1||y<0||y>m-1) return ;
- if(timen==t&&map0[x][y]=='D') {flag=ans=true;return ;}
- int temp=t-timen-abs(xe-x)-abs(ye-y);
- if(temp&1) return ;//奇偶剪枝,位运算判断是否为奇数,比mod更快.
- if(!visited[x-1][y]&&map0[x-1][y]!='X'){
- visited[x-1][y]=true;
- dfs(x-1,y,timen+1);
- visited[x-1][y]=false;
- }
- if(!visited[x+1][y]&&map0[x+1][y]!='X'){
- visited[x+1][y]=true;
- dfs(x+1,y,timen+1);
- visited[x+1][y]=false;
- }
- if(!visited[x][y-1]&&map0[x][y-1]!='X'){
- visited[x][y-1]=true;
- dfs(x,y-1,timen+1);
- visited[x][y-1]=false;
- }
- if(!visited[x][y+1]&&map0[x][y+1]!='X'){
- visited[x][y+1]=true;
- dfs(x,y+1,timen+1);
- visited[x][y+1]=false;
- }
- }
- int main(){
- int xs,ys;
- while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n||m||t)){
- int cnt=0;
- getchar();
- memset(visited,false,sizeof(visited));
- flag=ans=false;
- for(int i=0;i<n;i++){
- for(int j=0;j<m;j++){
- cin>>map0[i][j];
- if(map0[i][j]=='S'){
- xs=i;ys=j;
- visited[i][j]=true;
- }
- if(map0[i][j]=='D'){
- xe=i;ye=j;
- }
- if(map0[i][j]=='X'){
- cnt++;
- }
- }
- }
- if(n*m-cnt-1>=t) dfs(xs,ys,0);
- if(ans) printf("YES\n");
- else printf("NO\n");
- }
- return 0;
- }
HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】的更多相关文章
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu1010 Tempter of the Bone(深搜+剪枝问题)
Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...
- HDU1010 Tempter of the Bone(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...
- 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(DFS + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- java stoi
package string.string1_4; import java.util.Scanner; public class StrToInt { /** * 将str转换为int整数 * 1. ...
- TXT文本写入数据库
load data local infile "D:/abc.txt" into table lee; leedabao.txt内容如下,中间用Tab隔开: 2 yuanpeng ...
- 在VS2015中增加JQuery引用及智能提示
打开VS2015,从"工具"菜单选择NuGet选项,搜索Jquery,并点击安装. 可以看到解决方案的scripts增加了对应文件的引用 在HTML文件中可以直接引用j ...
- JavaScript全讲-必知的特性
上篇讲完JS面向对象的特性,我们今天就来聊聊它的其它特性. JS面向对象的特性较为隐晦,真正使用也是比較少的.而今天我们要讲的,却是在实际开发中常常遇到的特性. 1. 函数式编程 多数人并不知道&qu ...
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
- Oracle 数据库中序列结合触发器实现主键自增长
一.数据表名称为T_OFFICE,其主键为PID(number类型) 二.首先为数据表的PID字段创建序列 序列名称:S_T_OFFICE_PID 序列详细内容: 三.创建相应的触发器 触发器名称:T ...
- Dropout: A Simple Way to Prevent Neural Networks fromOverfitting
https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf Deep neural nets with a large number of par ...
- 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?
作者:Wang Namelos链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...
- Redis缓存服务搭建及实现数据读写 - Eric.Chen
发现博客园中好多大牛在介绍自己的开源项目是很少用到缓存,比如Memcached.Redis.mongodb等,今天得空抽时间把Redis缓存研究了一下,写下来总结一下,跟大家一起分享 一下.由于小弟水 ...
- 重学DSP:对于卷积的理解
最近,我发现自己对于一个事情,如果不给自己一个说服自己的理由,就会出现不能理解,不能记住,以至于不会使用或者“盲目”应用的情况. 但是,我学的这个学科就是应当建立在对信号作用过程的理解上面的. 下面, ...