HDU1010 Tempter of the Bone(小狗是否能逃生----DFS)

Tempter of the Bone

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 

题目大意:

一扇门只能在第T秒时打开,问小狗是否能在开门时恰好到达这扇门,逃出去。

解题思路:

DFS问题。

奇偶剪枝:

是数据结构的搜索中,剪枝的一种特殊小技巧。

现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,

s
|
|
|
+ e

如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步
数, 记做step,此处step1=8;

  

s
+
| +
|
+ e

如图,为一般情况下非最短路径的任意走法举例,step2=14;

step2-step1=6,偏移路径为6,偶数(易证);

故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;

返回,false;

反之亦反。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10

using namespace std;

bool flag,ans,visited[N][N];
int n,m,t,xe,ye;
char map0[N][N];

void dfs(int x,int y,int timen){
    if(flag) return ;
    if(timen>t) return ;
    if(x<0||x>n-1||y<0||y>m-1) return ;
    if(timen==t&&map0[x][y]=='D') {flag=ans=true;return ;}
    int temp=t-timen-abs(xe-x)-abs(ye-y);
    if(temp&1) return ;//奇偶剪枝,位运算判断是否为奇数,比mod更快.
      if(!visited[x-1][y]&&map0[x-1][y]!='X'){
        visited[x-1][y]=true;
        dfs(x-1,y,timen+1);
        visited[x-1][y]=false;
    }
    if(!visited[x+1][y]&&map0[x+1][y]!='X'){
        visited[x+1][y]=true;
        dfs(x+1,y,timen+1);
        visited[x+1][y]=false;
    }
     if(!visited[x][y-1]&&map0[x][y-1]!='X'){
        visited[x][y-1]=true;
        dfs(x,y-1,timen+1);
        visited[x][y-1]=false;
    }
     if(!visited[x][y+1]&&map0[x][y+1]!='X'){
        visited[x][y+1]=true;
        dfs(x,y+1,timen+1);
        visited[x][y+1]=false;
    }
}

int main(){
    int xs,ys;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n||m||t)){
        int cnt=0;
        getchar();
        memset(visited,false,sizeof(visited));
        flag=ans=false;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>map0[i][j];
                if(map0[i][j]=='S'){
                    xs=i;ys=j;
                    visited[i][j]=true;
                }
                if(map0[i][j]=='D'){
                    xe=i;ye=j;
                }
                if(map0[i][j]=='X'){
                    cnt++;
                }
            }
        }
        if(n*m-cnt-1>=t) dfs(xs,ys,0);
        if(ans) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

HDU1010 Tempter of the Bone(小狗是否能逃生----DFS)

时间: 2024-09-29 20:05:01

HDU1010 Tempter of the Bone(小狗是否能逃生----DFS)的相关文章

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70895    Accepted Submission(s): 19535 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU1010 Tempter of the Bone(回溯 + 剪枝)

题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D’代表门,‘.’代表可行走的地方,小狗每次可以选择往周围的四个方向行走,问这个小狗能否正好T步找到门. 思路: 利用回溯 + 剪枝,这道题剪枝特别重要. 剪枝一: 可以把图看成这样: 1 0 1 0 10 1 0 1 01 0 1 0 10 1 0 1 01 0 1 0 1 则假设从点 a(i + j,横纵坐标之和) 走到点 b(i + j) ,如果 a 和 b 的奇偶性相同,那么从 a 到 b 必须是偶数步

hdu1010 Tempter of the Bone(DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 90716    Accepted Submission(s): 24683 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu1010:Tempter of the Bone

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 转自:http://www.cnblogs.com/zhourongqing/archive/2012/04/28/2475684.html 深度优先搜索,用到了奇偶剪枝,第一次听说..仍然是咸鱼一条 奇偶剪枝: 是数据结构的搜索中,剪枝的一种特殊小技巧. 现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点, s         |         |         |

HDU1010 --- Tempter of the Bone(dfs+剪枝)

小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t秒大门会为你敞开”,而他自己所在的棋盘是大小为 N*M 的长方形,他可以向上下左右四个方向移动(不可走有障碍点).棋盘中有一扇门.根据机关的提示,小明顿时明白了,他和朋友必须在第 t 秒到门口.而这一切,没有回头路!因为一旦他移动了,他刚才所在的点就会消失,并且他不能在一个点上停留超过一秒,不然格子

HDU--1010 Tempter of the Bone(深搜+奇偶剪枝)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 我认为的剪枝就是在本来的代码中加入一些附加条件使之不去进行多余的计算,防止超时 奇偶剪枝的知识链接 AC代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int n,m,t,k,flag,starex,starey,endx,endy

HDU1010 Tempter of the Bone(DFS+剪枝)

1 # include <stdio.h> 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 char map[8][8]; 8 int visit[8][8]; 9 struct node{ 10 int x,y; 11 }; 12 int n,m; 13 node Save,Dog

ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径  或者走全然部可能路径都不满足 注意剪枝  当前位置为(r,c)  终点为(ex,ey) 剩下的时间为lt  当前点到终点的直接距离为  d=(ex-r)+(ey-c)   若多走的时间rt=lt-d<0 或为奇数时  肯定是不可能的  能够自己在纸上画一下 每一个点仅仅能走一次的图  走弯路的话多

Tempter of the Bone —HDU1010(DFS+剪枝)

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