Question of the eight Queens

Source: Internet
Author: User

Question thrown: Place 8 queens on the board, so that they do not attack each other, at this time each queen's attack range for peers and the same diagonal, ask to find all the solution. :


Analysis

The simplest approach is to turn the problem into a "subset from 64 squares" so that "there are exactly 8 squares in the sub-set, and any two selected squares are not on the same row, in the same column, or on the same diagonal". This is the subset enumeration problem. However, the subset of 64 lattices has 2^64, the order of magnitude has 10^19, too big, this is not a good model.

Simplification: Convert the problem to "choose 8 squares from 64 grids", which is a combinatorial generation problem. According to combinatorial mathematics, there are 8C64 = 4.426*10^9 schemes, much less, is not enough.

Further simplification: Observe the law by placing a queen in each row. If you use C[x] to represent the column number of the Queen X row, the problem becomes the full-array generation problem. The 0~7 arrangement is only 8!=40320, and the enumerator does not exceed it.

Note: When the problem is divided into several steps and solved recursively, if there is no legal selection for the current step, the function returns to the previous level of recursive invocation, which becomes backtracking. It is for this reason that recursive enumeration algorithm is often used as backtracking (backtracking) method, and its application is very common.


Demo 1

#include <iostream> #include <cstdio>using namespace std;int N, tot;int c[8];void search (int cur) {if (cur = = N) {//recursive boundary. As long as we come here, all the queens must not conflict tot++;} else for (int i = 0; i < n; i++) {bool OK = true; C[cur] = i; Try to place the Queen of line cur in column I for (int j = 0; j < cur; j + +) {//check if and before your queen conflict if (c[cur] = C[j] | | Cur-c[cur] = = J-c[j] | | Main diagonal decision cur + c[cur] = = j + c[j]) {//Sub diagonal decision OK = false;break;}} if (OK) {search (cur + 1);}}} int main () {cin >> N;tot = 0;search (0); cout << tot << endl;return 0;}

Note: Since it is placed row by line, the Queen will definitely not attack horizontally, so just check for vertical and oblique attacks.

Nodes are now difficult to reduce, but the efficiency of the program can continue to improve, using a two-dimensional array vis[2][] to directly determine the current attempt of the Queen's Column and two diagonal whether there are other queens. Note that the main diagonal identifier y-x may be negative, and N is added to the access.

Demo 2

#include <iostream> #include <cstdio>using namespace std;int N, tot;int c[8];int vis[3][15];void Search (int cur) {if (cur = = n) {tot++;}  else for (int i = 0; i < n; i++) {if (!vis[0][i] &&!vis[1][cur + i] &&!vis[2][cur-i + n]) {C[cur] = I;vis[0][i] = vis[1][cur + i] = vis[2][cur-i + n] = 1; Modify the global variable search (cur + 1); Vis[0][i] = Vis[1][cur + i] = vis[2][cur-i + n] = 0; Remember to change it back}}}int main () {cin >> N;tot = 0;memset (Vis, 0, sizeof (VIS)); search (0); cout << tot << endl;return 0;}

The Demo 2 program has one of its key points: the use of a vis array, the exact meaning of the VIS array: it represents which columns, main diagonal, and diagonal lines are occupied by the placed Queen. Future Queens should not modify these values. Generally, if the auxiliary global variables are modified in backtracking, they must be restored in time (unless the modifications are deliberately preserved). Also, be sure to empty the VIS array before calling.


Note: If you use auxiliary global variables in backtracking, be sure to restore them to their original state in time. In particular, if the function has multiple exits, the modified value needs to be restored at each exit.

Question of 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.