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 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

Author

ZHANG, Zheng

题目意思:

给你一个图,给定起点和终点

问你能不能恰好在k步内到达终点

X:不能走

.:可以走

S:起点

D:终点

分析:

路不能重复走,时间要恰好是k

不能用BFS,因为bfs求的是最短路,而这个题最短路不一定符合要求

得用dfs

先说一下需要用到的剪枝:

1.如果当前步数大于等于k且还没有到D点,则剪掉

2.最短距离都大于k的直接输出no

3.奇偶剪枝

涉及到奇偶剪枝(说一下自己的理解):

把矩阵看成如下形式: 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
从为 0 的格子走一步,必然走向为 1 的格子 。
从为 1 的格子走一步,必然走向为 0 的格子 。
即: 
从 0 走向 1 必然是奇数步,从 0 走向 0 必然是偶数步。

所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!

code:

#include<bits/stdc++.h>
char s[10][10];
int ax,ay,bx,by,n,m,k;
int t[4][2]={1,0,-1,0,0,1,0,-1};//方向引导数组
int vist[10][10],flag;
void dfs(int x,int y,int c)
{
    int i,mx,my;
    if(x==bx&&y==by)//找到终点
    {
        if(k==c)//恰好在规定时间找到终点则标志位置1
            flag=1;
        return;
    }
    if(c>=k)//超出规定时间,剪掉
        return;
    if(s[x][y]!=‘X‘)//可走点
    {
        for(i=0;i<4;i++)
        {
            mx=x+t[i][0];
            my=y+t[i][1];
            if(s[mx][my]!=‘X‘&&mx>=1&&mx<=n&&my>=1&&my<=m&&!vist[mx][my])//判断能不能往这个方向走
            {
                vist[mx][my]=1;
                dfs(mx,my,c+1);
                vist[mx][my]=0;//回退
                if(flag)                       //注意,在找到了目标之后,就不需要再找!以往编写dfs时,没有注意这点
                    return;
            }
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&k)>0&&(n+m+k))
    {
        int i,c;
        for(i=1;i<=n;i++)
        {
            getchar();
            for(int j=1;j<=m;j++)
            {
                scanf("%c",&s[i][j]);
                if(s[i][j]==‘S‘)
                {
                    ax=i;//起点
                    ay=j;
                }
                if(s[i][j]==‘D‘)
                {
                    bx=i;//终点
                    by=j;
                }
            }
        }
        getchar();
        memset(vist,0,sizeof(vist));
        if(abs(ax-bx)+abs(ay-by)>k||(ax+bx+ay+by+k)%2==1) // 最短距离都大于k的剪枝和奇偶剪枝
        {
            printf("NO\n");
            continue;
        }
        vist[ax][ay]=1;
        flag=0;
        c=0;
        dfs(ax,ay,c);
        if(flag==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/yinbiao/p/9313239.html

时间: 2024-10-01 07:11:38

hdu 1010(迷宫搜索,奇偶剪枝)的相关文章

HDU 1010 深搜+奇偶剪枝

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010 贴个资料: http://acm.hdu.edu.cn/forum/read.php?tid=6158 奇偶剪枝: 对于从起始点 s 到达终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y -

HDU 1010 (搜索+奇偶剪枝)

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep>t剪枝 ②搜到一个解后剪枝 ③当前走到终点最少步数>满足条件还需要走的步数剪枝(关键) ③奇偶剪枝(关键):当前走到终点步数的奇偶性应该与满足条件还需要走的步数奇偶性一致. 其中三四两步放在一步中写:remain=abs(x-ex)+abs(y-ey)-abs(dep-t) 奇偶剪枝的原理:abs(

hdu 1010 dfs搜索

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

HDOJ1010-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 despe

HDU 1010 Tempter of the Bone 骨头诱惑(AC代码)DFS搜索+剪枝法

参考了别人的思路:将迷宫外围四面都筑墙‘X’.方便减少代码量. 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <math.h> 5 using namespace std; 6 vector<string> v; 7 int n,m; 8 int x_1,y_1,x_2,y_2; 9 bool maze(int x,int y,int t) //目

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

HDOJ-ACM1010(JAVA) 奇偶剪枝法 迷宫搜索

转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5568822.html 第一次遇到迷宫搜索,给我的感觉是十分惊喜的:搞懂这个的话,感觉自己又掌握了一项技能~ 个人感觉,角色扮演类的游戏,起码都可以在迷宫搜索上找到影子. 奇偶剪枝这个算法感觉十分开阔的视野~这样去描述这个具体问题实在太形象生动了~ 总而言之,十分有趣. 仔细的人会发现  每当设计到移动,我们必须想到上下左右,这也让我们看到这类算法的思路. 在学习迷宫搜索当中,我发现:这个搜索算法是先分析 找到

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> #inclu

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