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 ...
随机推荐
- JS 模板引擎 Handlebars.js 中文说明
Handlebars 为你提供了一个可以毫无挫折感的高效率书写 语义化的模板 所必需的一切. Mustache 模板和 Handlebars 是兼容的,所以你可以把Mustache模板拿来导入到Han ...
- IOS 为UILabel添加长按复制功能
IOS 为UILabel添加长按复制功能 在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framewor ...
- php 面向对象的三大要素(封装、继承、多态)以及重写(override)和重载(overload)的举例说明
PHP是一种HTML内嵌式的,用来制作动态网页的服务器端的脚本语言.其特点是:开发周期短,稳定安全,简单易学,免费开源,良好的跨平台特性.PHP是一种面向对象的程序设计语言,目前已成为全球最受欢迎的五 ...
- requests 中文乱码
jsUrl = 'http://www.heze.cn/qiye/index.php?a=userinfo&username={}'.format(bizQiyeId)r = requests ...
- Netty 100万级高并发服务器配置
前言 每一种该语言在某些极限情况下的表现一般都不太一样,那么我常用的Java语言,在达到100万个并发连接情况下,会怎么样呢,有些好奇,更有些期盼. 这次使用经常使用的顺手的netty NIO框架(n ...
- LINUX上一个命令计算PI
Linux上一个命令计算PI – 笑遍世界 http://smilejay.com/2017/11/calculate-pi-with-linux-command/ [root@d1 goEcho]# ...
- 编写灵活、稳定、高质量的 HTML 和 CSS 代码的规范。
引用地址http://codeguide.bootcss.com/#html-ie-compatibility-mode <!DOCTYPE html> <html lang=& ...
- android系统启动框架、Activity界面显示过程详解
一.Android系统框架 android的系统架构和其操作系统一样,采用了分层的架构.从架构图看,android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心 ...
- MySQL学习笔记(一)——数据库基础
自己上大学时也学习过数据库,做开发时也用到过,但是做界面开发对于数据库的使用相对来说是比较简单的,大学时系统学习的数据库也还给老师了,在测试工作中也只是用到了一些基础sql,增删改查这一类的,但是自己 ...
- struts2 小例子(教训篇)
学了一阵子的struts2了,到了最后,想自己写个小程序,发现最简单的配置文件都 竟然能弄错,是我这几天睡眠不足么.怎么可能,爱好这门的,怎么会这样.这样真的很伤心啊.小小心灵受不了这种打击啊.... ...