Algorithm Experiment 4: Eight Queens Problem

Experiment 3: Greedy Algorithm: Traveling Salesman Problem (TSP)
Problem description: There are several cities, and the transportation fee between any two cities is determined. Now a traveling salesman starts from a certain city and passes through each city only once. Finally, Go back to the original city and determine a route to minimize travel expenses.
Experiment 4: Eight Queens Problem.
Problem description: N2 squares are arranged in a square with N rows and N columns, which is called an N-yuan chessboard. Place N queens on the N-yuan chessboard. If two queens are located in the same row or column or the same diagonal on the N-yuan chessboard On the line (slope ±1), it is said that they are attacking each other. Try to design an algorithm to find all the layouts that make the N queens not attack each other.
Problem-solving ideas;

  • First of all: write a function to determine whether the position of the k-th queen meets the requirements.
    Because the loop increment variable i can be restricted to not be in the same row, in this case, X[i] is used to restrict the not in the same column, that is: x≠X[i] is
    on the same main diagonal: the row-column values ​​are all equal iX[i ]=kX[k] is
    on the same anti-diagonal line: the values ​​of row + column are all equal i+X[i]=k+X[k]
    can be derived from the general formula of main diagonal and anti-diagonal line: |X[ i]-X[k]|=|ik|, if it is not on a diagonal, you need to ensure that ABS (|X[i]-X[k]|)≠ABS(ik)
    from i=1 to k if If the queen is not in the same row or column or on the diagonal, it is true, otherwise it is not true.

  • Next, I simulated the execution of the main function according to the pseudo code in the algorithm book.

  • The main function idea is as follows:
    step1: define row variable k and column variable X[k], k assign initial value 1, X[k] assign initial value 0
    step2: define while (k>0) loop, because k initial value is 1 , So when the backtracking comes back to k=0, the program ends.
    Step3: Increment X[k] by 1, and define a while (X[k]<=n also substandard) loop to determine whether the position of the queen meets the standard. At this time, the column will be incremented by 1.
    step4: If the position is found and X[k]<=n, first judge whether the n queens have positions (k=n), if there are positions, then print the array position, otherwise k++, the column is cleared. If X[k]>n, then k is decremented for backtracking.
    The idea is very clear and specific, but there is still a problem in writing the code. The problem is that {} is not added after an else, and another problem is that there is no return to mark the value after the function is called.
    code show as below:

#include <stdio.h>
#include <math.h>
int place(int k);
int k=1,x[100]={
    
    0};
int main(){
    
    
	int n,sum=0;
	printf("请输入皇后数量:"); 
	scanf("%d",&n);
	while(k>0){
    
    
		x[k]++;
		while(x[k]<=n&&place(k)==0){
    
    
			x[k]++; 
		}
		if(x[k]<=n){
    
    
			if(n==k){
    
    
				//printf("成立\n");
				sum++;
			}
			else{
    
    
				k++;x[k]=0;
			}
		}
		else k--; 
	}
	printf("sum=%d\n",sum);
}
int place(int k){
    
    
	int i=1,t=1;
	while(i<k){
    
    
		if(abs(x[i]-x[k])==abs(i-k))
		t=0;
		if(x[i]==x[k])
		t=0;
		i++;
	}
	return t;
}

The code to print the position of the queen is as follows:

#include <stdio.h>
#include <math.h>
int place(int k);
int k=1,x[100]={
    
    0};
int main(){
    
    
	int n,sum=0;
	printf("请输入皇后数量:"); 
	scanf("%d",&n);
	while(k>0){
    
    
		x[k]++;
		while(x[k]<=n&&place(k)==0){
    
    
			x[k]++; 
		}
		if(x[k]<=n){
    
    
			if(n==k){
    
    
				for(int i=1;i<=n;i++){
    
    
					for(int j=1;j<=n;j++){
    
    
						if(x[i]==j)
						printf("#");
						else printf("*");
					}
					printf("\n");
				}
				printf("\n");
				sum++;
			}
			else{
    
    
				k++;x[k]=0;
			}
		}
		else k--; 
	}
	printf("sum=%d\n",sum);
}
int place(int k){
    
    
	int i=1,t=1;
	while(i<k){
    
    
		if(abs(x[i]-x[k])==abs(i-k))
		t=0;
		if(x[i]==x[k])
		t=0;
		i++;
	}
	return t;
}

Guess you like

Origin blog.csdn.net/CSDN_Ysu/article/details/109075856