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  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 ( < N, M < ;  < T < ), 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 '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
S.X.
..X.
..XD
.... S.X.
..X.
...D
 
Sample Output
NO
YES
 
Author
ZHANG, Zheng
 
Source
 

 很久以前做过的一道题目,最近有学弟来问,所以又重新做了一遍。

题目要求的是 刚好在T时间走完了到达门的位置,并不是求最短到达的时间,所以不能用bfs。用dfs暴力深搜搞定。

此题要vis回溯,还要有两个剪枝。其中最重要的一个剪枝是奇偶剪枝,没了这个直接超时。如下所示:

                   if((T%2)!=(abs(s.x-ed.x)+abs(s.y-ed.y))%2) return;//奇偶剪枝,很重要!!!

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 10
#define inf 1e12
int n,m,t;
char mp[N][N];
int vis[N][N];
struct Node{
int x,y;
}st,ed;
int flag;
int dirx[]={-,,,};
int diry[]={,,-,};
void dfs(Node s,int T){ if(flag) return;
if(abs(s.x-ed.x) + abs(s.y-ed.y)>T) return;
//if((T-abs(s.x-ed.x) - abs(s.y-ed.y))%2 ) return;//奇偶剪枝,很重要!!!
if((T%)!=(abs(s.x-ed.x)+abs(s.y-ed.y))%) return;//奇偶剪枝,很重要!!!
if(T==){
if(s.x==ed.x && s.y==ed.y){
flag=;
printf("YES\n");
return;
}
}
Node tmp;
for(int i=;i<;i++){
tmp.x=s.x+dirx[i];
tmp.y=s.y+diry[i];
if(tmp.x< || tmp.x>=n || tmp.y< || tmp.y>=m ) continue;
if(vis[tmp.x][tmp.y]) continue;
if(mp[tmp.x][tmp.y]=='X') continue;
vis[tmp.x][tmp.y]=;
dfs(tmp,T-);
vis[tmp.x][tmp.y]=;
} }
int main()
{
while(scanf("%d%d%d",&n,&m,&t)==){
if(n== && m== && t==){
break;
}
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='S'){
st.x=i;
st.y=j;
}
if(mp[i][j]=='D'){
ed.x=i;
ed.y=j;
}
}
}
memset(vis,,sizeof(vis));
vis[st.x][st.y]=;
flag=;
dfs(st,t);
if(flag==){
printf("NO\n");
}
}
return ;
}

hdu 1010 Tempter of the Bone(dfs暴力)的更多相关文章

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

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

  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+奇偶剪枝)

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

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

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

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

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

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

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

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

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

  8. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

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

随机推荐

  1. 剑指offer-面试题12.打印1到最大的n位数

    题目:输入数字n,按照打印出从1最大的n位10进制数.比如3,则 打印出1.2.3一直到最大的3位数即999 1.你觉得如果面试会有这么简单的题,那 只能说明你---太天真. 2.n=3尚可,如果n= ...

  2. LeeCode-Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  3. python3 module中__init__.py的需要注意的地方

    网上关于__init__.py的作用的资料到处都是,我在此就不再啰嗦哪些了. 若有需要.请各位看官去搜搜即可. 最近刚开始用Python3 就遇到了这个比较有意思的事情 闲言少叙,下面要介绍的是pyt ...

  4. Linux下 输入 env 而得到的环境变量解读

    HOSTNAME=Master.Hadoop MAHOUT_HOME=/usr/hadoop/mahout-distribution-0.8 TERM=linux SHELL=/bin/bash HA ...

  5. PHP学习笔记六【方法-递归】

    <?php //递归 global $n;//定义全局变量 function abc($n) { if($n>2) { abc(--$n); } echo '$n='.$n.'<br ...

  6. android spinner 每行字体颜色都变化

    final static int[] COLOR_LIST={Color.WHITE,Color.WHITE,Color.GRAY,Color.YELLOW,Color.RED}; spinner=( ...

  7. gridview获取当前行索引的方法

    在用GridView控件时,我们经常会碰到获取当前行的索引,通过索引进行许多操作.例如,可以获得当前行某一个控件元素:设置某一元素的值等等. 下面结合实例介绍几种获得GridView当前行索引值的方法 ...

  8. 在IE中调试Javascript

    不管我们写代码的时候如何小心,都不可能完全避免程序中出现bug,这个时侯就需要我们在调试的时候找出错误,修改代码. Javascript是一门灵活的语言,灵活的语法和它解释执行的特性,使得Javasc ...

  9. JDBC之一:JDBC快速入门

    (1)下载Oracle的JDBC驱动,一般放在$ORACLE_HOME/jdbc/lib目录,关于驱动的版本请见: http://elf8848.iteye.com/blog/811037 随Orac ...

  10. Android studio 配置JNI环境

    Android studio配置jni开发环境,主要配置是两个build文件,以及新建一个jni文件,放c代码. 代码如下1: apply plugin: 'com.android.model.app ...