八皇后递归算法(Eight queens recursive algorithm)
First, the purpose and content of the experiment
Write a program to solve thessful, return true by referring to the parameter OK (otherwise returning false).
Void solve (int, I, bool&, OK);
After the queen is placed, if the i=8 is full then the recursive exit: otherwise, solve (i+1, OK); recursive call.
the preparation of the main function, first initialize a /zempty chessboard”, each element is a, B, C array are set to true (the queen said has not been placed in 8 rows, the current board of the 15 diagonal and 15 anti diagonal.). Then, execute the calling statement solve (1, OK) ; it is responsible for placing the queen on the 8 consecutive column starting at the first column; if successful, return true by referring to the parameter OK (otherwise returning false).
appendix
Source code 1 (output all 92 sets of solutions)
ftinclude "iostream, h""
Int LineNum [9];
Bool, a[9], b[15], c[15]:
Int totalmethod-0:
Void, print ();
Void solve (int, N, bool & OK);
Int main (int, argc, char*, argv[])
{
Bool ok=false:
For (int i=l: i<=8: i++)
A[i]=true;
For (int j=0; j<=14; j++)
(b[j]=true; c[j]=true;}
Solve (1, OK);
}
Void, print ()
{
Int, I, j:
Totalmethod++;
Cout<< "method:" <<totalmethod<<endl;
For (i=l: i<=8: i++)
{cout<< ”(" <〈i〈< " <<LineNum[i]<< ,z);}
Cout«endl;
For (i=l: i<=8; i++)
{
For (j=l: j<=8; j++)
{
八皇后递归算法Eightqueensrecursivealgorithm 来自淘豆网m.daumloan.com转载请标明出处.