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

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES
题意是问能不能在规定时间到达终点。
dfs,剪枝很重要。避免做一些不必要的判断,还有起点不可以再走了。 代码:
#include <iostream>
#include <cstdlib>
using namespace std; int flag,n,m,t,sx,sy,ex=-,ey=-,dir[][]={,,,,,-,-,},vis[][];
char ar[][];
void dfs(int x,int y,int time)
{
char ch;
if(ar[x][y]=='D')
{
if(time==t)flag=;
return;
}
if((t-time-x-y+ex+ey)&)return;///奇偶剪枝 以坐标和判断奇偶数
if(flag||time>t)return;
int tx,ty;
for(int i=;i<;i++)
{
if(flag)return;
tx=x+dir[i][],ty=y+dir[i][];
if(tx<||ty<||tx>=n||ty>=m||ar[tx][ty]=='X'||vis[tx][ty])continue;
ch=ar[tx][ty];
vis[tx][ty]=;
dfs(tx,ty,time+);
vis[tx][ty]=;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
while(cin>>n>>m>>t)
{
if(!n&&!m&&!t)break;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>ar[i][j];
if(ar[i][j]=='S')sx=i,sy=j;
else if(ar[i][j]=='D')ex=i,ey=j;
}
}
flag=;
ar[sx][sy]='X';//起点一定要标记 不可以再走了!!!!!!!!!
dfs(sx,sy,);
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}

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. Codeforces D - The Child and Zoo

    D - The Child and Zoo 思路: 并查集+贪心 每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序 然后用并查集从大的边开始合并,因为你要合并的这两个联 ...

  2. 机器学习 Matplotlib库入门

    2017-07-21 15:22:05 Matplotlib库是一个优秀的python的数据可视化的第三方类库,其中的pyplot支持了类似matlab的图像输出操作.matplotlib.pyplo ...

  3. Java 常用对象-System类

    2017-11-02 21:41:06 System类:System 类包含一些有用的类字段和方法.它不能被实例化. *常用方法 public static void gc() 运行垃圾回收器. 调用 ...

  4. ViewPager 如何得到当前的Fragment (使用FragmentPagerAdapter)

    使用FragmentPagerAdapter时,难免要在MainActivity 和 当前显示的Fragment间传递数据.但是FragmentPagerAdapter并没有给我们提供类似getCur ...

  5. 20170405xlVBA快速录入

    Dim Rng As Range Dim Arr As Variant Dim LastCell As Range Dim FindText As String Dim ItemCount As Lo ...

  6. 把Java Web工程转换为基于Maven的Web工程

    有一个之前的工程,在使用了基于Maven的Web开发后,发现这种方式很便利,于是就想把之前老的传统的J2EE Web Project转为Maven Web Project. 转换的思路如下: 1.新建 ...

  7. Linux Used内存到底哪里去了?

    原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: Linux Used内存到底哪里去了? 前几天 纯上 同学问了一个问题: 我ps aux看到的RSS内存只有不到30M,但是free看 ...

  8. ns-3

    二.NS-3C++脚本的编写如前所述,NS-3的脚本使用C++语言(也支持python),使用四种类型的网络构件(Node.NetDevice.Channel.Application).一个简单的脚本 ...

  9. nyoj1248(阅读理解???)

    海岛争霸 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己 ...

  10. Spring Boot的SpringApplication类详解

    相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...