1、问题描述
n-皇后问题是指将 n 个皇后放在 n∗n 的国际象棋棋盘上,使得皇后不能相互攻击到,即任意两个皇后都不能处于同一行、同一列或同一斜线上。
现在给定整数n,请你输出所有的满足条件的棋子摆法。
输入格式
共一行,包含整数n。
输出格式
每个解决方案占n行,每行输出一个长度为n的字符串,用来表示完整的棋盘状态。
其中”.”表示某一个位置的方格状态为空,”Q”表示某一个位置的方格上摆着皇后。
每个方案输出完成后,输出一个空行。
输出方案的顺序任意,只要不重复且没有遗漏即可。
数据范围
1≤n≤91≤n≤9
输入样例:
4
输出样例:
.Q.. ...Q Q... ..Q. ..Q. Q... ...Q .Q..
2、分析
3、代码
import java.util.*;
import java.io.*;
public class Main{
static int N = 10;
static boolean[] cols = new boolean[N];
static boolean[] dg = new boolean[2 * N]; //对角线条数:2*N -1
static boolean[] fdg = new boolean[2 * N];
static List<List<String>> res;
static char[][] blocks;
static int n;
public static void main(String[] args){
Scanner in = new Scanner(new InputStreamReader(System.in));
n = in.nextInt();
blocks = new char[n][n];
res = new ArrayList<>();
List<String> path = new ArrayList<>();
/*for(int i = 0;i < n;i ++){
for(int j = 0;j < n;j ++){
char[i][j] = '.';
}
}*/
dfs(0);
//System.out.println("res.size " + res.size());
}
public static void dfs(int u){ //u代表行
List<String> path = new ArrayList<>();
if(u == n){
for(int i = 0;i < n;i ++){
//path.add(new String(blocks[i]));
System.out.print(new String(blocks[i]));
System.out.println();
}
System.out.println();
res.add(path);
return;
}
Arrays.fill(blocks[u], '.');
for(int i = 0;i < n;i ++){ // i代表列
if(!cols[i] && !dg[i - u + n] && !fdg[i + u]) {
blocks[u][i] = 'Q';
cols[i] = dg[i - u + n] = fdg[i + u] = true;
dfs(u + 1);
blocks[u][i] = '.';
cols[i] = dg[i - u + n] = fdg[i + u] = false;
}
}
}
}
4、LeetCode 51. N-Queens N皇后
链接:https://leetcode-cn.com/problems/n-queens/
class Solution {
List<List<String>> res = new ArrayList<>();
public List<List<String>> solveNQueens(int n) {
boolean[] col = new boolean[n];
boolean[] dg = new boolean[2*n];
boolean[] fdg = new boolean[2*n];
char[][] queen = new char[n][n];
dfs(queen, col, dg, fdg, n, 0);
return res;
}
public void dfs(char[][] queen, boolean[] col, boolean[] dg, boolean[] fdg, int n, int u){
ArrayList<String> path = new ArrayList<>();
if(n == u){
for(int i = 0;i < n;i ++)
path.add(new String(queen[i]));
res.add(path);
return;
}
Arrays.fill(queen[u], '.');
for(int i = 0;i < n;i ++){
if(!col[i] && !dg[i - u + n] && !fdg[i + u]) {
queen[u][i] = 'Q';
col[i] = dg[i - u + n] = fdg[i + u] = true;
dfs(queen, col, dg, fdg, n, u+1);
queen[u][i] = '.';
col[i] = dg[i - u + n] = fdg[i + u] = false;
}
}
}
}
5、n皇后问题另一种深搜顺序
我们考虑每一位置都有两种情况:即放皇后或者不放皇后 。对每一格子都采取这种思考方式进行DFS
代码
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
static int N = 10;
static char[][] b;
static boolean[] row = new boolean[N];
static boolean[] col = new boolean[N];
static boolean[] dg = new boolean[2 * N];
static boolean[] fdg = new boolean[2 * N];
static void dfs(int n, int x, int y, int cnt){
if(y == n){
y = 0;
x ++;
}
if(x == n){
if(cnt == n){
for(int i = 0;i < n;i ++){
for(int j = 0;j < n;j ++){
System.out.print(b[i][j] );
}
System.out.println();
}
System.out.println();
}
return;
}
/*
*每一个格子都是放或者不放
* */
dfs(n, x, y+1, cnt);
if(!row[x] && !col[y] && !dg[y - x + n] && !fdg[y + x]){
b[x][y] = 'Q';
row[x] = col[y] = dg[y - x + n] = fdg[y + x] = true;
//这里注意一定是 : y+1 cnt+1,而不是 y++
dfs(n, x, y+1, cnt+1);
row[x] = col[y] = dg[y - x + n] = fdg[y + x] = false; //因为回溯的时候用到的是y
b[x][y] = '.';
}
}
public static void main(String[] args) {
Scanner in = new Scanner(new InputStreamReader(System.in));
int n = in.nextInt();
b = new char[n][n];
for(int i = 0;i < n;i ++){
for(int j = 0;j < n;j ++){
b[i][j] = '.';
}
}
dfs(n, 0, 0, 0);
}
}