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

http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客

http://blog.csdn.net/chyshnu/article/details/6171758//一个很好举例的博客

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

例题:ZOJ Problem Set - 2110 Tempter of the Bone

题目意思是讲有一只狗要吃骨头,结果进入了一个迷宫陷阱,迷宫里每走过一个地板费时一秒,该地板 就会在下一秒塌陷,所以你不能在该地板上逗留。迷宫里面有一个门,只能在特定的某一秒才能打开,让狗逃出去。现在题目告诉你迷宫的大小和门打开的时间,问你狗可不可以逃出去,可以就输出YES,否则NO。

搜索时要用到的剪枝:

1.如果当前时间即步数(step) >= T 而且还没有找到D点,则剪掉。

2.设当前位置(x, y)到D点(dx, dy)的最短距离为s,到达当前位置(x, y)已经花费时间(步数)step,那么,如果题目要求的时间T - step < s,则剪掉。

3. 对于当前位置(x, y),如果,(T-step-s)是奇数,则剪掉(奇偶剪枝)。

4.如果地图中,可走的点的数目(xnum) < 要求的时间T,则剪掉(路径剪枝)。

题目解析:

通过做这题算是懂剪枝的思想了,要学奇偶剪枝首先要看懂那个01矩阵(很好理解),之后就没什么问题了,

剪完枝后大约100ms就过了,怎么说呢,还是了解思想比较重要。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int n,m,t,tx,ty,flag;
char map[][];
int v[][];
int jx[]= {,-,,};
int jy[]= {,,,-};
int Distance ( int x, int y )
{
return abs(x - tx )+abs( y - ty ); // 当前点(x,y)到终点(tx,ty)的最短距离
}
void dfs(int x,int y,int ans)
{
if(ans>t) return ;
if(x==tx&&y==ty&&ans==t)
{
flag=;
return ;
}
int dis=t-ans-Distance(x,y);
if(dis<||dis%) return ;// 剩余步数小于最短距离或者满足奇偶剪枝条件 
for(int i=; i<; i++)
{
int xx=x+jx[i];
int yy=y+jy[i];
if(xx>=&&xx<n&&yy>=&&yy<m&&v[xx][yy]==&&map[xx][yy]!='X')
{
v[xx][yy]=;
dfs(xx,yy,ans+);
if(flag==)
break;
v[xx][yy]=;
}
}
return ;
}
int main()
{
int xx,yy,sum;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
flag=sum=;
if(n==&&m==&&t==) break;
for(int i=; i<n; i++)
{
scanf("%*c%s",map[i]);
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
v[i][j]=;
if(map[i][j]=='X')
{
sum++;
}
else if(map[i][j]=='S')
{
xx=i;
yy=j;
v[i][j]=;
}
else if(map[i][j]=='D')
{
tx=i;
ty=j;
}
}
}
v[xx][yy]=;
if(n*m-sum>t)
{
dfs(xx,yy,);// 可通行的点必须大于要求的步数,路径剪枝。
}
if(flag==) printf("YES\n");
else printf("NO\n");
}
return ;
}

什么是奇偶剪枝?

把矩阵看成如下形式:

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 但是要求时间是偶数的,都可以直接判断不可达!

比如有一地图:

  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,永远只能是最短距离(奇数步)加上某个偶数步。

关于奇偶剪枝

首先举个例子,有如下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的奇偶性与最短距离的奇偶性不一致,那么我们就可以直接判定这条路线永远不可达了。

HDU1010:Tempter of the Bone(dfs+剪枝)的更多相关文章

  1. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

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

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

  4. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  5. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  6. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

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

  7. HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】

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

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

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

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

随机推荐

  1. Visual Studio for Mac离线安装教程

    Visual Studio for Mac离线安装教程 可以在线安装,也可以离线安装(本次安装博主使用离线,在线安装失败了) 据说翻个墙就可以,有条件的就翻吧 没条件的我于是选择离线安装………… 离线 ...

  2. 题目1453:Greedy Tino(dp题目)

    题目链接:http://ac.jobdu.com/problem.php?pid=1453 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  3. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  4. 查看运行中的Java其配置的堆大小

    一.背景 有题目中的需求,也不是空穴来风:前一阵给公司搭建了一个持续集成服务器,Jenkins.最近发现,运行一段时间后,就变慢了. 随便一个操作,cpu就飙高了.然后就思考会不会是内存不够用,频繁G ...

  5. linux学习路线图

  6. 用CornerStone配置SVN,HTTP及svn简单使用说明

    转载 http://my.oschina.net/joanfen/blog/194491 一.下载地址 CornerStoneV2.6:http://pan.baidu.com/s/1qWEsEbM密 ...

  7. 用AT命令调试调制解调器

    最早生产调制解调器的公司是贺氏,后来组建的厂家制造的调制解调器都与HAYES兼容,大部分的通信软件使用菜单来对调制解调器进行配置.检测.但是有些通信软件要求用户直接发命令给调制解调器,在这种情况下必须 ...

  8. 【笔记】javascript权威指南-第六章-对象

    对象 //本书是指:javascript权威指南    //以下内容摘记时间为:2013.7.28 对象的定义: 1.对象是一种复合值:将很多值(原始值或者对象)聚合在一起,可以通过名字访问这些值. ...

  9. Xcode 6 下添加pch头文件

    没错了,Xcode 6 有着许多坑,例如新建的工程里没有默认的pch文件,当然本质上应该是为了提高编译的速度,但却让开发略微有点不方便. 话不多说,其实新建很简单 1.先新建一个PCH文件 2.设置头 ...

  10. TFS二次开发04——工作区(Workspace)和映射(Mapping)

    在前面几节介绍了怎样读取TFS服务器上的项目以及文件的信息,这一节将介绍怎么建立服务器和本地的映射(Mapping). 引用命名空间 usingMicrosoft.TeamFoundation.Cli ...