ACM_N Queen problem

N queens problem

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

N queens are placed on the N*N square chessboard so that they do not attack each other (that is, any 2 queens are not allowed to be in the same row, in the same column, nor on a diagonal line that forms a 45-angle with the border of the chessboard. .
Your task is to find, for a given N, how many legal placements there are.

Input:

There are several lines, and each line has a positive integer N≤12, which represents the number of chessboards and queens; if N=0, it means the end.

Output:

There are several lines, each with a positive integer representing the different placement numbers of queens corresponding to the input line.

Sample Input:

1
8
5
0

Sample Output:

1
92
10 
Problem-solving ideas: N queens problem that no two queens can be on the same horizontal line, vertical line or diagonal line. Solving this problem involves a heuristic + backtracking algorithm, and recursion can clearly show the idea. In the process of testing, the placement of the queen needs to check whether his position conflicts with the already placed queen. For this purpose, we need to check the function place() to check whether the current position of the queen to be placed is not with other already placed queens. Queen clashes. Suppose two queens are placed at positions (i, j) and (k, l), obviously, the two queens are on the same diagonal if and only if |ik|=|jl|. (1) First check from the first position, if it cannot be placed, then check the second position of the line, and check in turn, until a place where a queen can be placed is found on the line, then save the current state, go to the next line and repeat the above method retrieval.  (2) If a queen cannot be placed in all the positions of the line, it means that the position of the queen in the previous line cannot allow all the queens to find their own suitable positions, so it is necessary to go back to the previous line and re-check the position after the queen . Location.
AC code:
1 #include<bits/stdc++.h>
 2  using  namespace std;
 3  int queen[ 15 ],cnt,n;     // Store the column number of each queen, cnt is the number of solutions 
4  bool place( int j){      /* Check if the queen can be placed on the row and diagonal */ 
5      for ( int i= 0 ;i<j;++ i){
 6          if ((queen[i]==queen[j]) || ( abs(queen[i]-queen[j])==abs(j- i)))
 7              return  false ;
 8      }
 9      return  true ;
 10 }
 11  void QueenSet( int row){ /* Try back to the queen position, row is the abscissa */ 
12      for ( int col= 0 ;col<n;++col){ // First place the queen in the 0th column The position, for the first time, is definitely true 
13          queen[row]=col; /* Place the queen to the current loop position */ 
14          if (place(row)){
 15              if (row==n- 1 )++cnt; /* If all are placed, add 1 to the number of solutions */ 
16              else QueenSet(row+ 1 ); /* Otherwise continue to place the next queen */ 
17          }
 18     }
 19  }
 20  int main(){
 21      while (cin>>n && n){
 22          cnt= 0 ; // Number of solutions 
23          QueenSet( 0 ); /* Try in sequence starting from abscissa 0 */ 
24          cout<<cnt<< endl;
 25      }
 26      return  0 ;
 27 }
 

Guess you like

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