Detailed eight queens placement problem

The original question is in Luogu P1219; the
question describes
a checker board with 6 \times 66×6 as follows. There are six chess pieces placed on the board, so that each row and each column has one and only one, and each diagonal (including There is at most one piece on all parallel lines of the two main diagonals

Insert picture description here
The above layout can be described by the sequence 2 4 6 1 3 5, the iith number indicates that there is a pawn in the corresponding position of the iith row, as follows:

Line number 1 2 3 4 5 6

Column number 2 4 6 1 3 5

This is just a solution for the placement of the pieces. Please make a program to find the solution of all the chess pieces.
And output them in the above sequence method, and the solutions are arranged in lexicographic order.
Please output the first 3 solutions. The last line is the total number of solutions.

Input format
One positive integer n per line, indicating that the chessboard is m×n in size.

Output format The
first three lines are the first three solutions, and the two numbers in each solution are separated by a space. There is only one number in the fourth line, which indicates the total number of solutions.
Input and output sample
input
6
output
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
The following question is roughly what I understand:
Insert picture description here

//O is the position where the queen X cannot be placed.
Assuming that the first row (i) and the fourth column (k) is the queen, then the queen cannot be placed horizontally and vertically. The diagonal line from top left to bottom right and the diagonal line from top right to bottom left are also Can’t put the
core code:
if (!lie [k] && !left1[k+x] && !right1[x-k+n])
line, we can reduce the complexity at one time, lie
can be found by looking at the picture i+ k can find the diagonal from top left to bottom right.
Similarly, xk is the same, why add n, because xk may be out of bounds;
I use dfs(x) as the line.

Please see the code below
#include
using namespace std;

#define a 40
int rooms [a];
bool lie[a];
bool left1[a];
bool right1[a];
int n,cnt =0;
void av()
{
if (cnt <=3)
{
for (int i=1; i<=n; i++)
cout << rooms [i]<<’ ';
cout <<endl;
}
}
void dfs (int x)
{
if (x>n)
{
cnt ++;
av();
return ;
}
for (int k=1; k<=n; k++)
if (!lie [k] && !left1[k+x] && !right1[x-k+n])
{
lie [k]=1;
left1[k+x]=1;
right1[x-k+n]=1;
rooms [x]=k;
dfs (x+1);
lie [k]=0;
left1[k+x]=0;
right1[x-k+n]=0;
}
}
int main ()
{ cin >> n; dfs (1); cout << cnt; return 0; } Here is the traceback: lie [k]=1; left1[ k+x]=1; right1[x-k+n]=1; rooms [x]=k; dfs (x+1); lie [k]=0; left1[k+x]=0; right1[ x-k+n]=0; this step cannot be placed back to the previous step and repositioned; the concept is roughly that this way is not working, go back to the previous node to see if there is another way to go, if not, just go back Node, that’s why lie [k]=1, and then lie [k]=0;















Guess you like

Origin blog.csdn.net/qq_45531709/article/details/107668208