Eight queens Problem: put eight queens on the 8*8 lattice board. Any two queens cannot be in the same row, in the same column, or on the same diagonal line. There are several ways to solve this problem.
Queen N problem: Put N queens on the board of N * n grids. Any two queens cannot be in the same row, in the same column, or on the same diagonal line. There are several methods to solve this problem.
- # Include "stdafx. H"
- # Include <iostream>
- # Include <fstream>
- Using namespace STD;
- // Non-recursion
- Void Queen (int n)
- {
- Int * c = new int [N]; // records the column status
- Int * D1 = new int [2 * n-1]; // records the positive/diagonal state.
- Int * D2 = new int [2 * n-1]; // records the negative diagonal state.
- Int * lc = new int [N]; // records the number of columns placed by the Queen on the nth node.
- Int ** Queen = new int * [n]; // chessboard
- For (int I = 0; I <n; I ++) // Initialization
- {
- Queen [I] = new int [n];
- For (int j = 0; j <n; j ++)
- {
- Queen [I] [j] = 0;
- }
- C [I] = 0;
- D1 [I] = 0;
- D1 [2 * n-i-2] = 0;
- D2 [I] = 0;
- D2 [2 * n-i-2] = 0;
- }
- Int line = 0; // line
- Int column = 0; // column
- _ Int64 num = 0; // number of items that can be placed
- _ Int64 count = 0; // number of cycles
- While (line> = 0)
- {
- While (column <n)
- {
- Count ++;
- If (c [column] = 0 & d1 [line-column + n-1] = 0 & d2 [line + column] = 0) // if a column, true if no plus or minus diagonal lines are placed
- {
- If (line = N-1) // if it reaches the last line, it is true
- {
- Queen [Line] [column] = 1;
- Num ++; // Add 1 to the placement quantity
- Queen [Line] [column] = 0;
- Break;
- }
- Else
- {
- // Record the placement status
- Queen [Line] [column] = 1;
- C [column] = 1;
- D1 [Line-column + n-1] = 1;
- D2 [LINE + column] = 1;
- LC [Line] = column;
- Line ++; // enter the next row, starting from column 0th
- Column = 0;
- }
- }
- Else
- Column ++;
- }
- Count ++;
- // If all cells in this row cannot be placed, move the row back and traverse the row starting from the next column placed in the previous row.
- Line --;
- If (line> = 0)
- {
- Column = Lc [Line];
- Queen [Line] [column] = 0;
- C [column] = 0;
- D1 [Line-column + n-1] = 0;
- D2 [line + column] = 0;
- Column ++;
- }
- }
- Cout <num <'/t' <count <endl ;;
- }
- // Recursion
- Void queen1 (int I, int ** Queen, int * a, int * d1, int * d2, int & n, int & QueenNumber ,__ int64 & count)
- {
- Int iColumn;
- For (iColumn = 0; iColumn <n; iColumn ++)
- {
- Count ++;
- If (a [iColumn] = 0 & d1 [I-iColumn + n-1] = 0 & d2 [I + iColumn] = 0)
- {
- Queen [I] [iColumn] = 1;
- A [iColumn] = 1;
- D1 [I-iColumn + n-1] = 1;
- D2 [I + iColumn] = 1;
- If (I <n-1)
- Queen1 (I + 1, Queen, a, d1, d2, n, QueenNumber, count );
- Else
- {
- QueenNumber ++;
- /*
- Fstream outstuf;
- Outstuf. open ("out.txt", ios: out | ios: app );
- For (int I = 0; I <8; I ++)
- {
- For (int j = 0; j <8; j ++)
- {
- Outstuf <Queen [I] [j] <"";
- }
- Outstuf <"/r/n ";
- }
- Outstuf <"/r/n ";
- Outstuf. close ();*/
- }
- Queen [I] [iColumn] = 0;
- A [iColumn] = 0;
- D1 [I-iColumn + n-1] = 0;
- D2 [I + iColumn] = 0;
- }
- }
- }
- Void queendg (int n)
- {
- Int * c = new int [n];
- Int * d1 = new int [2 * n-1];
- Int * d2 = new int [2 * n-1];
- Int ** Queen = new int * [n];
- For (int I = 0; I <n; I ++)
- {
- Queen [I] = new int [n];
- For (int j = 0; j <n; j ++)
- {
- Queen [I] [j] = 0;
- }
- C [I] = 0;
- D1 [I] = 0;
- D1 [2 * n-i-2] = 0;
- D2 [I] = 0;
- D2 [2 * n-i-2] = 0;
- }
- Int QueenNumber = 0;
- _ Int64 count = 0;
- Queen1 (0, Queen, c, d1, d2, n, QueenNumber, count );
- Cout <QueenNumber <'/t' <count <endl ;;
- }
- Int _ tmain (int argc, _ TCHAR * argv [])
- {
- Queendg (8 );
- Queen (8 );
- Return 0;
- }
Tested
There are 14772512 methods for putting Queen (16) in total, in 206 seconds
A total of 14772512 queendg (16) release methods, with a total time of 248 seconds