HDU 1010 Tempter of Bone DFS + 奇偶剪枝

奇偶剪枝:

对于从起始点 s 到达 终点 e,走且只走 t 步的可达性问题的一种剪枝策略。

如下列矩阵 :

从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数。反之亦然

所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y - e.y),且 (t&1) == ((abs(s.x - e.x) + abs(s.y - e.y))&1)。

否则必不可达。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long LL
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 1000000007
#define LM(a,b) (((ULL)(a))<<(b))
#define RM(a,b) (((ULL)(a))>>(b))

using namespace std;

char Map[10][10];

bool mark[10][10];

struct Q
{
    int x,y,t;
};

int jx[] = {-1, 0, 1, 0};
int jy[] = { 0,-1, 0, 1};

bool dfs(int sx,int sy,int ans,int ex,int ey,int n,int m,int t)
{
    if(ans == t)
    {
        if(sx == ex && sy == ey)
            return true;
        else
            return false;
    }
    else if(sx == ex && sy == ey)
        return false;

    if(((abs(ex-sx) + abs(ey-sy))&1) != ((t-ans)&1))
        return false;

    for(int i = 0;i < 4 ; ++i)
    {
        int tx = sx+jx[i];
        int ty = sy+jy[i];

        if(1 <= tx && tx <= n && 1 <= ty && ty <= m && Map[tx][ty] != ‘X‘ && mark[tx][ty] == false)
        {
            mark[tx][ty] = true;
            if(dfs(tx,ty,ans+1,ex,ey,n,m,t) == true)
                return true;
            mark[tx][ty] = false;
        }
    }
    return false;
}

void Solve(int n,int m,int t)
{
    int i,j;

    int sx,sy,ex,ey;

    for(i = 1;i<= n; ++i)
    {
        for(j = 1;j <= m; ++j)
        {
            if(Map[i][j] == ‘S‘)
            {
                sx = i,sy = j;
            }
            else if(Map[i][j] == ‘D‘)
            {
                ex = i,ey = j;
            }
        }
    }

    memset(mark,false,sizeof(mark));

    mark[sx][sy] = true;

    if(dfs(sx,sy,0,ex,ey,n,m,t) == true)
        printf("YES\n");
    else
        printf("NO\n");
}

int main()
{
    int n,m,t;

    int i;

    while(scanf("%d %d %d",&n,&m,&t) && (n||m||t))
    {
        for(i = 1;i <= n ; ++i)
        {
            scanf("%s",Map[i]+1);
        }

        Solve(n,m,t);

    }
    return 0;
}

HDU 1010 Tempter of Bone DFS + 奇偶剪枝,布布扣,bubuko.com

时间: 2024-11-03 21:42:13

HDU 1010 Tempter of Bone DFS + 奇偶剪枝的相关文章

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

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

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

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

hdu 1010(迷宫搜索,奇偶剪枝)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 144191    Accepted Submission(s): 38474 Problem Description The doggie fou

hdu1010Tempter of the Bone(dfs+奇偶剪枝)

题目链接:点击打开链接 题目描述:给定一个迷宫,给一个起点和一个终点,问能否恰好经过T步到达终点?每个格子不能重复走 解题思路:dfs+剪枝 剪枝1:奇偶剪枝,判断终点和起点的距离与T的奇偶性是否一致,如果不一致,直接剪掉 剪枝2:如果从当前到终点的至少需要的步数nt加上已经走过的步数ct大于T,即nt+ct>t剪掉 剪枝3:如果迷宫中可以走的格子小于T直接剪掉 启发:剪枝的重要性 代码: #include <cstdio> #include <cstdlib> #inclu

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

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

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