学习链接:http://www.ihypo.net/1554.html

https://www.slyar.com/blog/depth-first-search-even-odd-pruning.html

http://blog.csdn.net/chyshnu/article/details/6171758

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

题解:刚开始写直接超时,还没学剪枝,奇偶剪枝...

关于奇偶剪枝

首先举个例子,有如下4*4的迷宫,'.'为可走路段,'X'为障碍不可通过

S...
....
....
...D

从S到D的最短距离为两点横坐标差的绝对值+两点纵坐标差的绝对值 = abs(Sx - Dx) + abs(Sy - Dy) = 6,这个应该是显而易见的。

遇到有障碍的时候呢

S.XX
X.XX
...X
...D

你会发现不管你怎么绕路,最后从S到达D的距离都是最短距离+一个偶数,这个是可以证明的

而我们知道:

奇数 + 偶数 = 奇数
偶数 + 偶数 = 偶数

因此不管有多少障碍,不管绕多少路,只要能到达目的地,走过的距离必然是跟最短距离的奇偶性是一致的。

所以如果我们知道从S到D的最短距离为奇数,那么当且仅当给定的步数T为奇数时,才有可能走到。如果给定的T的奇偶性与最短距离的奇偶性不一致,那么我们就可以直接判定这条路线永远不可达了。

这里还有个小技巧,我们可以使用按位与运算来简化奇偶性的判断。我们知道1的二进制是1,而奇数的二进制最后一位一定是1,而偶数的二进制最后一位一定是0。所以如果数字&1的结果为1,那么数字为奇数,反之为偶数。

代码:

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; char map[][];
int flag, Xnum, Sx, Sy, Dx, Dy;
int n, m, t;
int dx[] = {,,,-};
int dy[] = {,-,,}; void DFS(int x, int y, int time)
{
if (x<= || x>n || y<= || y>m) return;
if (flag == ) return;
if (x == Dx && y == Dy && time == t)
{
if(time == t)
flag = ;
return;
} int temp=t-time-abs(x-Dx)-abs(y-Dy);
if(temp<||temp%) return ; for (int i = ; i<; i++)
{
int x1 = x + dx[i];
int y1 = y + dy[i];
if (map[x1][y1] != 'X')
{
map[x1][y1] = 'X';
DFS(x1, y1, time + );
map[x1][y1] = '.';
}
}
return;
} int main()
{
while (cin>>n>>m>>t)
{
if(n==&&m==&&t==) break;
Xnum = ;
for (int i = ; i<=n; i++)
{
for (int j = ; j<=m; j++)
{
cin>>map[i][j];
if (map[i][j] == 'S')
{
Sx = i;
Sy = j;
}
if (map[i][j] == 'D')
{
Dx = i;
Dy = j;
}
if (map[i][j] == 'X')
Xnum ++;
}
}
flag = ;
map[Sx][Sy] = 'X'; if(n*m-Xnum<=t){
cout<<"NO"<<endl;
continue;
}
DFS(Sx, Sy, );
if (flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}

hdu Tempter of the Bone (奇偶剪枝)的更多相关文章

  1. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

  2. HDU 1010Tempter of the Bone(奇偶剪枝回溯dfs)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  4. hdu1010 Tempter of the Bone---DFS+奇偶剪枝

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过 ...

  5. TZOJ 1221 Tempter of the Bone(回溯+剪枝)

    描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...

  6. Tempter of the Bone dfs+剪枝

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  7. B - Tempter of the Bone(DFS+剪枝)

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  8. hdu Tempter of the Bone

    很典型的dfs题,但是涉及到很多的剪枝 . 奇偶剪枝: 是数据结构的搜索中,剪枝的一种特殊小技巧. 现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点, s         |   ...

  9. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

随机推荐

  1. HDU 4284 状压dp+spfa

    题意: 给定n个点 m条无向边 d元. 以下m行表示每条边 u<=>v 以及花费 w 以下top 以下top行 num c d 表示点标为num的城市 工资为c 健康证价格为d 目标是经过 ...

  2. Codeforces Round #258 (Div. 2) B. Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...

  3. hdu1052 Tian Ji -- The Horse Racing 馋

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...

  4. 使用X264编码yuv格式的视频帧使用ffmpeg解码h264视频帧

    前面一篇博客介绍在centos上搭建点击打开链接ffmpeg及x264开发环境.以下就来问个样例: 1.利用x264库将YUV格式视频文件编码为h264格式视频文件 2.利用ffmpeh库将h264格 ...

  5. PHP安装mcrypt.so报错 mcrypt.h not found 的解决的方法

    报错内容:configure: error: mcrypt.h not found. Please reinstall libmcrypt 网上搜索了非常多,包含自带的 yum install lib ...

  6. Directx11学习笔记【十二】 画一个旋转的彩色立方体

    上一次我们学习了如何画一个2D三角形,现在让我们进一步学习如何画一个旋转的彩色立方体吧. 具体流程同画三角形类似,因此不再给出完整代码了,不同的部分会再说明. 由于我们要画彩色的立方体,所以顶点结构体 ...

  7. git bash 出现vim的时候怎么退出

    如果是输出状态,首先Esc退出输入状态,然后Shift+;,再输入q!或wq!(不保存改动,wq!是保存文件的写入修改)退出

  8. 综合第一篇文章(带钩Quora)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDc4MzAyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  9. Could not drop object &#39;student&#39; because it is referenced by a FOREIGN KEY constraint

    1. Find foreign keys SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student' ...

  10. Swing中耗时任务需要另起新线程,这个新线程中更新GUI的操作仍需由EDT来做(转)

    最近调试程序时发现,点击某个界面时会出现卡死的情况,出现的频率还是比较频繁的. 再次出现卡死的情况后,利用jvisualvm查看线程的运行情况,dump操作之后发现线程间出现了死锁:Found one ...