算法:深搜(需要剪芝)

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

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
The 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.
Output
For 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

代码:

 #include<iostream>#include <cstring>#include <iomanip>#include <string>#include <algorithm>using namespace std;char ch[10][10];int ok,om,og,T,n,m;int ac[4][2]={0,1,0,-1,-1,0,1,0};void dfs(int x,int y,int c,int d,int step){if(step==T){if(x==c&&y==d){ok=1;return ;}}if(ok==1) return ;int mm=abs(x-c)+abs(y-d);if(mm%2==0&&(T-step)%2==1) return;if(mm%2==1&&(T-step)%2==0) return ;if(mm>abs(T-step)) return;for(int i=0;i<4;i++){int dx,dy;dx=x+ac[i][0];dy=y+ac[i][1];if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]=='.'){ch[dx][dy]='X';dfs(dx,dy,c,d,step+1);ch[dx][dy]='.';}} }int main(){int i,j,k,p,q,s,t,a;while(cin>>n>>m>>T){k=0;if(n==0&&m==0&&T==0) break;for(i=0;i<n;i++){for(j=0;j<m;j++){cin>>ch[i][j];if(ch[i][j]=='S'){p=i;q=j;ch[i][j]='#';}if(ch[i][j]=='D'){s=i;t=j;ch[i][j]='.';}if(ch[i][j]=='X')k++;}}  ok=0;if(n*m-k<=T) {cout<<"NO"<<endl;}else{dfs(p,q,s,t,0);if(ok==1) cout<<"YES"<<endl;else cout<<"NO"<<endl;}}return 0;}

转载于:https://www.cnblogs.com/wangyumin/p/5323435.html

Tempter of the Bone相关推荐

  1. Tempter of the Bone(DFS + 奇偶剪枝,好题)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. 回溯法+奇偶剪枝——Hdu 1010 Tempter of the Bone

    1)   题目 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. HDU1010 Tempter of the Bone DFS+剪枝

    点击打开链接 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  4. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  5. hdu1010 Tempter of the Bone

    转载自:http://acm.hdu.edu.cn/forum/read.php?tid=6158 sample input: 4 4 5 S.X. ..X. ..XD .... 问题: (1): 在 ...

  6. HDOJ 1010 HDU 1010 Tempter of the Bone ACM 1010 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述: 代码 ...

  7. HDU.1010 Tempter of the Bone

    文章目录 一.题目解读 1.原题 2.分类 3.题意 4.输入输出格式 5.数据范围 二.题解参考 1.总体思路 2.思路① (1).分析 (2).AC代码 三.评价与后话 1.评价 2.奇偶剪枝0- ...

  8. HDU1010 Tempter of the Bone(DFS奇偶剪枝)

    传送门 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. Howe ...

  9. 输入空格hdu - 1010 - Tempter of the Bone

    时间紧张,先记一笔,后续优化与完善. 题意:一个N*M的地图,走过的点不能再走,X为墙弗成走,能否从点S到点D恰好用时T.(1 < N, M < 7; 0 < T < 50) ...

最新文章

  1. java从字符串中提取数字
  2. MySQL数据库have_openss_MySQL 关于OpenSSL证书支持检查方式
  3. java超线程_超线程多核心下Java多线程编程技术分析
  4. 【渝粤教育】国家开放大学2019年春季 0408-22T管理学基础 参考试题
  5. ssh源码编译安装mysql_总结源码编译安装mysql
  6. Cell重磅综述:关于人类转录因子,你想知道的都在这
  7. vbox设置剪切板共享
  8. 西瓜书+实战+吴恩达机器学习(十五)无监督学习之关联分析(Apriori, FP-growth)
  9. Siri 自动拨打 911、Google 建筛查网站、IBM 测药物成分,国际抗疫在行动!
  10. libnids libnet
  11. Skara 是研究JDK源代码的工具
  12. atitit.团队建设总结fx O622
  13. 云控微信开发SDK使用教程--手机微信收钱任务执行结果通知服务端
  14. Hausdorff 距离
  15. web期末复习---老师划重点!!
  16. 戴尔台式计算机型号怎么查,戴尔台式电脑型号的查询方法
  17. 车载网络测试 - UDS诊断篇 - 诊断数据简析
  18. P1199(NOIP2010 普及组)三国游戏 题解
  19. ZOJ 3557 (插板法+Lucas定理)
  20. 北斗星通GPS调试记录

热门文章

  1. mark python新手必碰到的问题---encode与decode,中文乱码[转]
  2. Dubbo服务引入原理
  3. php xdebug 教程视频_PHP Xdebug 是什么?
  4. 第一节 Java语言发展史
  5. 【MyBatis框架】查询缓存-二级缓存-整合ehcache
  6. 详解 Qt 串口通信程序全程图文 (1)
  7. Go语言计时器的使用详解
  8. Spring循环依赖源码剖析
  9. Nginx的原理解析
  10. springcloud 使用git作为配置中心