杭电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中动态获取数据使用Highcharts图表控件【Copy By Internet】
具体实现的效果如图:
- jQuery_添加与删除元素
一.jQuery添加元素(通过 jQuery,可以很容易地添加新元素/内容.) 1.添加新的 HTML 内容,用于添加新内容的四个 jQuery 方法(都能解析HTML标签): append() - ...
- hdu4638 group 树状数组
连接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:就给给你n个数(大小在1-n里),然后给你连续的可以构成一个块,再给你N个询问,每个询问一个l ...
- BZOJ 4198 荷马史诗
哈夫曼树. 如果要最大的深度最小,再按h排序即可. #include<iostream> #include<cstdio> #include<cstring> #i ...
- jquery ajax GET POST 跨域请求实现
同一段逻辑代码需要在多个网站中使用, 每个网站都新建一个ashx真是扯蛋的作法, 所以想只请求一处的ashx, 这样便于维护和修改, 那么,ajax跨域问题就来了. 废话少说, 直接上代码, 我现 ...
- UVALive 5532 King(差分约束,spfa)
题意:假设一个序列S有n个元素,现在有一堆约束,限制在某些连续子序列之和上,分别有符号>和<.问序列S是否存在?(看题意都看了半小时了!) 注意所给的形式是(a,b,c,d),表示:区间之 ...
- 聊聊Dataguard的三种保护模式实验(上)
Data Guard是Oracle高可用性HA的重要解决方案.针对不同的系统保护需求,DG提供了三种不同类型的保护模式(Protection Mode),分别为:最大保护(Maximum Protec ...
- requirejs之demo (转)
具体的理论就不讲了,可以参考 http://www.ruanyifeng.com/blog/2012/10/javascript_module.html http://www.ruanyifeng.c ...
- Jquery实现文本框输入提示
一些用户体验好的表单都会在文本框里设置输入提示,文本框获取焦点时,提示内容消息,如果未输入,失去焦点时又会出现提示. 网上找到一个比较好用的控件jquery.inputDefault.js 使用方法: ...
- RedHat 5 配置CentOS yum 更新源
YUM是Redhat Linux在线安装更新及软件的工具,但是这是RHEL5的收费功能,如果没有购买Redhat的服务时不能使用RHEL5的更新源的,会提示注册. 由于CentOS是从Redhat演化 ...