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 ...
随机推荐
- website link
error: template with C linkage http://blog.csdn.net/jiong_1988/article/details/7915420http://velep.c ...
- angular input file 上传文件
<body > <div ng-controller="fileCtrl"> <form ng-submit="submit(obj)&qu ...
- 根据ip识别地区
<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></scri ...
- 【Effective C++】继承与面向对象设计
关于OOP 1,继承可以是单一继承或多重继承,每一个继承连接可以是public.protected或private,也可以是virtual或non-virtual. 2,成员函数的各个选项:virtu ...
- 事件 MotionEvent
点击和长按可能会同时发生,需要在长按的回调函数中返回true,就不会产生点击.谁处理事件谁就是消费者 如果view组件不处理事件,最后会让ontouchevent处理,它是备胎 <LinearL ...
- Xcode工程断点调试失效
1.我解决的是方法是,选择Product---->Edit Scheme------>(这里进入后会有Info,Arguments,Options,Diagnostics)选择Info-- ...
- hdu-5761 Rower Bo(数学)
题目链接: Rower Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- C++之萃取技术(traits)
为什么需要类型萃取(特化) 前面我们提到了迭代器,它是一个行为类似于smart pointer之类的东西,主要用于对STL容器中的对象进行访问,而且不暴露容器中的内部结构,而迭代器所指对象的型别称为该 ...
- win10 下安装linux子系统
一.开发人员选项 打开控制面板->程序与功能->启用或关闭windows功能 勾选 [适用于linux的windows子系统] 选项 打开win10设置 找到更新与安全 启动开 ...
- linux下实现目录即文件的完整删除
功能: 1.删除目录 2.删除文件 3.删除不为空的目录即下属文件 #ifndef _DELETE_FILE #define _DELETE_FILE #include <sys/stat.h& ...