HDU1010 Tempter of the Bone(DFS+奇偶剪枝)

题目来源

HDU1010 Tempter of the Bone

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 N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T 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 N ,M, and T(1<N,M<7;0<T<50) 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 N lines give the maze layout, with each line containing M characters. A character is one of the following:

X ′ X ′ : a block of wall, which the doggie cannot enter;
S ′ S ′ : the start point of the doggie;
D ′ D ′ : the Door; or
. ′ . ′ : an empty block.

The input is terminated with three 0s 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,但此题考查点为剪枝,其中还有奇偶剪枝,普通DFS必定超时。
其中剪枝判断主要有两个地方:
第一个地方在进行深度搜索之前,进行一次普通剪枝判断,判断地图中的空地个数是否大于给与的时间,如若不是,直接给出无法到达的答案,就不用进行搜索浪费时间。
第二个就是在深度搜索中,同时进行步数判断和奇偶剪枝。关于奇偶剪枝,如若按照最短路径到达终点,步数是确定的,而在途中每次改变路线,必定是改变的偶数步,所以当最短路径和时间的差值为奇数的时候,就无法到达终点。

#include<iostream> 
#include<cstdio>
#include<stack>
#include<cmath>
#include<cstring>

using namespace std;

typedef struct part{
    int x,y,times;
}part;
char map[8][8];//地图 
int nap[4][2]={0,1,1,0,0,-1,-1,0},m,n,k,tx,ty;
bool flag,vis[8][8];//flag为标志,判断是否刚好准时到达终点
                    //vis为标记数组,标记已经走过的点 

void dfs(int x,int y,int step){
    if(x<0||x>=m||y<0||y>=n)    return;
    if(x==tx&&y==ty){
        if(step==k) flag=true;
        return;
    }
    if(step>k)    return;
    int p=k-step-abs(tx-x)-abs(ty-y); 
    if(p<0||p&1)    return;//第二步剪枝 ,p&1为奇偶剪枝 
    for(int i=0;i<4;i++){ 
        int sx=x+nap[i][0],sy=y+nap[i][1];
        if(map[sx][sy]!='X'&&!vis[sx][sy]){
            vis[sx][sy]=true;
            dfs(sx,sy,step+1);
            vis[sx][sy]=false;
        }
        if(flag) return;
    }
    map[x][y]=0;//回溯法 
}

int main(){
    char c;
    int x,y,wall=0;
    //输入地图并标记起点和终点 
    while(cin>>m>>n>>k,m,n,k){
        for(int i=0;i<m;i++){
            getchar();
            for(int j=0;j<n;j++){
                cin>>map[i][j];
                if(map[i][j]=='S'){
                    x=i;y=j;
                }
                if(map[i][j]=='D'){
                    tx=i;ty=j;
                }
            }
        }
        flag=false;
        if(n*m-wall-1>=k){//第一步剪枝 
            memset(vis,false,sizeof(vis));
            vis[x][y]=true;
            dfs(x,y,0);
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值