HDU-1010-Tempter of the Bone(DFS+奇偶剪枝)

Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 140186 Accepted Submission(s): 37451

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是不行的
需要奇偶剪枝,那么什么是奇偶剪枝?
每走一步都有奇偶性,总会从奇步走到偶步(但也不用在意到底是奇还是偶,只需要关注相对性)
所以地图也可以有,如图
01010101010
10101010101
01010101010
10101010101
所以如果需要走的步数是偶步数,那么肯定是从奇起点走到奇终点,或者是偶起点走到偶终点。
所以如果需要走的步数是奇步数,那么肯定是从奇起点走到偶终点,或者是偶起点走到奇终点。

所以根据这个就可以剪枝了。

另外需要注意,还需要确定走的步数的奇偶性。可能一开始觉得这个步数的奇偶性需要dfs之后才知道。
但其实有更简单的方法。
首先忽略墙的存在,求出起点到终点最短长度,即为abs(sx-dx)+abs(sy-dy),这个应该还是好理解的。(如果不懂就画画图)
之后你就会发现,如果你要绕路,比如向北方向绕1格的路,那么为了去终点,将来某时刻你必须向南走1格绕回来。
所以一来一回就是2步了。正因为是2步所以奇偶性就不变了。所以你无论怎么绕路,奇偶性都是最短距离的奇偶性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值