简体   繁体   中英

Breadth first search example

I was wondering if the solution to the 8 queens problem below uses breadth-first search. 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.

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. 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. etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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