Use STL to implement deep search and wide search-n queen's question example

Source: Internet
Author: User
Use STL to implement deep search and wide search -- N Queen's question exampleTwo days ago in the sharpdew blog (http://blog.csdn.net/sharpdew) saw the n queen problem article, sharpdew algorithm is very beautiful, high efficiency. I just want to try my DFS/BFS algorithm from the test point of view, so I tried to write it. But I can tell you that my program is at least several times slower than sharpdew. I think the reason is that STL containers (such as vector, stack, and queue) when the size of the Search Tree increases, the validity rate decreases. After all, sharpdew algorithms all use the long type for processing, and the efficiency cannot be the same as the Japanese. N queen's question is: place n queens (ChEss) in an nxn board ), each queen must not attack each other (that is, there cannot be two queens on the same row, column, or diagonal line ). For example, if n = 8, the following State is a solution:

I don't want to output all the answers, just search for the total number of answers for the specified n. Like the sharpdew program, I also limit n to less than 32. Since n = 1, 2, and 3 are obviously not resolved, I assume N is between 4-32. Like what we do in the sudoku program, what we need is a state class that can work with DFS/BFs. I name it queenstate. In addition to nextstep () and istarget (), what else does it need? The first is the data member, that is, how to represent a checkerboard status. The sharpdew program uses the bit in long, which is very delicate, but also very difficult to understand (so you can see that the sharpdew program has a lot of comments. Even so, you have to be very careful and take a serious look to understand ). I want to use some simple representation methods, so I use an int array. Each element represents the position of the queen in a row on the board. The rows and columns start from 0. For example, a [0] = 0 indicates that the Queen of the first row is in the first column, and a [1] = 7 indicates that the Queen of the second row is in the eighth column. Obviously, each row on the board can have only one queen, so this representation method is feasible. Of course, we also need to express the Unknown Status of the Queen's position, which I use-1 to represent. Therefore, the data members of the queenstate include the N _ representing the board size and the lines _ representing the Queen's position _. This is enough, but in order to facilitate our search and processing, I also added an int to indicate the number of rows in the current Checker that have determined the location of the Queen, named cur_line _. Currently, the queenstate is defined as follows: Class queenstate {public: queenstate (int n); void nextstep (vector <queenstate> & VQ) const; bool istarget () const; private: int N _, cur_line _; int lines _ [maxqueennumber] ;}; let's start with simple writing. The first is the constructor, which is very simple, initialize the three data members. N _ is passed in, cur_line _ is initialized to 0, and all elements in the array are initialized to-1, as follows: queenstate (int n): N _ (n ), cur_line _ (0) {fill_n (lines _, N _,-1);} next is istarget (), which is simpler, just check cur_line _ to see if all rows have determined the location of the Queen. As follows: bool istarget () const {return cur_line _ = N _;} is finally nextstep (), which is slightly more complex. We need to determine the location where the queen can be placed in row cur_line _ + based on the location of the Queen in the row before cur_line. The method is to traverse all rows with the specified Queen location from the first row, and iterate each row, the conflicting columns (including line and diagonal line conflicts) in the row cur_line _ + 1 are excluded based on the row's Queen position ). After the traversal is complete, the remaining unexcluded positions are the positions where the queen can be placed. Based on these locations, the next node of the state space tree is generated one by one, and the result is returned. We use a bool array to record whether a location is excluded. Void queenstate: nextstep (vector <queenstate> & VQ) const {queenstate newstate (N _); bool POS [maxqueennumber]; fill_n (Pos, N _, true ); for (INT I = 0; I <cur_line _; ++ I) {pos [Lines _ [I] = false; // It cannot be the same as the existing Queen's Int J = lines _ [I] + (cur_line _-I); // it cannot be the same diagonal line if (j <n _) {pos [J] = false;} J = lines _ [I]-(cur_line _-I); If (j> = 0) {pos [J] = false ;}}for (INT I = 0; I <n _; ++ I) {If (Pos [I]) {NEWS Tate = * This; newstate. lines _ [cur_line _] = I; // a possible location of the Queen of the next row + + newstate. cur_line _; // The number of determined rows plus a VQ. push_back (newstate) ;}} there are so many queenstates, followed by the result processing function required by DFS/BFs. As I mentioned earlier, I do not want to process any search results, but simply accumulate the number of results, and this DFS/BFS has already been done. Therefore, we only need to simply return a false value to request the DFS/BFs to continue searching. Bool continuesearch (const queenstate &) {return false;} is the main program. What we need to do is to read the size of the Board from the command line and generate an initial checker status, then, call the DFS/BFS function template to retrieve the call result and display it. Int main (INT argc, char * argv []) {If (argc <2) {cerr <"Usage: Queen <queen_number>" <Endl; return 1 ;} int n = atoi (argv [1]); If (n <minqueennumber | n> maxqueennumber) {cerr <"queen_number too large or too small" <Endl; return 1;} cout <"queen_number =" <n <Endl; queenstate initstate (n); int Total = depthfirstsearch (initstate, & continuesearch ); cout <"Total =" <total <Endl; return 0;} from the previous analysis, we can see that the Queen's problem search starts from the first line and goes down from one line, each layer of node in the status space tree has a row of determined Queen location more than the previous node, so there is no duplicate status in the search process. This is the same as the solution to the sudoku problem, so our DFS/BFS can run normally. Next time, we will face the possibility of duplicate node states, which requires some improvements to the current simple DFS/BFS algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.