Eight Queens (dfs)

Problem Description
Examine a 6 x 6 checkers board as follows, with six pieces placed on the board such that each row and column has one and only one, each diagonal (including all parallels of the two main diagonals) line) has at most one pawn.

insert image description here

The above layout can be described by the sequence 2 4 6 1 3 5, the ith number indicates that there is a pawn in the corresponding position on the ith 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 checkers placement. Please write a program to find solutions for all checkers placements. and output them in the sequence method above. The solutions are in lexicographical order. Please output the first 3 solutions. The last line is the total number of solutions.

//The following words are from usaco official and do not represent Luogu's point of view

Special Note: For larger N (chessboard size N x N) your program should improve more efficiently. Don't calculate all the solutions in advance and just output (or find a formula for it)

Input and output format
Input format:
A number N (6 <= N <= 13) means the board is N x N in size.

Output format:
The first three lines are the first three solutions, and the two numbers of each solution are separated by a space. The fourth line has only one number, which represents the total number of solutions.

input sample

6

Sample output

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

Problem- solving ideas:
in the code,

The array arr[line]=i indicates that there is a pawn in the i-th column of the line line, ensuring that there is only one pawn in each line;

The array check ensures that there is only one pawn per column and each diagonal line. The specific mechanism is as follows, without some strange and incomprehensible formulas:

check[0] stores the number of rows of pawns, each time ans[line]=i is performed, so that check[0][i] is marked as used;
check[1] and check[2] store the pawns on the diagonal Distribution: As shown in the
figure,
insert image description here
for a diagonal line from upper right to lower left, the coordinates of the pieces on it should satisfy x+y as a certain value;

For a diagonal line from top left to bottom right, the coordinates of the pieces on it should satisfy xy as a certain value. In order to avoid the generation of negative numbers, x-y+n is used in the code to store the numbers. Readers can study the specific effects by themselves.

for the statement

if((!check[0][i])&&(!check[1][line+i])&&(!check[2][line-i+n]))

As long as all three numbers are used, place a pawn at ans[line]=i, mark the corresponding number in the check array as used, and search the next line.

Since the problem requires the output of the first 3 sets of solutions, the result is not output when the counter sum>3, and finally the number of final solutions is output in the main function.

#include<cstdio>
#include<iostream>
using namespace std;
int arr[14],check[3][28] = {0},ans = 0,n;
void dfs(int line){
    if(line > n){
        ans++;
        if(ans > 3) return;
        else{
			for(int i = 1;i <= n;i++){
            	cout << arr[i] << " ";
        	}
            cout << endl; 
            return;
        }
    }
    for(int i = 1;i <= n;i++){
        if((!check[0][i]) && (!check[1][line+i]) && (!check[2][line-i+n])){
            arr[line] = i;
            check[0][i] = 1; check[1][line+i] = 1; check[2][line-i+n] = 1;
            dfs(line+1);
            check[0][i] = 0; check[1][line+i] = 0; check[2][line-i+n] = 0;
        }
    }
}
int main(){
    cin >> n;
    dfs(1);
    cout << ans;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325736882&siteId=291194637