hdu 1010 Tempter of the Bone 奇偶剪枝

本文介绍了一种称为奇偶剪枝的技术,在搜索算法中利用起点与终点的坐标奇偶性来减少不必要的搜索路径,提高搜索效率。文章通过具体实例解释了如何应用此技巧,并提供了一段代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先介绍一下什么是奇偶剪枝:

奇偶剪枝

  是数据结构的搜索中,剪枝的一种特殊小技巧。   现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,     


 

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

 


  

如图,为一般情况下非最短路径的任意走法举例,step2=14;   step2-step1=6,偏移路径为6,偶数(易证);   故,

t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;   返回,false;   反之亦反。

下面是我复制的一些网上的资料  没怎么看懂

路径奇偶条件剪枝

仔细观察,从起点到终点的距离的奇偶性决定了所有从起点到终点的可行路径距离的奇偶性!而且这种奇偶相关不随搜索状态的变化而变化。

 

可以把map看成这样:

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代表积数步

从为 的格子走一步,必然走向为 的格子

从为 的格子走一步,必然走向为 的格子

即:

0 ->11->0 不管怎么走 必然是奇数步

0->0 1->1 不管怎么走 必然是偶数步

所以我们可以把其看成最短路径的时候情况 用来判断从一点到另一点是用积数步走完还是偶数步走完

很明显,如果起点在而终点在那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了。

所以通过分析,可以得到下面这一判定条件,网上有好多别的式子,但本质是一样的。

if (abs(P.x-Q.x)+abs(P.y-Q.y)%2==T%2)    dfs(P);//只需判断一次即可

即如果用x y 表示当前所在的迷宫位置时 xx yy表示出口时  t表示规定的要用多少步走出迷宫    count表示已经走过的步数  那么t-count是剩下要走的步数   那么满足下面的式子才能够在t步正好走出迷宫   注易是正好走出哈 

abs(xx-x)+abs(yy-y))%2!=(t-count)%2 

这个剪枝是很强大的、概率上来说可以减掉一半的Case

下面举例题:

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24023    Accepted Submission(s): 6616

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

我的代码:

#include<stdio.h>

#include<string.h>

#include<math.h>

int n,m,t,s_i,s_j,d_i,d_j;

char a[10][10];

int flag[10][10];

int ans,cnt;

void dfs(int i,int j,int ti)

{

if(ans==1) return ;

if(i<=0||j<=0||i>n||j>m) return;

if(ti>t) return;

if(a[i][j]=='X') return;

//if(t-ti<abs(d_i-i)+abs(d_j-j)) return ;//剩余最短路径所需时间如果比剩余时间大 则return

//上面那句话加上可以更加缩短时间

if(flag[i][j]==1) return;

if(t==ti&&i==d_i&&j==d_j) {ans=1;return;}

if(abs(t-ti)%2!=(abs(d_i-i)+abs(d_j-j))%2) return;//奇偶剪枝 如果少了这句就会超时 所以

//这句话的作用很大 要好好体会

flag[i][j]=1;

dfs(i-1,j,ti+1);

dfs(i+1,j,ti+1);

dfs(i,j+1,ti+1);

dfs(i,j-1,ti+1);

flag[i][j]=0;

}

int main()

{

int i,j;

while(scanf("%d %d %d",&n,&m,&t))

{

getchar();

cnt=0;

if(n==0&&m==0&&t==0) return 0;

for(i=1;i<=n;i++)

{

for(j=1;j<=m;j++)

{

scanf("%c",&a[i][j]);

if(a[i][j]=='S') {s_i=i;s_j=j;}

else

if(a[i][j]=='D') {d_i=i;d_j=j;cnt++;}//一开始这里的cnt++忘了加了导致错了//好多次 浪费了好多时间    所以以后做题小细节一定要注意到 不要忽视任何一个小地

//

else if(a[i][j]=='.') cnt++;

}

    getchar();

}

if(cnt<t) {printf("NO\n");continue;}

memset(flag,0,sizeof(flag));

ans=0;

        dfs(s_i,s_j,0);

if(ans==1) printf("YES\n");

else printf("NO\n");

}

return 0;

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值