hdu 1010 Tempter of the Bone(DFS)

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


算是第一道真正的搜索题吧,用到了剪枝

下面是奇偶剪枝的介绍(谢谢大牛的解释)

转自:http://blog.csdn.net/iaccepted/article/details/22997299

描述

奇偶剪枝是数据结构的搜索中,剪枝的一种特殊小技巧。

现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,

s

|

|

|

+

e

如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8

s

+

|

+

|

+

e

如图,为一般情况下非最短路径的任意走法举例,step2=14

step2-step1=6,偏移路径为6,偶数(易证);

2结论

推广之,若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达;

返回false

反之亦反。

3原理补充

还是以这个为例子吧,现在我把矩阵填满 0 1 [1]

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

我们现假设从 0 开始走,则不难证明,

从任意 0 走到任意 1 始终是奇数步;

从任意 0 走到任意 0 始终是偶数步;

引用描述里的例子 s e 的最短步数为 t (当然你也可以理解成此时到终点刚好剩余 t 步等等)

则,我们从 s e 的步数之和(或者说总距离)总可以表示成 sum= t + extra ( extra>=0 ),其中 extra 表示额外的步数。[2]

比如例子里面的,做例1

s

+

|

+

|

+

e

此时 t=8sum=14,所以我们容易得到 extra=6。也就是说按照这个走法,需要在最短的步数上再走额外的 6 (先不用太在意这些偏移是在什么地方产生的)

在来一个例2吧,

s

+

|

+

|

+

e

+

此时,t=7sum=15,所以我们也容易得到 extra=8

根据理科生的天性,由这两个一般性的例子,我们很容易嗅察到 extra 都为偶数。先带着疑惑,再来看我给的 0 1 矩阵。

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

1

0

设左上角坐标为(11),右下角坐标为(55.

那么我们给的例1

起点 s 的坐标为(11),此点为“0”

终点 e 为(55),此点为“0”

所以t=8,为偶数。

现在我们再倒过来看,从终点(也就是 e )出发,把最短步数 t=8 耗费掉,不妨这样走,

s

+

+

|

+

e

如图所示从 e 55)耗费 8 步走到了(15)点。

因为是从 0 走偶数步,所以走到的坐标也一定是 0 ,就像这里的(15)点是 0 一样。

又因为最短步数已经耗费掉了,所以不管怎样,从(15)再走回到起点 s 所用的步数总是最开始从起点 s 走到终点 e 所花的某一个额外步数 extra

注意到,(15)点和起点 s 11)都是 0,也就是说,这个 extra 必然是偶数!

再看例2,同样从终点 e 开始耗费 t=7 步,

则所到的点一定是 0 (不管她在哪里),再从这个点回到起点 s ,所用的 extra 也必然是个偶数!

所以无论如何,sum= t + extra ( extra>=0 ) 中的 extra 都是一个偶数

那么我们就可以用公式 t-[abs(ex-sx)+abs(ey-sy)] 计算出extra是否为偶数来判断当前点能否恰好在这么多步到达终点了。

 

 

由此的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max_size 10
char ch[max_size][max_size];
int start_i, start_j, index_i, index_j, N, M, T, biaoji;
int d[4][2]={0,1,1,0,0,-1,-1,0};
void dfs(int x, int y, int num) 
{
    if(biaoji)
        return;
    if(!num) 
	{
        if(x == index_i && y == index_j)
            biaoji = 1;
        return;
    }
    if(num < abs(index_i - x) + abs(index_j - y)) //精细步数剪枝
        return ;
    for(int i = 0; i < 4; i++) 
	{  
        int nx = x + d[i][0],ny = y + d[i][1];  
        if(!ch[nx][ny])    
		{//边界检查
            ch[nx][ny] = 1;             
            dfs(nx, ny, num - 1);  
            ch[nx][ny] = 0;           //待思考  
        }   
    }   
    return ;
}
int main() 
{
    char str[10];
    while(scanf("%d%d%d", &N, &M, &T) && N || M || T) 
	{
        int wall = 0;
        memset(ch, 1, sizeof(ch));
        for (int i = 1; i <= N; i++)  
		{  
            scanf("%s", str);
            for (int j = 1;j <= M; j++)  
			{  
                ch[i][j] = str[j - 1];     
                if(ch[i][j] == 'S')
                    start_i = i,start_j = j,ch[i][j] = 1;
                else if(ch[i][j] == 'D') 
					index_i = i, index_j = j, ch[i][j] = 0;
                else if(ch[i][j] == 'X') 
					wall++,ch[i][j] = 1;
                else
                    ch[i][j] = 0;
            }  
        }
        if(N * M - wall <= T || (abs(index_i - start_i) + abs(index_j - start_j) + T) % 2) 
		{ //粗略步数剪枝, 奇偶剪枝
            printf("NO\n");
            continue;
        }
        biaoji = 0;
        dfs(start_i, start_j, T);
        if(biaoji)
            printf("YES\n");
        else
            printf("NO\n");
    }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值