Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 102071    Accepted Submission(s): 27649

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

———————————————————————————————
题目的意思是从地图的S点搜到D点,‘.’为路‘X’为墙,问能否正好在t秒时到达终点。
处理方法是直接DFS搜索,加上剪枝。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std; int m, n, t;
char mp[10][10];
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, -1 }, { 0, 1 } };
bool escape; bool check(int x, int y)
{
if (x < 0 || x >= m||y < 0 || y >= n||mp[x][y] == 'X')
return 0;
return 1;
} void dfs(int si,int sj,int di,int dj,int cnt)
{
if (si == di&&sj == dj&&cnt == t)
{
escape = 1;
return;
}
int tmp = t-cnt-abs(di - si) - abs(dj - sj); if (tmp < 0 || tmp % 2)
return; for (int i = 0; i < 4; i++)
{
int xx = si + dir[i][0];
int yy = sj + dir[i][1];
if (check(xx, yy))
{
mp[xx][yy] = 'X';
dfs(xx, yy, di, dj, cnt + 1);
mp[xx][yy] = '.';
if (escape == 1)
return;
}
}
} int main()
{
int si, sj, di, dj;
while (~scanf("%d%d%d", &m, &n, &t))
{
if (m == 0 || n == 0 || t == 0)
break;
int wall = 0;
for (int i = 0; i < m; i++)
{
scanf("%s", &mp[i]);
for (int j = 0; j < n; j++)
{
if (mp[i][j] == 'S')
{
si = i;
sj = j;
}
if (mp[i][j] == 'D')
{
di = i;
dj = j;
}
if (mp[i][j] == 'X')
wall++;
} }
if (t > m*n - wall)
{
printf("NO\n");
continue;
}
escape = 0;
mp[si][sj] = 'X';
dfs(si, sj, di, dj, 0);
if (escape == 1)
printf("YES\n");
else
printf("NO\n"); }
return 0;
}












Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏的更多相关文章

  1. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  3. HDU1518 Square(DFS) 2016-07-24 15:08 49人阅读 评论(0) 收藏

    Square Problem Description Given a set of sticks of various lengths, is it possible to join them end ...

  4. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  5. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  6. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  7. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  9. hdu2602 Bone Collector(01背包) 2016-05-24 15:37 57人阅读 评论(0) 收藏

    Bone Collector Problem Description Many years ago , in Teddy's hometown there was a man who was call ...

随机推荐

  1. 4C - 七夕节

    七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" 人们纷纷来到告示前,都想知道谁才是自己 ...

  2. (转)JavaScript的压缩

    JavaScript的压缩 (转自)http://blog.csdn.net/ybygjy/article/details/6995435 简述 如果非常着急,这块可以跳过直接从约束条件开始也行. J ...

  3. andorid 列表视图 ListView 之ArrayAdapter

    activity_ui3.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns ...

  4. eclipse中增加多个Tomcat

    一.在eclipse中新增Tomcat,并增加在其上部署的工程 1.打开eclipse,并选择菜单中的 "Window" ---> "Show View" ...

  5. 2019年学Java开发有优势吗?

    随着信息科技的发展,在我们的日程生活和工作中到处充斥和使用着互联网信息技术.事实说明,互联网已经越来越广泛地深入到人们生活的方方面面,Java技术服务市场需求空缺会越来越大.学会一门IT技术,将拥有更 ...

  6. vim窗口切换

    参考资料: http://www.cnblogs.com/litifeng/p/8282479.html 当用vim写代码的时候,我喜欢一边看着头文件中结构的定义,一边编写实现的代码,这样就经常用到多 ...

  7. 请简要介绍Sping MVC、IoC和AOP

    Sping MVC是在Spring框架上发展起来的框架,它提供了构建Web应用程序的全功能MVC模块,使用了Spring可插入的MVC架构,可以自由的选择各个模块所使用的架构,非常灵活.Spring ...

  8. JAVA钩子方法+模板方法

    模板方法: 写一个抽象类,这个抽象类有多个抽象方法,里面设立一个模板方法,这个模板方法也可以称之为模板算法,设立不同方法的执行顺序,封装业务流程,暴露出去: 模板方法模式的特点很好总结,它将一般性的可 ...

  9. [ASP.NET]使用Layer简介

    layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 在与同类组件的比较中,layer总是能轻易获胜.她尽可能 ...

  10. 清幽傲竹实现的kbmMWServer数据库联接失败重联(转载红鱼儿)

    1.修改kbmMWUnidac单元的TkbmMWUNIDACConnection.InternalOpenConnection方法,加上:          //支持unidac重联          ...