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.

InputThe 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.

OutputFor each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

Sample Input

  1. 4 4 5
  2. S.X.
  3. ..X.
  4. ..XD
  5. ....
  6. 3 4 5
  7. S.X.
  8. ..X.
  9. ...D
  10. 0 0 0

Sample Output

  1. NO
  2. YES
    题意是问能不能在规定时间到达终点。
    dfs,剪枝很重要。避免做一些不必要的判断,还有起点不可以再走了。
  3.  
  4. 代码:
  5.  
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int flag,n,m,t,sx,sy,ex=-,ey=-,dir[][]={,,,,,-,-,},vis[][];
  6. char ar[][];
  7. void dfs(int x,int y,int time)
  8. {
  9. char ch;
  10. if(ar[x][y]=='D')
  11. {
  12. if(time==t)flag=;
  13. return;
  14. }
  15. if((t-time-x-y+ex+ey)&)return;///奇偶剪枝 以坐标和判断奇偶数
  16. if(flag||time>t)return;
  17. int tx,ty;
  18. for(int i=;i<;i++)
  19. {
  20. if(flag)return;
  21. tx=x+dir[i][],ty=y+dir[i][];
  22. if(tx<||ty<||tx>=n||ty>=m||ar[tx][ty]=='X'||vis[tx][ty])continue;
  23. ch=ar[tx][ty];
  24. vis[tx][ty]=;
  25. dfs(tx,ty,time+);
  26. vis[tx][ty]=;
  27. }
  28. }
  29. int main()
  30. {
  31. ios::sync_with_stdio(false);
  32. cin.tie();
  33. while(cin>>n>>m>>t)
  34. {
  35. if(!n&&!m&&!t)break;
  36. for(int i=;i<n;i++)
  37. {
  38. for(int j=;j<m;j++)
  39. {
  40. cin>>ar[i][j];
  41. if(ar[i][j]=='S')sx=i,sy=j;
  42. else if(ar[i][j]=='D')ex=i,ey=j;
  43. }
  44. }
  45. flag=;
  46. ar[sx][sy]='X';//起点一定要标记 不可以再走了!!!!!!!!!
  47. dfs(sx,sy,);
  48. if(flag)cout<<"YES"<<endl;
  49. else cout<<"NO"<<endl;
  50. }
  51. }

Tempter of the Bone dfs+剪枝的更多相关文章

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

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

  2. B - Tempter of the Bone(DFS+剪枝)

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  3. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  4. 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 ...

  5. Tempter of the Bone(dfs奇偶剪枝)

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

  6. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  7. 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 ...

  8. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

  9. zoj 2110 Tempter of the Bone (dfs)

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

随机推荐

  1. StartCoroutine 和 StopCoroutine

    我的Unity版本是2017.2.0p4(64-bit) StartCoroutine的两个版本: StartCoroutine(string methodName) StartCoroutine(I ...

  2. [.NET开发] C# 如何更改Word语言设置

    一般在创建或者打开一个Word文档时,如果没有进行过特殊设置的话,系统默认的输入语言的是英语输入,但是为适应不同的办公环境,我们其实是需要对文字嵌入的语言进行切换的,因此,本文将介绍如何使用Spire ...

  3. python3 操作windows的粘贴板(读取和传值)

    #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" import win32con import wi ...

  4. 12月10日 render( locals:{...}) 传入本地变量。

    Jdstor第一部分后台设计,4-4上传图片. 3.4 Using Partials--3.4.4 Passing Local Variables You can also pass local va ...

  5. Python中的魔术方法详解

    介绍 在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”,中文称『魔术方法』,例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中 ...

  6. CF1083B The Fair Nut and String

    题意 给出两个长度为n的01字符串S和T. 选出k个字典序在S和T之间的长度为n的01字符串,使得尽可能多的字符串满足其是所选字符串中至少一个串的前缀. 这是一道思路比较奇怪的类似计数dp的题. 首先 ...

  7. Coconuts, Revisited(递推+枚举+模拟)

    Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...

  8. javaScript面向对象是什么?(一)

    js就是个面向对象语言,一切皆对象 一.什么是面向对象? 简单点,就这么说吧.生活中有一些个物品,譬如(哈哈,还想起个譬如)说一个iPhone,我们也不知道里面的工作原理吧?但是咱们会按按钮呀,用一俩 ...

  9. JavaScript权威指南(第6版)(中文版)笔记

      JavaScript权威指南(第6版)(中文版)笔记      

  10. learning uboot distro design in am335x-evm board

    reference: uboot_dir/doc/README.distro Linux distributions are faced with supporting a variety of bo ...