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. HTML表单组件

    HTML表单组件 一.说明 form标签里面的东西 二.效果图 三.代码 <!DOCTYPE html> <html> <head> <title>Fo ...

  2. Linux后台日志定时清理脚本

    一. 简介 linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否 ...

  3. Mac安装软件时 提示已损坏的解决方法

    进入终端: sudo spctl --master-disable

  4. android--------自定义控件ListView实现下拉刷新和上拉加载

    开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,为了方便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能. Android下拉刷新可以分为两种情况: 1.获取 ...

  5. axios构建请求池处理全局loading状态&&axios避免重复请求

    很多时候我们能够看到类似进度条一样的东西在页面顶部进行加载,代表页面是否加载完成,或者其他的loading效果,我们当然不可能通过promise.all来讲所有的请求合并到一起然后进行处理,这个时候我 ...

  6. zzuli303(奇葩26进制转换)

    序号互换 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来.单元格的行坐标是由数字 ...

  7. 页面跳转 Server.Transfer和 Response.Redirect的区别

    1.Server.Transfer 用于把处理的控制权从一个页面转移到另一个页面,在转移的工程中没有离开服务器内部控件(如request,session等)保存的信息不变.因此你能从a页面跳转到b页面 ...

  8. Eclipse用了官方汉化后,无法输入

    解决方法:Rclipse右键→属性→兼容性→windows vista

  9. Oracle外部表的管理和应用

    外部表作为oracle的一种表类型,虽然不能像普通库表那么应用方便,但有时在数据迁移或数据加载时,也会带来极大的方便,有时比用sql*loader加载数据来的更为方便,下面就将建立和应用外部表的命令和 ...

  10. 无名管道跟dup,dup的使用

    参考资料: http://www.tldp.org/LDP/lpg/node11.html http://blog.csdn.net/yeyuangen/article/details/6852682 ...