The question of the eight queens was raised by the international chess player Max Bessel in 1848: placing eight queens on 8x8 's chess, so that they cannot attack each other, that any two queens cannot be on the same line, in the same column, or on the same slash, asking how many kinds of pendulum. 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. The idea of solving the problem is as follows: from the first line of the chessboard, first select the first square as the first Queen, and then in the second row, starting from the first lattice to test a suitable position to place the second queen, then in the third row from the first lattice to test a suitable place to place the third queen ... And so on, know the eighth Queen to find the right place. At this point the first pendulum that satisfies the requirement appears. Because each row is searched from the first lattice, and in order to exhaust all the pendulum, the last Queen will continue to move in the last line to check if there is any pendulum to meet the requirements. When the last line is complete, return to the previous line, the seventh Queen to detect if there is a suitable location, if the seventh line to find another place to meet the requirements, then to the eighth row, from the first lattice to detect the placement of the eighth Queen position; if the seventh line does not find another suitable position, Then the program goes to line sixth and moves the sixth Queen back in turn, probing another position where the sixth queen can be placed ... And so on, this is the meaning of backtracking: a few steps forward from a point a, when the problem is solved or cannot be solved, then go back to point a, go further in the other direction, detect the solution of the problem or more solutions.
Check if a location B can place a queen, because the program is traversing the board from the top to the next line, so you just need to check if there are any squares above that position that make it impossible to place a Queen: 1) b position above the same column; 2) b position the lattice above the left diagonal diagonal 3) b position at the top right diagonal of the grid; line does not need to detect, because one line can only place a queen, if there is already a queen and B peers, the check will not proceed.
Checkerboard data structure and code to create a chessboard
#include "stdafx.h"#include <iostream>using namespace STD;Const intCandidate_pos =0;Const intLegal_pos =1;structchessboard{intSizeint*board;}; chessboard* Create (intSize) {chessboard* CB =NewChessboard; cb->size = size; Cb->board =New int[Size*size];memset(Cb->board,0, size*size*sizeof(int));returnCB;}
To print the board's code:
print(chessboard* cb){ intsize = cb->size; for(int0size; ++i) { for(int0size; ++j) cout<<cb->board[i*size + j]<<‘ ‘; cout<<endl; } cout<<endl;}
Check if a location is appropriate to place a Queen's code:
BOOL Check(chessboard* CB,intRowintCol) {intSize = cb->size;int* board = cb->board;//Check column int* p = &board[col]; for(inti =0; i < row; ++i) {if(*p)return false; p + = size; }//Check the diagonal descending from left to right for(inti = row-1, j = col-1; I >=0&& J >=0; -I,--J) {if(Board[i*size + j])return false; }//Check the diagonal descending from right to left for(inti = row-1, j = col +1; I >=0&& J < size; --I,++J) {if(Board[i*size + j])return false; }return true;}
Search for the Eight Queens Location code:
void_solve (chessboard** CB,intIintJint&Count){//row or column dimension, any one dimension to the end, the bank search ends, return to the previous row if(I >= (*CB)->size | | J >= (*CB)->size)return;intSize = (*CB)->size;int* board = (*CB)->board;//Find a place where the queen can be placed if(check (*CB,I,J)) {//Set the position to 1, indicating that a queen was placedBoard[i*size + j] = Legal_pos;//If it's the last line, that means it's got a solution. if(i = = Size-1) { ++Count; Print (*CB); }//or search for the next queen's position down the line. Else_solve (Cb,i +1,0,Count);//No matter how many solutions are found in the following search, we need to //To find the Queen to move, the backtracking process may be correctBoard[i*size + j] = Candidate_pos; }//For the J position in each row, whether it is a can or cannot be placed //Queen's position, we have to continue searching backwards to exhaust all the solutions_solve (Cb,i,j +1,Count);}intSolve_eightqueen (intSize) {int Count=0; chessboard* cb = Create (size);//Start search from the first position_solve (&CB,0,0,Count);return Count;}
Test code:
int_tmain (intARGC, _tchar* argv[]) {cout<<"=============1============"<<endl;int Count= Solve_eightqueen (1); cout<<"Total:"<<Count<<endl; cout<<"=============2============"<<endl;Count= Solve_eightqueen (2); cout<<"Total:"<<Count<<endl; cout<<"=============3============"<<endl;Count= Solve_eightqueen (3); cout<<"Total:"<<Count<<endl; cout<<"=============4============"<<endl;Count= Solve_eightqueen (4); cout<<"Total:"<<Count<<endl; cout<<"=============5============"<<endl;Count= Solve_eightqueen (5); cout<<"Total:"<<Count<<endl;return 0;}
Program run:
The chessboard takes 1 to 5 o'clock to get the results.
Because when the checkerboard size is 8*8, there are too many results, there are 92, so only 4 results are captured as an example:
In fact, the algorithm above is a brute force solution that attempts to explore all permutations. According to a queen of each row of the row of a total of 8^8 species arrangement, that is, 2^24 = 16,777,216, which is a large number, but the program can be a very short time to get answers, mainly because the previous row of the search together to limit the search in some locations in the following row, This makes many permutations not to be inspected, thus allowing the problem to be solved quickly. The checkerboard size can be set to 12, then the total search space size becomes: 8916100448256 (8 trillion), and the time used:
This is not to say that backtracking is so good, but the problem is that the search results in front of it profoundly affect the search in the back, and backtracking is just right for solving such problems, which is a more natural way to think.
Topic 17: Eight Queens question