Backtracking algorithm - n queens problem

What is the backtracking algorithm?
The backtracking method can generally solve the following problems:
      Combination problems, permutation problems, subset problems, chessboard problems and other problems ;
The n-queen problem is one of the chessboard problems ;
The problems to be solved by the backtracking method can be abstracted into a tree structure, which can be understood as an N-ary tree;
The backtracking method uses recursion to recursively find subsets in a set. The size of the set can be understood as the layer that requires recursion;
And when using recursion, there must be a termination condition , otherwise it will not be able to exit the function body and an error will occur. So this N-ary tree is limited;
The process of backtracking algorithm:
 
         1. Trace back the parameters entered by the function and the return value; it is a function body

         2. Termination condition; because it is recursive, there must be a termination condition

         3. Core backtracking algorithm

                 N-ary tree: (

                     for loop: horizontal traversal

                      Recursion: vertical traversal

                )

         4. Output return value

This is the core introduction to the backtracking algorithm

Let’s start looking for questions to get started! ! !

If it helps you, please give me a thumbs up! ! !    



Topic requirements

     Place n queens on an n*n chessboard so that they cannot attack each other. That is, no two queens can be in the same row, column or diagonal. How many ways are there to place them?

     Input format: 8

     Output format: 92

Question analysis

1. For the n queen chessboard, there are these rules

      1. Not in the same line

      2. Cannot be in the same column

      3. Cannot be on the same slash line

Normally, for a chessboard, we will use a two-dimensional array

But for this game rule, we can know that using a one-dimensional array is fine, because only one chess piece can be placed in each row

2. For these three rules, our core layer should have two judgments ( use one-dimensional arrays, no need to consider row problems )

      When you are preparing to place a queen in the current column, you need to determine:   

1. Is the current queen position in the same column      as the queen that has been placed before ? If so, change the column until the Nth column ( reaches the boundary ). You need to go back to the previous row of queens and change the column of the previous row of queens.

2. Is the current queen position on the same slope      as the previously placed queen ( i.e. 45° or 135° )? If so, change the column until the Nth column ( reaches the boundary ), and you need to go back to the previous row of queens. , changes the column of the queen of the previous row.

3. Termination conditions in the recursive process

     We can think about the backtracking algorithm. The most straightforward explanation is that the current road is blocked, what to do, go back, go to the previous position and change the direction;

      Isn't the termination condition that you have gone back to the first step and reached the last position of the first step? If there is no other choice, then it is over. Why stay here when there is no way to go? Hahahaha;

   

Code



#include<bits/stdc++.h> 

using namespace std;


int main(){
         int  n,sum=0;
         cin>>n;	
         int HANG= 0;//皇后个数,行数;	
         int LIE = 0;//皇后占据的列为;
         int queen[n] = {0};//储存皇后的位置,值为列;						
		
	  while(1){
		
        int AK = 0;		//攻击
		if(HANG== 1 && queen[0] == 7 && LIE == 7)break;//终止条件	
		  
		//判断是否在同一列,统一斜线
		for(int i=0;i<HANG;i++)
		{			
			if(queen[i] == LIE)AK = 1;	//同一列,攻击	
			if(HANG-i == queen[i]-LIE ||HANG-i == -(queen[i]-LIE) )AK = 1;//同一斜线,攻击					
		}
		  
		//判断可不可以放入
		if(AK == 0)
		{	//表示可以放置
			queen[HANG] = LIE;		
			HANG++;	LIE = 0;				
			if(HANG== 8)sum++;				    
		 }
	    else
		{			
			 LIE++;			
			 while(LIE>=8)
			 {	//回朔,上一个皇后往后移一格
			     HANG--;				
				 LIE = queen[HANG]+1;	
			 }
		}
   }
	cout<<sum<<endl;
	return 0;	
}

Creation is not easy, please leave comments, follow and like to avoid getting lost! ! !

Creation is not easy, please leave comments, follow and like to avoid getting lost! ! !

Creation is not easy, please leave comments, follow and like to avoid getting lost! ! !

Guess you like

Origin blog.csdn.net/weixin_59367964/article/details/127986711