简体   繁体   English

广度优先搜索示例

[英]Breadth first search example

I was wondering if the solution to the 8 queens problem below uses breadth-first search. 我想知道下面8个皇后区问题的解决方案是否使用广度优先搜索。 It searches all possible nodes; 它搜索所有可能的节点; however, when there nowhere the queen can be legally placed, the previous queen is deleted. 但是,如果在任何地方都不能合法放置女王/王后,则会删除前一个女王/王后。 If not, I am wondering how I could convert this to use breadth-first search. 如果没有,我想知道如何将其转换为使用广度优先搜索。

I'd say the general format of a BFS solution to the eigth queens problem is to take some representation of a solution (like 8 numbers form 0 to 7, representing the row the queen is in for each column) and then just walking past the possible solutions ( 0000000 to 77777777 ) sequentially, noting each possible solution. 我要说的是,BFS解决方案对eigth皇后号问题的一般格式是采用某种解决方案的表示形式(例如从0到7的8个数字,代表皇后在每一列中的行),然后走过去可能的解决方案( 000000077777777 ),并注意每个可能的解决方案。

Depth-First-Search with backtracking, like you use, is more efficient. 像您使用的一样,具有回溯功能的深度优先搜索更加有效。 See this article for a comparison between the two techniques. 请参阅本文以比较这两种技术。

Edit: On the topic of BFS: There are tricks to make it more efficient for this problem. 编辑:关于BFS的主题:有一些技巧可以使它更有效地解决此问题。 For example, if you placed one queen in row 0, then you know that you don't have to try that row for the queens in other columns. 例如,如果您在第0行中放置一个皇后,那么您就不必为其他列中的皇后而尝试该行。 etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM