Tempter of the Bone
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

第一次做深搜索题目,这一道应该算简单的吧,参考了别人的代码写的,不过收获挺多的,下面的一句话,应该可以大概的概括搜索的一般解法吧:

所有的搜索算法从其最终的算法实现上来看,都可以划分成两个部分:控制结构和产生系统,也应该就是穷举着,并且筛选着。

奇偶剪枝:t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;

参考百度百科:http://baike.baidu.com/link?url=vS7Q7XC5UXvuJomzh-HLq8LJV2-ql0RTxfFEtQPSOIQDS3bnF48bHCkfYMH57hsIv_suGQYN8PaQBPNrFDvDr_#2

//N行迷宫的长度,T是开门的时间,M是每行的字符
//(1 < N, M < 7; 0 < T < 50), #include<iostream>
#include<cmath>
using namespace std;
int n,m,ex,ey,t;
bool success;
char maze[][];
void dfs(int stx,int sty,int dt)
{
if(stx<= || stx >n || sty<= ||sty > m)
return ; if(stx==ex && sty == ey && dt==t) //成功逃脱
{ success = true;return ;} int temp = (t-dt) - abs(ex-stx) - abs(ey-sty); if(temp < || temp & ) //奇偶剪枝 ,一个奇数&1是0
return; //然后是对上下左右进行搜索
if(maze[stx][sty+]!='X') //向上搜索
{
maze[stx][sty+]='X'; //把block堵上后在进行dfs;
dfs(stx,sty+,dt+);
maze[stx][sty+]= '.'; //恢复自由
} if(maze[stx+][sty]!='X') //向右搜索
{
maze[stx+][sty]='X';
dfs(stx+,sty,dt+);
maze[stx+][sty]= '.';
} if(maze[stx][sty-]!='X') //向下搜索
{
maze[stx][sty-]='X';
dfs(stx,sty-,dt+);
maze[stx][sty-]= '.';
} if(maze[stx-][sty]!='X') //向左搜索
{
maze[stx-][sty]='X';
dfs(stx-,sty,dt+);
maze[stx-][sty]= '.';
}
return;
} int main()
{
int i,j;
int stx,sty,wall;
while(cin>>n>>m>>t,n+m+t)
{
wall = ;
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
cin>>maze[i][j]; if(maze[i][j]=='S') //判定起点
{ stx = i;sty = j;} if(maze[i][j]=='D') //判定终点
{ ex = i;ey = j;} if(maze[i][j]=='X') //判断墙壁数量
wall++; }
}
success = false; //其实逃脱的成功率是渺茫的吧!
maze[stx][sty] = 'X'; //堵住入口
if(n*m-wall<=t) //当可以走的block大于时间才可能到达door
cout<<"NO"<<endl;
else
{
dfs(stx,sty,);
if(success)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return ;
}

杭电1010Tempter of the Bone的更多相关文章

  1. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  2. 杭电dp题集,附链接还有解题报告!!!!!

    Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...

  3. acm入门 杭电1001题 有关溢出的考虑

    最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...

  4. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  5. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  6. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  7. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  8. 杭电ACM2076--夹角有多大(题目已修改,注意读题)

    杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...

  9. 杭电ACM2092--整数解

    杭电ACM2092--整数解    分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...

随机推荐

  1. Struts2 原理

    .Struts2原理 .Struts 1 原理 .Struts1 和webwork的关系 .HttpServletRequest Struts2原理

  2. 基础组件_panel面板

    面板作为承载其它内容的容器.这是构建其他组件的基础(比如:layout,tabs,accordion等).它还提供了折叠.关闭.最大化.最小化和自定义行为.面板可以很容易地嵌入到web页面的任何位置. ...

  3. 事务&视图和索引

    一:事务 1.含义:事务是一个不可分割的整体,事务中的多个执行过程,同生共死.要么都执行成功,要么都执行失败. 事务必须具备以下四个属性,(简称:ACID): ①.原子性(Atomicity):事务的 ...

  4. redis接入sentinelPool的配置

    package com.netease.mobile.commonUtil; import java.util.ArrayList; import java.util.HashSet; import ...

  5. 浅析Android中的消息机制(转)

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  6. js模拟实现继承功能

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. MYSQL复制的几种模式

    MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制. MYSQL复制的几种模式 MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制.简言之,这种新技 ...

  8. 项目中使用的ajax异步读取数据结构设计

    设计稍微复杂了一点,完成ajax读取功能涉及到了很多页面.虽然如此,但感觉比较灵活. 和传统方法唯一的区别在于多了一层数据容器控件,里面提供了显示数据的HTML元素及相应的JS方法. 这样数据控件指生 ...

  9. LeetCode——Add Two Numbers

    Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...

  10. 剑指offer—第三章高质量代码(数值的整数次方)

    高质量的代码:容错处理能力,规范性,完整性.尽量展示代码的可扩展型和可维护性. 容错处理能力:特别的输入和处理,异常,资源回收. 规范性:清晰的书写,清晰的布局,合理的命名. 完整性:功能测试,边界测 ...