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. C#设计模式-1简单工厂模式Simple Factory)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 简单的工 ...

  2. C++中强制类型转换

    C++强制类型转换 C++中的强制类型转换虽然兼容C语言中的强制类型转换.但是并不建议在C++中使用C语言风格的强制类型转换.C++中的强制类型转换共有4个关键字分别是:static_cast,con ...

  3. window 安装gdal和python

    进入 http://www.gisinternals.com/release.php 中下载下图(也可以不是这个版本但是下载的python和gdal一定要版本对应) 1.点击下图中release-17 ...

  4. MySQL Workbench常用快捷键

    执行当前SQL语句(即鼠标所在的SQL语句,以 ; 结尾) Ctrl + Enter 执行选中的SQL语句(或执行所有SQL语句) Ctrl + Shift + Enter

  5. 把dataset对象转换成list集合方法

    public static List<T> GetList<T>(DataTable table) where T:new() { List<T> list = n ...

  6. vb中去掉string数组的一部分

    今天碰到一个问题,登陆的时候,如果不需要验证手机号为空,则不去验证手机号 因为登陆的时候所有的验证信息都存放在一个数组里 Dim CheckUserInfo() As String ={UserBir ...

  7. Python内置的subprocess.Popen对象

    具体内容参见:https://docs.python.org/3/library/subprocess.html 大概来说,就是可以对应输入的命令产生一个进程,该进程实例内置如下方法. |  comm ...

  8. 【Redis】Redis cluster集群搭建

    Redis集群基本介绍 Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施installation. Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行 ...

  9. python下使用opencv拍照

    首先在命令中安装opencv: pip install opencv-python 然后打开notebook: jupyter notebook 建立文件,写入如下代码: import cv2 cap ...

  10. 如何将spring boot项目打包成war包

    一.修改打包形式 在pom.xml里设置 <packaging>war</packaging> 二.移除嵌入式tomcat插件 在pom.xml里找到spring-boot-s ...