剪枝是什么,简单的说就是把不可行的一些情况剪掉,例如走迷宫时运用回溯法,遇到死胡同时回溯,造成程序运行时间长。剪枝的概念,其实就跟走迷宫避开死胡同差不多。若我们把搜索的过程看成是对一棵树的遍历,那么剪枝顾名思义,就是将树中的一些“死胡同”,不能到达我们需要的解的枝条“剪”掉,以减少搜索的时间。

这里介绍一下奇偶剪枝

什么是奇偶剪枝?

部分内容来自https://blog.csdn.net/chyshnu/article/details/6171758

把矩阵看成如下形式: 
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 但是要求时间是偶数的,都可以直接判断不可达!

比如一张地图c

  1. S...
  2. ....
  3. ....
  4. ....
  5. ...D

要求从S点到达D点,此时,从S到D的最短距离为s = abs ( dx - sx ) + abs ( dy - sy )。

如果地图中出现了不能经过的障碍物:

  1. S..X
  2. XX.X
  3. ...X
  4. .XXX
  5. ...D

此时的最短距离s' = s + 4,为了绕开障碍,不管偏移几个点,偏移的距离都是最短距离s加上一个偶数距离。

就如同上面说的矩阵,要求你从0走到0,无论你怎么绕,永远都是最短距离(偶数步)加上某个偶数步;要求你从1走到0,永远只能是最短距离(奇数步)加上某个偶数步。

例题

Tempter of the Bone

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

题目意思:给定你起点S,和终点D,问你是否能在 T 时刻恰好到达终点D。

#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int map[][];
int a,b,c,d,flag;
int n,m,t,count;
int dir[][]= {{,-},{,},{,},{-,}}; ///分别表示左、右、下、上四个方向
void DFS(int x,int y,int count)
{
int k,p,q,i;
if(x>n||y>m||x<||y<)
{
return ;
}
if(count==t&&x==c&&y==d)///时间正好才能逃生
{
flag=;
return ;
}
k=(t-count)-(abs(x-c)+abs(y-d))/*当前点到终点的最短路*/;///k为当前点到终点最短路还需要的时间差
if(k<||k&)///k<0或者为奇数则不可能到达
{
return ;
}
for(i=; i<; i++)
{
p=x+dir[i][];
q=y+dir[i][];
if(map[p][q]!='X')
{
map[p][q]='X';
DFS(p,q,count+);
if(flag)
{
return ;
}
map[p][q]='.';///搜索不到则退出,重新将该点刷成。
}
}
return ;
}
int main()
{
int w,i,j;///墙的数量
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
getchar();
w=;
if(n==&&m==&&t==)
{
break;
}
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')///记录狗的位置
{
a=i;
b=j;
}
else if(map[i][j]=='D')///出口的位置
{
c=i;
d=j;
}
else if(map[i][j]=='X')///墙的数量
{
w++;
}
}
getchar();
}
if(n*m-w<=t)///路径剪切,走完了不含墙的迷宫都还不到时间t,迷宫塌陷
{
printf("NO\n");
}
else
{
flag=;
map[a][b]='X';///将狗的起始位置刷为x
DFS(a,b,);///这里的DFS函数是一个需要自身调用自身的递归函数,需要设置参数
if(flag==)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}
return ;
}

DFS中的奇偶剪枝(技巧)的更多相关文章

  1. DFS中的奇偶剪枝学习笔记

    奇偶剪枝学习笔记 描述 编辑 现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点, s | | | + — — — e 如图所示(“|”竖走,“—”横走,“+”转弯),易证abs( ...

  2. HDU 1010 Tempter 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奇偶剪枝(t时刻恰好到达)】

    Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  4. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

  5. hdoj1010 奇偶剪枝+DFS

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

  6. Tempter of the Bone(dfs奇偶剪枝)

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

  7. hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)

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

  8. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  9. hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

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

随机推荐

  1. 竞赛题解 - NOIP2018 保卫王国

    \(\mathcal{NOIP2018}\) 保卫王国 - 竞赛题解 按某一个炒鸡dalao名曰 taotao 的话说: \(\ \ \ \ \ \ \ \ \ "一道sb倍增题" ...

  2. 微信小程序数据分析之自定义分析

    在小程序后台,微信已经提供了强大的数据分析功能,包括实时统计.访问分析.来源分析和用户画像功能,可以说对一般的数据分析已经完全足够了,但有时应用需要做一些更加精准的数据分析,比如具体到某一个页面的分享 ...

  3. 帝国cms发布信息时替换正文IMG图片标签里的ALT内容

    帝国cms发布信息时替换正文IMG图片标签里的ALT内容 在 e/class/userfun.php 里面增加 //替换正文IMG里的ALT内容 function user_imgalt($mid,$ ...

  4. Delphi7 GDI+学习

    Delphi7自带的绘图有锯齿,所以要学习GDI+ 主要是从这个网站学习 http://www.bianceng.com/Programming/Delphi/201212/34691.htm 相关控 ...

  5. Invoice Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Crm.Sdk.Messag ...

  6. 20145207 myeclipse测试

    实验博客  

  7. POJ2431_Expedition_KEY

    题目传送门 由题目可得,在一条路上有N个加油站,在距离终点a[i](细节)的位置上,你需要通过长度为L的路,油箱的容量是无限的,但是初始只有P点油,经过每个加油站时可以选择加b[i]的油,问最少加油几 ...

  8. 北京Uber优步司机奖励政策(2月26日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 无偏方差为什么除以n-1

    设样本均值为,样本方差为,总体均值为,总体方差为,那么样本方差有如下公式:. 很多人可能都会有疑问,为什么要除以n-1,而不是n,但是翻阅资料,发现很多都是交代到,如果除以n,对样本方差的估计不是无偏 ...

  10. Android ObjectOutputStream Serializable引发的血案

    遇到一个问题 安装后第二次进app,闪退 重现步骤 [前置条件] 打包分支:dev_7.13 手机:vivo NEX 8.1.0 [步骤] 安装三星app----同意用户协议进入书城---连续点击ba ...