博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdoj1010 奇偶剪枝+DFS
阅读量:7055 次
发布时间:2019-06-28

本文共 3115 字,大约阅读时间需要 10 分钟。

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 151082    Accepted Submission(s): 40265

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

分析:这个题目用一般的搜索无法完成,因为题目要求在指定的时间内完成,所以只好一步一步来,用DFS解决。

但是如果这样结果会超时,网上说是用一种奇偶剪枝的方法来间断搜索时间,下面是剪枝的简单理论,一看就懂:

                          把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->0 需要偶数步

                       那么设所在位置 (px,py) 与 目标位置 (ppx,ppy)

                       如果abs(px-ppx)+abs(py-ppy)为偶数,则说明 abs(x-y) 和 abs(dx-dy)的奇偶性相同,需要走偶数步

                       如果abs(x-y)+abs(dx-dy)为奇数,那么说明 abs(x-y) 和 abs(dx-dy)的奇偶性不同,需要走奇数步

                       理解为 abs(si-sj)+abs(di-dj) 的奇偶性就确定了所需要的步数的奇偶性!!

                       而 (t-setp)表示剩下还需要走的步数,由于题目要求要在 ti时 恰好到达,那么  (t-step) 与 abs(x-y)+abs(dx-dy) 的奇偶性必须相同

                       因此 t+abs(px-ppx)-abs(py-ppy) 必然为偶数!!!

下面是AC代码:

#include
#include
#include
using namespace std;int n,m,t; // 行n列m时间tint flag; //标记是否可以surviveint dir[4][2]={1,0,-1,0,0,1,0,-1}; //用来表示下,上,右,左int visit[8][8]; //用来标识地图每一点是否被经过char map[8][8]; //记录地图每一点的属性int px,py,ppx,ppy; //分别表示‘S’的坐标和‘D’的坐标void dfs(int x,int y,int count){ if(count>t) return; else if(map[x][y]=='D'){ if(count==t) flag=1; return; } for(int i=0;i<4;i++){ int xx=x+dir[i][0]; //移动 int yy=y+dir[i][1]; if(map[xx][yy]!='X'&&xx>=0&&xx
=0&&yy
>n>>m>>t&&(m+n+t)){ flag=0; for(int i=0;i
>map[i]; for(int j=0;j

 体会:学习了奇偶剪枝技巧和DFS算法。

转载于:https://www.cnblogs.com/FrankChen831X/p/10326087.html

你可能感兴趣的文章
古城钟楼微博:葡萄城程序员演练技术的产物
查看>>
最常用的四种数据分析方法
查看>>
Mesos安装部署笔记
查看>>
epoll的作用和原理介绍
查看>>
服务器远程监控管理(一)-硬件篇
查看>>
Android permission 工具类
查看>>
Tomcat使用与配置
查看>>
接口与抽象类的区别(转)
查看>>
转载:分析apk工具aapt的使用,解析其原理
查看>>
如何向视图插入数据
查看>>
注册和策略模式
查看>>
python 列表
查看>>
第七课作业
查看>>
MEAN实践——LAMP的新时代替代方案(下)
查看>>
CentOS7 下安装 Oracle 12c
查看>>
简单介绍AngularJs Filters
查看>>
Dubbo下一站:Apache顶级项目
查看>>
我说分布式事务之最大努力通知型事务
查看>>
挖机全车无动作是什么故障原因引起的?
查看>>
监狱电视系统设计原则及应用场景
查看>>