[Sword refers to offer] Eight queens

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/ns_code/article/details/26614999


The sword solves the eight queens Problem on the offer. Instead of using the traditional recursive or non-Recursive Backtracking Method, it uses a clever full arrangement method.

Let's talk about the eight queens problem first: Eight queens are placed on 8X8 chess sets so that they cannot attack each other, that is, they cannot be in the same line, for the same column or on the diagonal line of consent, find all the qualified pendulum methods.

The solution to the eight queens problem is arranged in an all-round manner as follows:

Because the eight queens cannot be in the same row, it is certain that each Queen occupies A row. In this way, an array A [8] can be defined and the number I in the array can be defined, that is, A [I] indicates the column number of the Queen located in row I. First, initialize array A [8] with 0-7, and then arrange the array in full. Because we use seven different numbers 0-7 to initialize the array, therefore, any two queens are certainly different columns, so we only need to determine whether any two of the eight queens in each arrangement are on the same diagonal line, that is, for the two subscripts I and j of the array, if I-j = A [I]-A [j] Or I-j = A [j]-A [I], if two elements are located on the same diagonal line, the arrangement does not meet the conditions.

The Code is as follows:

# Include <stdio. h> void swap (int * a, int * B) {int temp = * a; * a = * B; * B = temp ;} /* if there is A qualified pendulum, print all the pendulum methods. Otherwise, nothing will be printed */void CubVertex (int * A, int len, int begin) {if (A = NULL | len! = 8) return; if (begin = len-1) {int I, j; bool can = true; // whether the pendulum for (I = 0; I <len; I ++) for (j = I + 1; j <len; j ++) if (I-j = A [I]-A [j] | I-j = A [j]-A [I]) {// if any two of them are on a diagonal line, it does not conform to can = false; break;} // if (can) {for (I = 0; I <len; I ++) printf ("% d", A [I]); printf ("\ n ");}} else {int I; for (I = begin; I <len; I ++) {swap (& A [begin], & A [I]); CubVertex (, len, begin + 1); swap (& A [begin], & A [I]) ;}} int main () {int A [8] = {0, 1, 2, 3, 4, 5, 6, 7}; CubVertex (A, 8, 0); return 0 ;}

Test results:

Queen four:


There are two methods in total for the four queens.

1, 3, 0, 2 means that the Queen of the fifth row is placed in the second position (starting from 0), and the queen of the fifth row is placed in the second position, the queen on the fifth row is placed in 3rd locations, and the Queen on the fifth row is placed in 0th locations.

Queen eight:


There are a total of 92 methods for the eight queens.

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.