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 766C - Mahmoud and a Message

    C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. English trip -- Review Unit4 Health 健康

    medicine    n. 药:医学:内科:巫术  vt. 用药物治疗:给…用药 drug  毒药;药店(drugstore) pill  药丸 patient 病人 head 头 hands 手 ...

  3. 12月3日周日,关联:has_many(dependent::delete_all和destroy的区别) 注意看log; where等查询语句的用法。 layout传递参数❌

    错误❌: 1.belongs_to :job, dependent: :destroy //尝试删除一条resumen后,job没有同步删除?? 答:建立一对多的关系,如job和resume.应该在j ...

  4. Ubuntu 18.04 LTS 安装wine 、exe程序安装和卸载

    什么是wine?Wine(是“Wine Is Not an Emulator”的缩写)是一个兼容层,能够在几个POSIX兼容的操作系统上运行Windows应用程序,如Linux.MaOS.BSD.代替 ...

  5. spring boot 基础篇 -- 自带图片服务器

    我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到resource或者项项目中的某个位置,这样会有一个缺点,当我们重新项目打包时,这些图片会丢失.为了解决这一缺点,我们 ...

  6. spark出现task不能序列化错误的解决方法 org.apache.spark.SparkException: Task not serializable

    import org.elasticsearch.cluster.routing.Murmur3HashFunction; import org.elasticsearch.common.math.M ...

  7. springboot笔记(一)

    1.为什么一定要实现Iterable接口? http://blog.csdn.net/albert0420/article/details/44922325 而Iterable则不然,每次调用都会返回 ...

  8. Excel中输入身份证后3位变成0,怎么办?

    1.问题介绍: 1)问题: 在使用excel输入身份证号时,会发现直接输入数字后最后面的三位数字都变成0了,这可怎么办呢?

  9. UVALive 2318 水题

    给出c 个竞争者.v 个投票人.每个投票人的投票顺序.问你谁会胜出.在第几轮.完全是个水题.比赛的时候debug接近两个点没过.因此差点放弃了整场比赛.猜测是错在找最大和第二大的序号哪里错的.因为我换 ...

  10. SSO-基本概念

    什么是单点登录 单点登录(Single Sign On) 简称为sso,是目前流行的企业业务整合的解决方案之一.SSO的定义是在多个引用系统中用户只需要登录一次就可以访问所有相互信任的应用系统. 单点 ...