深搜--Tempter of the Bone hdu1010

这道题磨了我一个下午,虽然在大神帮助下完成,要注意的东西太多,之前还用了宽搜,那是每看懂题目意思,宽搜是找到一条最短路,而这道题不一定是最短路,要在规定的时间到达,换句话说就是有规定的步数,走了的不能再走。

写题中遇到以下几个问题:

1.对于深搜还是没有完全理解,由于是递归,所以每次的函数体应该都是一样的,之前把不该改变的起点步数清零放在递归函数里,导致错误。

2.对于我之前有点画蛇添足,我又加了个结构体,存迷宫每个点的横纵坐标和信息,然后我在递归结束条件那里直接判断的,但是我在赋值又没有对每个点赋值,导致出错,太粗心了。看下面的错误!!

typedef struct dd{
    int a,b;
    char sr;
}dd;

bool dfs(dd t,int dis){
    if((t.sr==‘D‘)&&(dis==ti))
        return true;

for(int i=0;i<4;i++){
  
         s.a=t.a+x[i];
        s.b=t.b+y[i];//少了一个s.sr=maze[s.a][s.b],太粗心拉
}

  

 3.这题的特殊是在递归时返回上层应返回可不可行,而不仅仅是继续搜下去,所以有如下:

 if(dfs(s,dis+1))
                return true;

  

 4.全部完成了以后还是超时,要剪枝!由曼哈顿距离,总的耗时=曼哈顿距离+多余的路,并且多余的路一定是个偶数,否则不能到达。曼哈顿距离=|起点横坐标-终点横坐标|+|起点纵坐标-终点纵坐标|。然后这题就好啦><

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
int x[4]={-1,0,0,1};
int y[4]={0,-1,1,0};
char maze[10][10];
int vis[10][10];
int ti,n,m;
typedef struct dd{
    int a,b;
    char sr;
}dd;
dd st,en;
bool dfs(dd t,int dis){
    if((maze[t.a][t.b]==‘D‘)&&(dis==ti))//之前在这犯错误,改了一下
        return true;
    if(dis>ti)
        return false;
    dd s;
    for(int i=0;i<4;i++){
        s.a=t.a+x[i];
        s.b=t.b+y[i];
        if(s.a>=0&&s.a<n&&s.b>=0&&s.b<m&&maze[s.a][s.b]!=‘X‘&&!vis[s.a][s.b]){
            vis[s.a][s.b]=1;
            if(dfs(s,dis+1))
                return true;
            vis[s.a][s.b]=0;
        }
    }
    return false;
}
int main(){
    while(scanf("%d%d%d",&n,&m,&ti)!=EOF){
        if(n==0&&m==0&&ti==0)
            break;
        getchar();
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
                scanf("%c",&maze[i][j]);
            getchar();
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(maze[i][j]==‘S‘){
                    st.sr=maze[i][j];
                    st.a=i;
                    st.b=j;
                }
                else if(maze[i][j]==‘D‘){
                    en.a=i;
                    en.b=j;
                }
            }
        }
        vis[st.a][st.b]=1;
        if(((ti-(st.a-en.a+st.b-en.b))%2==0)&&dfs(st,0))//剪枝,否则超时
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

  

时间: 2024-08-04 03:36:17

深搜--Tempter of the Bone hdu1010的相关文章

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

【深搜加剪枝五】HDU 1010 Tempter of the Bone

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

HDU - 1010 Tempter of the Bone 深搜模板题(DPS)解题报告

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

HDU 1010 Tempter of the Bone(深搜)

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

hdu 1010 Tempter of the Bone 深搜+剪枝

题意:在一个坐标内,给定起点和终点,问能否恰好在t时刻到达终点. 以前很少写搜索题,所以看到这个题,就按照普通的深搜写了一下,交上去超时了.后来在网上搜了一下才知道,要剪枝才行.可是,我以前从没写过剪枝,不知道怎么剪,就按照别人的思路往下想.看懂以后,我对剪枝的理解是:对于一些没有必要继续搜索的路径,不再往下深搜,提前返回到上一层.花了半天时间调试代码,终于AC了. 奇偶剪枝:根据题目,doggie必须在第t秒到达门口.也就是需要走t-1步.设doggie开始的位置为(sx,sy),目标位置为(

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): 69699    Accepted Submission(s): 19176 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

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(小狗是否能逃生----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 doggi

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 或为奇数时  肯定是不可能的  能够自己在纸上画一下 每一个点仅仅能走一次的图  走弯路的话多