Problem Description:
The eight Queens question, an old and famous problem, is a typical case of backtracking algorithms. The issue is the international chess player Max Bessel in 1848: Put eight queens on the 8x8 chess board, so that any two queens can not attack each other, that is, any row, column or diagonal (with a horizontal axis angle of 45° or 135° diagonal) on not two or more than two queens. For this problem the mathematician Gauss thinks there are 76 kinds of schemes. In 1854, in the Chess magazine in Berlin, different authors published 40 different solutions, and later some of them solved 92 kinds of results using the method of graph theory. After the invention of computer, there are many computer languages to solve this problem.
Problem analysis
#: Indicates a queen
*: Indicates no Queen
Create a function to implement a recursive invocation, where we declare the void searcheightqueue (int i);
Specific design ideas are as follows:
A row of the board to scan, if found can be placed in the position of the Queen to the position of the element representing the status of ' # ', and then continue to scan the next line, if the scan continues to the eighth row can still find the location of the Queen, indicating that the pendulum is correct, the board printed out. If you find that you cannot find a place to put the Queen when you go to a line, then go back to the original state.
The code is as follows:
/* Eight Queen question, #代表皇后位置 */#include <stdio.h> #include <stdlib.h>static int eightqueuenum = 0;// Define a global static variable eightqueuenum to print out the total number of scenarios static char queue[8][8];//defines a 8 row 8 column checkerboard static int a[8];//defines the state of each column in a row, a[0]~a[7] Represents the first column to the eighth column, if a[1]=1, indicating that the column exists queen static int b[15];static int c[15];void searcheightqueue (int i) {int icolumn,iline,icolumn 1; for (IColumn = 0;icolumn < 8;icolumn++) {if (a[icolumn] = = 0 && B[i+icolumn] = = 0 && c[i-icolu MN+7] = = 0) {Queue[i][icolumn] = ' # '; A[icolumn] = 1; B[i+icolumn] = 1; C[I-ICOLUMN+7] = 1; if (I < 7) searcheightqueue (i+1); else {eightqueuenum++; printf ("\ n" \ Eight the Queen's Problem%d solution is: \ n ", eightqueuenum); for (ILine = 0;iline < 8;iline++) {for (IColumn1 = 0;icolumn1 < 8;icolumn1++) {printf ("%c", Queue[iline][icolumn1]); } printf ("\ n"); } if (eightqueuenum%10 = = 0) {getch (); }/* If the program did not successfully scan to line 7th, we found no place to put our Queen, so we go back to the original state rescan if we i= 3 o'clock to meet if (a[icolumn ] = = 0 && B[i+icolumn] = = 0 && c[i-icolumn+7] = 0) The statement entered the loop, but when i+1 becomes 4 o'clock does not satisfy if (a[icolumn] = = 0 &am p;& B[i+icolumn] = = 0 && c[i-icolumn+7] = = 0) statement So this order is not correct, we need to put the previous from i=0 to I=3 (the first row to the fourth row) when the Queen was put off, The order of recursive call functions can be implemented exactly. Thus, when the Searcheightqueue () function finishes, the state in The Matrix Queue[8][8] is exactly the same as the state of the definition. Just so always try, until all the states have been tried (where the correct result is printed out, is the correct result that shows when i = 7 o'clock, still satisfies if (a[icolumn] = = 0 && B[i+icolumn] = = 0 && c[i-icolumn+7] = = 0) enters the loop, and I= 7, enters the Else statement block. */Queue[i][icolumn] = ' * '; A[icolumn] = 0; B[i+icolumn] = 0; C[I-ICOLUMN+7] = 0; }}}int Main () {int i = 0,J; for (i = 0;i < 8;i++) {A[i] = 0; for (j = 0;j < 8;j++) {Queue[i][j] = ' * '; } printf ("\ n"); } for (i = 0;i < 16;i++) {B[i] = 0; C[i] = 0; } searcheightqueue (0); printf ("Total Solution:%d", eightqueuenum); return 0;}
Using recursion and backtracking to achieve the eight queen problem