HDU 1010 Tempter of the Bone 奇偶剪枝 DFS

7 篇文章 0 订阅
  • 题目

  • 题意

根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,

问:在指定的时间,是否能到达'门'的位置.

注意:路不可以重复经过,时间也要刚好是 t ,不能少.

  • 分析

之前用了BFS,是不对的。

因为使用BFS的话,求得的是最短路径(T时间到终点,且是最短路径)。然而,这一题的路径不一定是最短的。

参考博客

  •  代码

我的奇偶剪枝怎么跟别人 不太一样???


//AC
//HDU 1010 using DFS algorithm
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<cstring>
#include<cmath>
using namespace std;

using namespace std;
int nextx[4] = { 0 , 0 , 1 , -1};
int nexty[4] = { 1 , -1 , 0 , 0};
char mp[8][8];
int book[8][8];
int leaststep[8][8];
int N, M , T;
int startx , starty , endx , endy;
int DFS ( int x, int y , int reststep)
{
    //cout<<x<<" "<<y<<" ";
    if ( x == endx && y == endy && reststep == 0)
    {
        //cout<<"Y1";
        return 1;
    }
    book[x][y] = 1 ;
    for ( int i = 0 ; i < 4 ;i++ )
    {
        int nx = x + nextx[i];
        int ny = y + nexty[i];
        //cout<<nx<<" "<<ny<<" ";
        if( book[nx][ny] == 1 )
            continue;
        if (mp[nx][ny] == 'X')
            continue;
        if ( nx > N || nx <= 0 || ny > M || ny <= 0 )
        {
            continue;
        }
        if(reststep == 0 )
            continue;
      //  cout<<nx<<" "<<ny<<" "<<reststep <<endl;
        book[nx][ny] = 1; //前后都有标记

        if (T - reststep - (abs(endx - nx) + abs(endy - ny)) & 1) continue;  //不加会T
                //如果
        if(DFS( nx , ny , reststep - 1))
            return 1;

        book[nx][ny] = 0; //是为了回溯
    }

    return 0;
}

int main()
{

    while(scanf("%d%d%d" , &N , &M , &T) !=EOF)
    {
        if(N == 0 && M == 0 && T == 0)
        {
            break;
        }
        for (int i = 1 ; i <=N ; i++)
        {
            for ( int j =1 ; j <=M ; j++)
            {
                cin>>mp[i][j];
                if( mp[i][j] == 'S')
                {
                    startx = i;
                    starty = j;

                }
                if ( mp[i][j] == 'D')
                {
                    endx = i;
                    endy = j;
                }
            }
        }

        memset(book , 0 ,sizeof(book));
        book[startx][starty] = 1;


        /*
        for (int i =1; i<=M ;i++)
        {
            for( int j =1;j <=N ;j++)
               cout<<leaststep[i][j];
            cout<<endl;
        }*/

        if ( DFS( startx , starty , T))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;


    }
}

/*
if ( x > N || x <= 0 || y > M || y <= 0 )
    {
        cout<<x<<" "<<y<<" ";
        cout<<"N1";
        return 0;
    }
    if (reststep == 0 )
    {
        cout<<x<<" "<<y<<" ";
        cout<<"N2";
        return 0;
    }
    if ( book[x][y] == 1 )
    {
        cout<<x<<" "<<y<<" ";
        cout<<"N3";
        return 0;
    }
    if ( mp[x][y] != '.' || mp[x][y] == 'S')
    {
        cout<<x<<" "<<y<<" ";
        cout<<"N4";
        return 0;
    }
*/

递归,回溯,DFS,BFS的理解和模板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值