n Queens Problem--Backtracking Algorithm

1. What is backtracking

    The backtracking method is a method of selecting the optimal retrieval method, which is depth-prioritized retrieval according to the optimal selection conditions. If it is found that the node is not an optimal node or a dead node, go back to the previous step and select again. If you can advance, you will advance, if you don't advance, you will change, if you don't change, you will retreat.

2. Algorithmic elements

(1) First determine the form of the solution == solution space: the space composed of all possible solutions

(2) Imagine the organizational structure of the solution space (which can be simply understood as a tree)

(3) Explore the solution space (which contains an important concept: implicit constraints (pruning function: constraints + bound functions))

            Pruning function: Subtract the branches that do not get a feasible solution or an optimal solution

                        *Extended node: a node that is generating children

                        * Live node: a node that has been generated by itself, but the children have not yet been generated

                        * dead node: a node where all children have been spawned

3. Algorithm Design

(1) Define the solution space: the n queen problem is to define an n-tuple: {x1, x2, ........., xn}

(2) The organizational structure of the solution space: it can be regarded as an n-ary tree, and the depth of the tree is n.

(3) Retrieve the solution space:

        *Constraints if(x[t]==x[j] || tj==x[t]-x[j]) break;

        *Boundary conditions not required

        *Searching process: starting from the root, searching is carried out in a depth-first traversal manner. The root node is the live node and is the current extension node. During the expansion process, the current expansion node moves to a new node along the depth direction, and it is judged whether the node satisfies the implicit constraint. If (0), the node is a live node, and becomes an extension node, and continues to search one level deeper; if (1), it switches to the sibling node of the node and continues to search, if there is no sibling node or all After the search is completed, the node is a dead node, and the search is traced back to the parent node----until the root node becomes a dead node

4. Code (Interesting Learning Algorithm - Code in Chen Xiaoyu's Note)

#include<iostream>
#include<cmath>
#define M 105
using namespace std;

int n;
int x[M];
int countn;

bool Place(int t){
	bool ok=true;
	for(int j=1;j<t;j++){
		if(x[t]==x[j]||t-j==fabs(x[t]-x[j]))
		{
			ok=false;
			break;
		}
	}
	return ok;
}


void Backtrack(int t){
	if(t>n){
		countn++;
		for(int i=1;i<=n;i++) cout<<x[i]<<" ";
		cout<<endl;
		cout<<"—————————"<<endl;
	}
	else
		for(int i=1;i<=n;i++){
			x[t]=i;
			if(Place(t))
				Backtrack(t+1);
		}
}

int main(){
	cout<<"Please enter the number of queens: ";
	cin>>n;
	countn=0;
	Backtrack(1);
	cout<<"Number of answers:"<<countn<<endl;
	return 0;
}

5. Lao Tzu is still in a dazed state. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


Guess you like

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