bfs判重与减枝的区别

本文介绍了在解决Dragon Maze问题时,BFS算法中判重和减枝的区别。判重确保程序不会陷入死循环,而减枝则是为了提高搜索效率。以China New Grad Test 2014 Round B的Problem D为例,解释了如何在BFS中处理这两个概念,以找到从入口到出口的最短路径并最大化收集的能源。
摘要由CSDN通过智能技术生成

提到bfs,很多人第一反应是往往要做”状态判重“ 和 ”减枝“。两者有何区别?

我的理解是:

”判重“是保证程序在有限时间内有正确输出,不出现死循环。简单理解就是,走过的路不再走,防止绕圈圈的情况出现。

而”减枝“是在保证程序有正确输出的基础上,将不必要的搜索枝条减去。

一个是保证正确性,一个是提高效率。

我以以下例子来说明:

China New Grad Test 2014 Round B

Problem D. Dragon Maze

Problem

You are the prince of Dragon Kingdom and your kingdom is in danger of running out of power. You must find power to save your kingdom and its people. An old legend states that power comes from a place known as Dragon Maze. Dragon Maze appears randomly out of nowhere without notice and suddenly disappears without warning. You know where Dragon Maze is now, so it is important you retrieve some power before it disappears.

Dragon Maze is a rectangular maze, an N x M grid of cells. The top left corner cell of the maze is (0,0) and the bottom right corner is (N-1, M-1). Each cell making up the maze can be either a dangerous place which you never escape after entering, or a safe place that contains a certain amount of power. The power in a safe cell is automatically gathered once you enter that cell, and can only be gathered once. Starting from a cell, you can walk up/down/left/right to adjacent cells with a single step.

Now you know where the entrance and exit cells are, that they are different, and that they are both safe cells. In order to get out of Dragon Maze before it disappears, you must walk from the entrance cell to the exit cell taking as few steps as possib

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值