N Queen Problem: Java Recursive Implementation

question:

Place n queens on an n

parse

When you get the problem, first consider the storage method. Using a one-dimensional array to represent the chessboard can greatly reduce the storage space.
It can be seen from the meaning of the question that there must be only one queen in each row, so it can be placed row by row during recursion.
When placing the queen of the current row (row K), you should check whether the queens of rows 0 to k-1 conflict with the current position.
If the column numbers are the same, there is a conflict; if the column spacing between the two queens is equal to the row spacing, it means that they are on the same diagonal, and there is a conflict.
If there is no conflict with the previous line, it can be placed and the recursion continues to the next line.

code

import java.util.Scanner;
public class N_Queen {
    
    
	public static void main(String[] args) {
    
    
		Scanner nscan = new Scanner(System.in);
		int n = nscan.nextInt();
		//用一维数组表示棋盘,下标为行,值为每一行皇后所在的列
		int a[] = new int[n];
		Q q = new Q();
		q.digui(a, 0, n);
	}
}

class Q {
    
    
	//k代表此时要摆放皇后所在的行数,也就是说0到k-1行已经摆好了
	public void digui(int a[], int k, int n) {
    
    
		if (k == n) {
    
    
			//全都放好了,输出结果
			for (int row : a) {
    
    
				System.out.print(row + " ");
			}
			System.out.println();
		}
		//i代表第k行的列数
		for (int i = 0; i < n; i++) {
    
    
			int j;
			//看0 - k-1行有没有冲突的
			for (j = 0; j < k; j++) {
    
    
				if (a[j] == i || Math.abs(j-k) == Math.abs(a[j]-i)) {
    
    
					break;
				}
			}
			//跟前面的皇后都没有冲突,可以放置
			if (j == k) {
    
    
				a[k] = i;
				digui(a, k+1, n); //放置下一枚皇后
			}
		}
	}
}

Finally, why is every situation being output? Because our recursion is inside the for loop, recursion is performed at every position in each line, so all correct answers can be output.

Guess you like

Origin blog.csdn.net/weixin_43390123/article/details/121833925