ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
题意 一仅仅狗要逃离迷宫 能够往上下左右4个方向走 每走一步耗时1s 每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次 问狗是否有可能逃离这个迷宫
直接DFS 直道找到满足条件的路径 或者走全然部可能路径都不满足
注意剪枝 当前位置为(r,c) 终点为(ex,ey) 剩下的时间为lt 当前点到终点的直接距离为 d=(ex-r)+(ey-c) 若多走的时间rt=lt-d<0 或为奇数时 肯定是不可能的 能够自己在纸上画一下 每一个点仅仅能走一次的图 走弯路的话多走的步数一定为偶数
#include<cstdio>
#include<cmath>
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = { -1, 1, 0, 0};
const int N = 10;
char mat[N][N];
bool ans;
int t, sx, sy, ex, ey; void dfs(int r, int c, int lt)
{
if(mat[r][c] == 'D' && lt == 0||ans) //满足条件或已经满足条件
{
ans = true;
return;
}
char tc=mat[r][c]; //保存原来的可能值 有'D'和'.'两种情况
mat[r][c] = 'X';
int rt = lt - abs(ex - r) - abs(ey - c); //比直线到达终点多用的时间
if(rt >= 0 && rt % 2 == 0) //剪枝
for(int i = 0; i < 4; ++i) //4个方向走
{
int x = r + dx[i], y = c + dy[i];
if(mat[x][y] == '.' || mat[x][y] == 'D')
dfs(x, y, lt - 1);
}
mat[r][c] = tc; //恢复原状
} int main()
{
int n, m;
while(scanf("%d%d%d", &n, &m, &t), n)
{
for(int i = 1; i <= n; ++i)
scanf("%s", mat[i] + 1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
if(mat[i][j] == 'S') sx = i, sy = j;
if(mat[i][j] == 'D') ex = i, ey = j;
} ans = false;
dfs(sx, sy, t);
printf(ans ? "YES\n" : "NO\n");
}
return 0;
}
Tempter of the Bone
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)的更多相关文章
- ZOJ 2110 Tempter of the Bone
Tempter of the Bone Time Limit: 2 Seconds Memory Limit: 65536 KB The doggie found a bone in an ...
- zoj 2110 Tempter of the Bone (dfs)
Tempter of the Bone Time Limit: 2 Seconds Memory Limit: 65536 KB The doggie found a bone in an ...
- ZOJ 2110 Tempter of the Bone(DFS)
点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...
- 题目1461:Tempter of the bone(深度优先遍历DFS)
题目链接:http://ac.jobdu.com/problem.php?pid=1461 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- Tempter of the Bone HDU 1010(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- 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 ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 【HDU - 1010】Tempter of the Bone(dfs+剪枝)
Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱 ...
随机推荐
- Webbrowser加载Flash后方向键失效问题(用到了OLE接口,没有被处理就转发,够复杂的)
原文:http://blog.csdn.net/dropme/article/details/6253528 窗体上放一个ApplicationEvent控件,OnMessage事件中这么写 uses ...
- cct,web技术
基本信息 全国计算机等级考试二级教程——Web程序设计(2016年版)作 者:教育部考试中心 编出 版 社:高等教育出版社出版时间:20115-12-1 ISBN:9787040442991版 ...
- 在SharePoint 2010中部署RBS (转)
一.RBS(Remote BLOB Storage)简单介绍 在SharePoint的大部分企业应用案例中,SharePoint都是要承担着非常繁重的文件管理工作,这些文件类型包含了Word文档,Ex ...
- Static关键字的作用及使用
1.使用static声明属性 如果希望一个属性被所有对象共同拥有,可以将其声明为static类型. 声明为static类型的属性或方法,此属性或方法也被称为类方法,可以由类名直接调用. class P ...
- Codeforces325-B(二分搜索)
题目:B. Stadium and Games 分析:问题可以转化为下面的等式求解问题: 由于n在10^18范围内,所以k的范围是从0到63即可,这样就可以枚举k,二分m,然后所有符合条件的就是答案了 ...
- codeforces 659B Qualifying Contest
题目链接:http://codeforces.com/problemset/problem/659/B 题意: n个人,m个区.给出n个人的姓名(保证不相同),属于的区域,所得分数.从每个区域中选出成 ...
- JDBC增删改数据库的操作
JDBC入门及简单增删改数据库的操作 一.JDBC的引入 1.JDBC的概念 JDBC:Java Database Connection,表示数据库连接(任何数据库都支持JDBC的连接),是一个独立于 ...
- java自己主动打开包装盒很容易导致两个误区
从J2SE 5.0开始提供基本数据类型的自己主动装箱(autoboxing).拆箱(unboxing)功能. 何为自己主动装箱: 当我们创建一个Integer对象时,却能够这样: Integer i ...
- SecureCRT 连接虚拟机Linux
SecureCRT 连接虚拟机Linux 最近在学习linux,在学习中遇到了一些问题,现总结一下. 虚拟机我用的是VirtualBox,完美支持中文,可以在电脑中创建虚拟机环境,上手非常简单.具 ...
- POJ 2676/2918 数独(dfs)
思路:记录每行每列每一个宫已经出现的数字就可以.数据比較弱 另外POJ 3074 3076 必须用剪枝策略.但实现较麻烦,还是以后学了DLX再来做吧 //Accepted 160K 0MS #incl ...