Question of the eight Queens
First, test instructions analysis
The Queen in chess can move horizontally, vertically, and obliquely. How to place 8 Queens on an 8x8 board, so that any two queens are not on the same line, vertical bar, slash direction ? The eight Queens question is an old question that was raised by a chess player in 1848: placing eight queens on the 8x8 chess, so that they cannot attack each other, that is, any two queens cannot be in the same row, the same column, or the same slash, how to solve? Many mathematicians, represented by Gauss, have studied this problem successively. Later, when the computer came out, it was easy to solve the problem by computing the computer program.
Second, how to solve the eight queen problem?
The so-called recursive backtracking is essentially an enumeration method. This method starts from the first line of the board and attempts to place the first Queen, after which the second queen is placed on the second line of the Board, after the successful placement, recursively. If the current position cannot be placed, then move to the right one pane to try again, if the display is successful, then continue to recursion layer, placing the third queen ...
If a layer has seen all the squares and cannot be placed successfully, go back to the previous queen, move the previous queen to the right, and then recursively. If the eight queens are placed and conform to the rules, then one of the correct solutions is obtained. It's a bit abstract, let's take a look at the detailed process of recursive backtracking.
1. First level recursion, try to place the first Queen in the first row :
2. Second level recursion, try to place the second Queen in the second row (the first two blocks are blocked by a queen, only in the third block):
3. Third level recursion, try to place the third Queen in the third row (the first four blocks are blocked by the second queen, only in the fifth block):
4. Fourth level recursion, try to place the fourth Queen on Line fourth (the first block is blocked by the second Queen and only falls in the second block):
5. Fifth level recursion, try to place the fifth Queen on line Fifth (the first three blocks are blocked by the front queen and can only fall in the fourth lattice):
6. Since all the squares are "green", the sixth row has not been able to place the Queen, so go back and rearrange the fifth Queen to the eighth grid . :
7. The sixth line still has no way to put the Queen, the fifth line has been tried all over, so go back to line fourth , re-placed fourth Queen to seventh . :
8. Continue placing the fifth Queen, and so on ...
Three or eight Queen's question code implementation
Solving the eight Queen problem can be divided into two levels:
1. find out the first proper placement , i.e. depth-first traversal.
2. find out all the correct placement , that is, the breadth of priority traversal.
This article only describes how to find the first way to put it right. The specific code is as follows:
1 //"Eight Queens Problem backtracking"2#include <iostream>3 using namespacestd;4 Const intArsize =8;//the number is equal to a few queens. 5 intnum =0;6 voidSolveBOOLArr[arsize][arsize],introw);7 BOOLCheckBOOLArr[arsize][arsize],intRowintcolumn);8 voidOutPut (BOOLarr[arsize][arsize]);9 Ten intMain () One { A BOOLChessboard[arsize][arsize]; - //Array Initialization - for(Auto &I:chessboard) the { - for(Auto &j:i) - { -j =false; + } - } +Solve (chessboard,0); Acout <<"Eight Queen's question in common"<< Num <<"Kind of Solution! "<<Endl; atSystem"Pause"); - return 0; - } - //Backtracking Method - voidSolveBOOLArr[arsize][arsize],introw) - { in for(intColumn =0; Column < arsize; ++column) - { toArr[row][column] =true; + if(check (arr, row, column)) - { the if(Row +1==arsize) * { $ OutPut (arr);Panax Notoginseng } - Else the { +Solve (arr, row +1); A } the } +Arr[row][column] =false; - } $ } $ //determine if the Queen's Landing is compliant - BOOLCheckBOOLArr[arsize][arsize],intRowintcolumn) - { the if(Row = =0) - {Wuyi return true; the } - intI, J; Wu //Judging whether there is a conflict in portrait - for(i =0; i < row; ++i) About { $ if(Arr[i][column]) - { - return false; - } A } +i = row-1; thej = Column-1; - //determine if there is a conflict in a forward diagonal line $ while(I >=0&& J >=0) the { the if(Arr[i][j]) the { the return false; - } in--i; the--J; the } Abouti = row-1; thej = column +1; the //determine if there is a conflict in negative diagonal diagonal the while(I >=0&& J <= Arsize-1) + { - if(Arr[i][j]) the {Bayi return false; the } the--i; -++J; - } the return true; the } the//print each of the correct solutions the voidOutPut (BOOLArr[arsize][arsize]) - { the++num; thecout <<"**********************"<< Num <<"*********************"<<Endl; the for(inti =0; i < arsize; ++i)94 { the for(intj =0; J < Arsize; ++j) the { thecout << Arr[i][j] <<" ";98 } Aboutcout <<Endl; - }101cout <<"*********************************************"<<Endl;102}
The part of the output is as follows:
Resources:
Http://www.cnblogs.com/yonggandefeng/p/6275861.html
An algorithm problem-eight Queen question (C + + implementation)