Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 146511    Accepted Submission(s): 39059

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

题意

给出一个N*M的矩阵和一个时间T,问能不能在时间恰好为T的时候从S走到D。X不能走'.'可以走,每次走过之后'.'就会消失。

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e2+10;
using namespace std;
char ch[maxn][maxn];
int vis[maxn][maxn];
int ans;
int xx,yy;
int T,n,m;
int flag;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void dfs(int x,int y,int t)
{
vis[x][y]=1;
//如果正好在T时刻走到D
if(t==T&&ch[x][y]=='D')
{
flag++;
return ;
}
//奇偶剪枝,如果相差的时间和相差的曼哈顿距离的奇偶性不同,则一定无法到达
//或者相差的时间小于曼达顿距离也不行
//PS:不剪枝会超时
int res=T-t-abs(xx-x)-abs(yy-y);
if(res<0||res%2)
return ;
for(int i=0;i<4;i++)
{
int dx=x+dir[i][0];
int dy=y+dir[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]!='X'&&vis[dx][dy]==0)
{
dfs(dx,dy,t+1);
//如果能够走到D,就可以结束了,不需要回溯
if(flag)
return ;
// 回溯
vis[dx][dy]=0;
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin>>n>>m>>T&&T&&n&&m)
{
ms(vis);
flag=0;
ans=0;
int x,y;
for(int i=0;i<n;i++)
cin>>ch[i];
// 记录开始和结束的位置
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(ch[i][j]=='S')
{x=i;y=j;}
if(ch[i][j]=='D')
{xx=i;yy=j;}
} dfs(x,y,0);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

HDU 1010:Tempter of the Bone(DFS+奇偶剪枝+回溯)的更多相关文章

  1. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  2. 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 ...

  3. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

  4. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  5. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

  6. hdu 1010 Tempter of the Bone 深搜+剪枝

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

  7. Tempter of the Bone(dfs奇偶剪枝)

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

  8. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  9. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  10. (step4.3.1) hdu 1010(Tempter of the Bone——DFS)

    题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...

随机推荐

  1. Codeforces 847E - Packmen

    847E - Packmen 思路:二分时间. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long ...

  2. 寻找重复的子树 Find Duplicate Subtrees

    2018-07-29 17:42:29 问题描述: 问题求解: 本题是要求寻找一棵树中的重复子树,问题的难点在于如何在遍历的时候对之前遍历过的子树进行描述和保存. 这里就需要使用之前使用过的二叉树序列 ...

  3. 移动开发基础和Dalvik VM

    移动开发基础和Dalvik VM 1.基础 移动开发:是指以PDA.UMPC等便携终端为基础进行的开发工作. PDA:Person Digital Assistant 个人数码助理 UMPC:Ultr ...

  4. YII第三步,日志开启

    YII第三步,日志开启 index.php入口文件配置: defined('YII_DEBUG') or define('YII_DEBUG',true); cofig/main.php 'prelo ...

  5. Lightoj Halloween Costumes

    题意:给出要n个时间穿的服装.服装脱下就不能再穿.问最少要准备多少? dp[i][j]表示i到j之间最少花费.如果n=1(n指长度),肯定结果为1,n=2时,也很好算.然后n=3的时候dp[i][j] ...

  6. AC自动机技巧

    AC自动机技巧 可以用树上的一些算法来进行优化 对于要求支持插入和删除字符串的题目,可以通过建两个AC自动机,查询的时候作差来实现. 当给出的查询串是一个含有空格的文本时,可以用特殊字符(比如'z'+ ...

  7. 『PyTorch』第十四弹_torch.nn.Module类属性

    nn.Module基类的构造函数: def __init__(self): self._parameters = OrderedDict() self._modules = OrderedDict() ...

  8. thinkphp关于时间加减几天

    1.当前时间,往后退5天: date('Y-m-d H:i:s',strtotime('-1 days')); 2.有固定时间,往后面退一天或者七天,或者30天: 比如时间:$time = 2014- ...

  9. SQL2005 安装问题

    1. 单击“开始”,依次指向“程序”.“Microsoft SQL Server 2005”和“配置工具”,然后单击“SQL Server 外围应用配置器”. 2. 在“SQL Server 2005 ...

  10. ubuntu下没有Language Support

    sudo apt-get installlanguage-selector-gnome