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[8][8];
int v[8][8];
int jx[]= {1,-1,0,0};
int jy[]= {0,0,1,-1};
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=1;return ;}int dis=t-ans-Distance(x,y);if(dis<0||dis%2) return ;// 剩余步数小于最短距离或者满足奇偶剪枝条件  for(int i=0; i<4; i++){int xx=x+jx[i];int yy=y+jy[i];if(xx>=0&&xx<n&&yy>=0&&yy<m&&v[xx][yy]==0&&map[xx][yy]!='X'){v[xx][yy]=1;dfs(xx,yy,ans+1);if(flag==1)break;v[xx][yy]=0;}}return ;
}
int main()
{int xx,yy,sum;while(scanf("%d%d%d",&n,&m,&t)!=EOF){flag=sum=0;if(n==0&&m==0&&t==0) break;for(int i=0; i<n; i++){scanf("%*c%s",map[i]);}for(int i=0; i<n; i++){for(int j=0; j<m; j++){v[i][j]=0;if(map[i][j]=='X'){sum++;}else if(map[i][j]=='S'){xx=i;yy=j;v[i][j]=1;}else if(map[i][j]=='D'){tx=i;ty=j;}}}v[xx][yy]=1;if(n*m-sum>t){dfs(xx,yy,0);// 可通行的点必须大于要求的步数,路径剪枝。
        }if(flag==1) printf("YES\n");else printf("NO\n");}return 0;
}

什么是奇偶剪枝?

把矩阵看成如下形式:
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-sharp] view plaincopy
  1. S...
  2. ....
  3. ....
  4. ....
  5. ...D

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

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

[c-sharp] view plaincopy
  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的奇偶性与最短距离的奇偶性不一致,那么我们就可以直接判定这条路线永远不可达了。

转载于:https://www.cnblogs.com/zhangmingcheng/p/3984852.html

HDU1010:Tempter of the Bone(dfs+剪枝)相关推荐

  1. HDU1010 Tempter of the Bone DFS+剪枝

    点击打开链接 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  2. HDU1010 Tempter of the Bone dfs(奇偶减枝)

    直接搜索会超时,需要减枝(奇偶减枝). #include<stdio.h> #include<string.h> int n,m,t; char map[7][7]; int ...

  3. hdu1010 Tempter of the Bone

    转载自:http://acm.hdu.edu.cn/forum/read.php?tid=6158 sample input: 4 4 5 S.X. ..X. ..XD .... 问题: (1): 在 ...

  4. (step4.3.1) hdu 1010(Tempter of the Bone——DFS)

    题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...

  5. HDU1010 Tempter of the Bone(DFS奇偶剪枝)

    传送门 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. Howe ...

  6. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  7. 【hdoj_1010】Tempter of the Bone(迷宫+剪枝)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给出一个迷宫(含起点和终点),要求找出一条路径,这条路径的长度必须为某个规定的长度. 在做 ...

  8. HDU 1010 Tempter of the Bone heuristic 剪枝法

    本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢, ...

  9. hdu1010 Tempter of the Bone

    题目意思:   一只吉娃娃去迷宫捡骨头, 捡到骨头后发现是一个陷阱, 然后就想逃出迷宫:迷宫是N*M 规格的, 迷宫只有一道门且 只在第 T 秒钟开一会儿(少于1秒) 也就是说只在[ t, t+1) ...

最新文章

  1. 工厂模式、策略者模式、责任链模式综合应用
  2. 必应词典UWP版-开发小结
  3. wordpress archive.php,哪个网址将导致wordpress使用archive.php?
  4. c语言试卷浙江理工大学杀人案件追踪,浙江理工大学c语言期末考试模拟试卷6 .pdf...
  5. 批量将csv转xls
  6. 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
  7. openjudge 7622 求排列的逆序数(归并)
  8. 自主安全国产虚拟化平台CNware
  9. visio绘图固定图形位置
  10. python层次分析法案例_在R语言中使用层次分析法-案例1
  11. 存储专栏:深度解读高端存储的快照技术
  12. 在地化和本土化的区别_翻译和本地化有什么区别?
  13. Shiro在线刷新权限
  14. 全球最牛逼的并发架构,抖音排第二,它排第一!
  15. 简单的使用EA进行需求管理
  16. HHKB 键盘 配置Mac 的 command 和 切换输入法
  17. Nginx 负载均衡动静分离配置
  18. visual studio创立上位机软件(C#)(定时器)
  19. 以复旦大学为例。我对复旦比较熟悉。
  20. 港科喜讯|香港科大再获[商科]评审全港第一!

热门文章

  1. highcharts一天时间 与一周时间_一天当中什么时间减肥降重最好的
  2. xmind快捷键_XMind思维导图软件最全面的使用教程!
  3. python接口测试之requests详解_Python接口测试-requests库
  4. win10安装mysql5.6,mysql启动时,闪退
  5. 三十三、数据仓库的概述
  6. Scrapy爬取整个美女网爬下来,要多少有多少
  7. 掌握这些 NumPy Pandas 方法,快速提升数据处理效率!
  8. 这里有 8 个流行的 Python 可视化工具包,你喜欢哪个?
  9. 深度学习-Tensorflow2.2-深度学习基础和tf.keras{1}-梯度下降算法概述-03
  10. 机器学习-分类算法-模型的保存和加载12