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 ...
随机推荐
- kubernetes之故障现场二,节点名称冲突
系列目录 问题描述:测试环境由于异常断电导致服务器重启一后,有一个节点的状态一直是NotReady.通过journalctl -f -u kubelet没有错误日志输出.通过tail /var/log ...
- krpano HTML5 Viewer可以实现全景展示
http://www.krpano360.com/ krpanp 陀螺仪调取 全景展示
- ubuntu 16.04 更新 gcc/g++ 4.9.2
ubuntu 转载 2016年10月12日 :: 标签:ubuntu /g++ /gcc [html] view plain copy sudo dpkg -l g++ 最近在学C++primer , ...
- MySQL 存储过程 (3)
以下介绍下像数据库循环插入数据操作 第一步:建立存储过程用到的信息表
- stretchableImageWithLeftCapWidth
本文转载至 http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html (NSInteger)topCapHeight 这个函数是UIIm ...
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
- 【BZOJ4953】lydsy七月月赛 F DP
[BZOJ4953]lydsy七月月赛 F 题面 题解:设f[i][j]表示第i个强度取为j时的最小误差.那么每次转移时,我们只计算j'和j之间的像素点带来的误差,于是有: $f[i][j]=min( ...
- python中的括号以及元组和列表的区别
1 python中的括号 1.1 花括号 花括号表示的是字典,即键值对. 1.2 方括号 方括号表示的是列表,类似于数组,但是可以允许存放混杂类型的数据. 1.3 圆括号 圆括号表示的是元组,类似于列 ...
- mysql user password plugin
caching_sha2_passwordcaching_sha2_passwordcaching_sha2_passwordcaching_sha2_passwordcaching_sha2_pas ...
- 5.JavaScript改变样式,验证用户输入
① x=document.getElementById("demo") //找到元素 x.style.color="#ff0000"; //改变样式 ② if ...