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. UESTC_Sea Base Exploration CDOJ 409

    When the scientists explore the sea base, they use a kind of auto mobile robot, which has the missio ...

  2. apache http配置https

    <一,Lamp系统搭建> yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-co ...

  3. 找不到eth0,但能找到eth1的问题解决办法

    故障现象:Linux的网卡由eth0变成了eth1,如何修复?解决方案:在linux中,udev记录网络规则的脚本为:/etc/udev/rules.d/70-persistent-net.rules ...

  4. UISearchBar 点击X 按钮收键盘

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{ NSLog(@"textD ...

  5. IOS开发之---触摸和手势

    Touch:在与设备的多点触摸屏交互时生成. 响应者对象 响应者对象就是可以响应事件并对事件作出处理.在iOS中,存在UIResponder类,它定义了响应者对象的所有方法.UIApplication ...

  6. JNI_最简单的Java调用C/C++代码

    JNI_最简单的Java调用C/C++代码 JNI.是Java Native Interface的简称,中文是"Java本地调用".通过这种技术能够做到下面两点: Java程序中的 ...

  7. 用Jfree实现条形柱状图表,java代码实现

    用Jfree实现条形柱状图表,java代码实现.可经经常使用于报表的制作,代码自己主动生成后能够自由查看.能够自由配置图表的各个属性,用来达到自己的要求和目的 package test1; impor ...

  8. asp.net中Repeart选中整行操作

    <asp:Repeater runat="server" ID="rpt_Student"> <HeaderTemplate> < ...

  9. dojo.io.script

    dojo.io.script 定义: 跨域访问数据,可以动态的将script标签插入到网页当中. 局限: 1.只支持get方式访问: 2.只支持异步调用. 使用: 1.dojo.io.script.g ...

  10. 单链表(Single Linked List)

    链表的结点结构  ┌───┬───┐  │data|next│  └───┴───┘ data域--存放结点值的数据域 next域--存放结点的直接后继的地址(位置)的指针域(链域) 实例:从终端输入 ...