Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 90328    Accepted Submission(s): 24554

Problem Description
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
 
又是一道搜索题目。使用深度优先搜索,注意要加入剪枝条件。不然非常easy造成超时,同一时候做搜索题要注意回溯。举个样例,若是求经典问题--油田的块数,我们不须要回溯,可是本题是求路径,我们须要用到回溯,不然会答案错误的,这是因为求油田块数的话,我们遍历了,不须要再次遍历,而求路径的话,若是求得的路径不通。须要将标记的路径还原,回溯非常重要!

。。

本题代码例如以下:
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std; int N,M,T,escape,wall;
int
starti,startj,endi,endj;
char
map[101][101];
int
v[101][101];
int
dir[4][2]={1,0,-1,0,0,1,0,-1};
void
dfs(int x,int y,int time)
{
if(
abs(endi-x)+abs(endj-y)>T-time)return;
if((
abs(endi-x)+abs(endj-y))%2!=(T-time)%2)return;
if(
x==endi&&y==endj&&time==T)escape=1;
if(
escape==1)return;
for(int
i=0;i<=3;i++)
{
int
xx=x+dir[i][0];
int
yy=y+dir[i][1];
if(
xx>=1&&xx<=N&&yy>=1&&yy<=M&&map[xx][yy]!='X'&&v[xx][yy]==0)
{

v[xx][yy]=1;
dfs(xx,yy,time+1);
v[xx][yy]=0; //回溯
}
}
return;
}
int main()
{
while(
cin>>N>>M>>T,N||M||T)
{

wall=0;
memset(v,0,sizeof(v));
for(int
i=1;i<=N;i++)
for(int
j=1;j<=M;j++)
{

cin>>map[i][j];
if(
map[i][j]=='S')
{

starti=i;
startj=j;
}
else if(
map[i][j]=='X')
{

wall++;
}
else if(
map[i][j]=='D')
{

endi=i;
endj=j;
}
}

v[starti][startj]=1;
escape=0;
dfs(starti,startj,0);
if(
N*M-wall<T){cout<<"NO"<<endl;continue;}
if(
escape==1)cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return
0;
}

杭电(hdu)ACM 1010 Tempter of the Bone的更多相关文章

  1. 杭电 HDU ACM 2795 Billboard(线段树伪装版)

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 杭电 HDU ACM Milk

    Milk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. 杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)

    欢迎"热爱编程"的高考少年--报考杭州电子科技大学计算机学院 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memor ...

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

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

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

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

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

  7. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

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

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

随机推荐

  1. Java压缩技术(一) ZLib

    原文:http://snowolf.iteye.com/blog/465433 有关ZLib可参见官方主页 http://www.zlib.net/ ZLib可以简单的理解为压缩/解压缩算法,它与ZI ...

  2. netty百万连接跟踪记录

    0. 启动客户端和服务端 # 测试环境: centos7 jdk8 2核16G# 服务端启动nohup java -Xmx8192m -Xms4096m -XX:+UseG1GC -XX:Parall ...

  3. CentOS6 在线安装PostgreSQL10

    本文主要通过实际案例介绍如何在CentOS6环境中在线安装PostgreSQL10,安装环境需具备能够使用yum在线安装功能.具体安装步骤如下, 1 下载对应版本的PGDG文件 从https://yu ...

  4. Spring Boot (8) 全局异常处理

    服务层和dao层的最终异常会交给controller处理,控制层的异常则会记录日志系统. 新建一个类用来处理异常,名字随便GlobalDefaultExceptionHandler.java,加上@C ...

  5. Application windows are expected to have a root view controller at the end of application launch

    今天把Xcode升级了,模拟器 用的12.1的系统,运行时发现项目总是崩溃,采用9.3系统的测试机发现错误日志如下: Application windows are expected to have ...

  6. CSS画各种二维图形

    1.效果 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %> ...

  7. 浅谈ByteBuffer转换成byte[]时遇到的问题

    有些时候我们要把ByteBuffer转换成byte[]来使用.于是很多时候会用以下代码来转换: ByteBuffer buf; .....(一些往buffer写数据的操作) byte[] bs= ne ...

  8. chown chmod chgrp chattr chroot usermod 命令简单分析

    chown用于修改文件或目录的所属主与所属组,格式为:“chown [参数] 所属主:所属组 文件或目录名称”.[root@localtion ~]# chown root:bin test[root ...

  9. vue中eventbus 多次触发的问题

    相比于react,vue是一个更上手很快的框架.所说简单易用,但在做Vue项目的过程中,就会发现,坑还是有的.组件之间传递数据是一个常见的需求,父子组件传递数据在官网有介绍,除了父子组件,一般地,任意 ...

  10. Jenkins构建项目

    创建项目 Jenkins版本:Jenkins ver.2.150.1 在Jenkins首页点击‘New 任务’进入创建任务页面,在‘Enter an item name’输入框内输入项目名称,选择Je ...