提到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