杭电1010Tempter of the Bone
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步恰好到达;
//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的更多相关文章
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 杭电dp题集,附链接还有解题报告!!!!!
Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱 最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...
- acm入门 杭电1001题 有关溢出的考虑
最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”
按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...
- C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~
暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...
- 杭电ACM2076--夹角有多大(题目已修改,注意读题)
杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...
- 杭电ACM2092--整数解
杭电ACM2092--整数解 分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...
随机推荐
- ASP.NET MVC路由配置
一.命名参数规范+匿名对象 routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}" ...
- UVa 1609 (博弈) Foul Play
姑且把它归类为一道博弈吧,毕竟这也是在找必胜方案. 十分有意思的一道题目,设计一种方案让你支持的1队获胜. 题目给出了两个很重要的条件: 1队能打败至少一半的队伍 对于1队不能打败的黑队,一定存在一个 ...
- HDU 4023 (博弈 贪心 模拟) Game
如果硬要说这算是博弈题目的话,那这个博弈是不公平博弈(partizan games),因为双方面对同一个局面做出来的决策是不一样的. 我们平时做的博弈都是公平博弈(impartial games),所 ...
- 解决VS2013调试ASP.NET中无法调试的问题:当前不会命中断点。在 XXXX.dll 中找到了 XXX.cs 的副本,但是当前源代码与 XXXX.dll 中内置的版本不同。
解决思路: 一定是在某个文件夹存在了副本,结果果然不出所料. 当前日期是2016年3月10日,But C:\Windows\Microsoft.NET\Framework\v4.0.30319\Tem ...
- 01.C语言关于结构体的学习笔记
我对于学习的C语言的结构体做一个小的学习总结,总结如下: 结构体:structure 结构体是一种用户自己建立的数据类型,由不同类型数据组成的组合型的数据结构.在其他高级语言中称为记录(record) ...
- linux sed 命令
转载:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行 ...
- hihoCoder #1195 高斯消元·一
题意:便利店老板为了促销,推出了组合包的形式,将不同数量的各类商品打包成一个组合.比如2袋薯片,1听可乐的组合只要5元,而1袋薯片,2听可乐的组合只要4元.通过询问老板知道:一共有N种不同的商品和M种 ...
- JS面向对象组件 -- 继承的其他方式(类式继承、原型继承)
继承的其他形式: •类式继承:利用构造函数(类)继承的方式 •原型继承:借助原型来实现对象继承对象 类 : JS是没有类的概念的 , 把JS中的构造函数看做的类 要做属性和方法继承的时候,要分开继 ...
- bootstrap-datetimepicker时间控件
欢迎各种吐槽. 本人小前端,学习过程中,某日遇到做时间控件的需求,于是无休止的召唤了度娘,发现看不太懂.算是为自己做个笔记,也便于菜鸟级别的看的懂. 首先,我们看看点击选择时间的时候的展示页面吧 年 ...
- A woman without arms
任吉美出生在中国烟台海阳一个极为普通的渔民家里.她先天残疾,没有胳膊和手. 小吉美注定要比别人生活得更艰难.她不能自己穿衣,不能自己端碗吃饭,也不能像兄弟姐妹们一样帮助妈妈干家务活,她觉得自己成了家里 ...