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. Android Camera 使用小结

    Android手机关于Camera的使用,一是拍照,二是摄像,由于Android提供了强大的组件功能,为此对于在Android手机系统上进行Camera的开发,我们可以使用两类方法:一是借助Inten ...

  2. Android中GridView拖拽的效果【android进化三十六】

      最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...

  3. vmware装redhat该光盘无法被挂载

    为了考网工,没办法只能学学linux了,前天在vmware7装redhat 提示该光盘无法被挂载,还以为是光盘错误,换了N个盘,又装了很多次,最后观察到,换了盘之后点确定,里面就提示该光盘无法被挂载, ...

  4. jQuery 清除div内容

    $.ajax({            url: "SearchSN.aspx",            data: "SN=" + $("#txtS ...

  5. 【英语】Bingo口语笔记(20) - i长短音

    短音有个ei的音,多练习下 长音是咦拉长

  6. margin collapse 之父子关系的DIV

    打算花点时间将知识整理一下,虽然平时现用现查都能完成工作,可是当遇到面试这种事情的时候,临时查就来不及了... 关于margin,整理若干知识点如下: 一:父子关系的DIV标签以及未加margin时的 ...

  7. PrintWriter的println问题

    今天发现一个奇怪的问题,同样的代码web server部署在windows自测机器上跟linux服务器上, 在通信上出现了不一样的换行结束符. Debug发现通过PrintWriter的println ...

  8. Java核心技术II读书笔记(一)

    Char2 XML 解析器:读入一个文件,确认其具有正确的格式,然后将其分解成各种元素,使程序员能够访问这些元素. java库提供了两种XML解析器:DOM和SAX,即文档对象模型和流机制解析器. D ...

  9. 【转】Xcode添加静态库以及编译选项配置常见问题

    原文网址:http://www.cnblogs.com/Quains/p/3276425.html 一,Xcode编译出现Link错误,出现"duplicate symbols for ar ...

  10. objective-C 中两种实现动画的方法

    第一种方法: [UIView beginAnimations:@"Curl"context:nil];//动画开始 [UIView setAnimationDuration:1.2 ...