Title of Queen eight

Source: Internet
Author: User

Place eight queens on an 8*8 chessboard, so that they are not on a horizontal or vertical or diagonal line. You can imagine that the first piece occupies the first position () on the first board, and then places the second piece on the second Board to meet the conditions of the Queen, for example, (2, 3. When there is no position available, you must move the position of the pawns in the previous line, that is, to trace back. For other details, refer to relevant books, it can be recursive or non-recursive. I hope it is clear :)
---------------------------------------------------------------
 
It is best to use the stack to solve this problem:
1. Create a stack first. Then, put the first pawn in the first row, the first column (), and store it to the bottom of the stack, as described above.
2. traverse the position of the second piece and place the second piece in the position where the second row of the Board meets the conditions, such as (2, 3), and save it to the top of the stack.
3. Search for the position of the Third pawn,
4 ,............
5. When the position of the previous piece cannot be found, the position of the previous piece is incorrect. Take the previous piece out of the stack and traverse it to find other positions of the previous piece,
6. If the position of the previous pawn is not found, continue to the stack and traverse to find its position,
7 ,......
Actually, this is a recursive and rollback problem, but in the process of recursion and backtracking, stack is used to store the position of the piece found.

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.