import java.util.ArrayList;
import java.util.List;
public class Queen2 {
static class point {
int x;
int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
int n = 8;
List<List<String>> s = QueenN(n);
System.out.println("在 "+n+" 个皇后的情况下"+"总共有 "+s.size()+" 种解法");
for(List<String> s1 : s){
System.out.println("=============================");
for(String a : s1){
System.out.println(a);
}
}
}
//获取N皇后方案
public static List<List<String>> QueenN(int n) {
List<point> list = new ArrayList<>();
List<List<point>> lists = new ArrayList<>();
method(0, n, list, lists);
return change(n, lists);
}
//搜索和回溯
public static void method(int row, int n, List<point> list, List<List<point>> lists) {
//复制加入方案集合
if (row == n) {
List<point> l = new ArrayList<>();
for (point p : list) {
l.add(p);
}
lists.add(l);
return;
}
for (int i = 0; i < n; i++) {
//如果不冲突
if (isVaild(row, i, list)) {
list.add(new point(row, i));
method(row + 1, n, list, lists);
list.remove(list.size() - 1);
}
}
}
//判断是否与已确定皇后冲突 没有返回true
public static boolean isVaild(int row, int col, List<point> list) {
for (point p : list) {
if ((p.y == col&&Math.abs(p.y-col)<=7) ||
(p.x + p.y == col + row&&Math.abs(p.x-row)<=7) ||
(p.x - p.y == row - col&&Math.abs(p.x-row)<=7)) {
return false;
}
}
return true;
}
public static List<List<String>> change(int n, List<List<point>> lists) {
List<List<String>> res = new ArrayList<>();
//获得方案
for (List<point> list : lists) {
StringBuilder[] strings = new StringBuilder[n];
//新建顺序表保存String
List<String> list1 = new ArrayList<>();
//全部都初始化为点
for(int i = 0 ; i <strings.length;i++){
String s1 = "";
for(int j = 0 ; j <n;j++){
s1 = s1+".";
}
strings[i] = new StringBuilder().append(s1);
}
for (point p : list) {
//在此处替换对应坐标的点为Q
for (int i = 0; i < n; i++) {
if(p.x==i){
//(index,index+num,String s) 左闭右开区间内全换成s
strings[i].replace(p.y,p.y+1,"Q");
}
}
}
for(StringBuilder s : strings){
list1.add(s.toString());
}
//结束后将list1加入方案集合
res.add(list1);
}
//遍历完所有方案后返回
return res;
}
}
失败方法:
用boolean类型二维数组存储方案时,出错
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Queen {
}
//发现用顺序表存储boolean类型二维数组时 在其他方法遍历会初始化为false
class Solution {
public static void main(String[] args) {
solveNQueens(4);
}
static List<boolean[][]> bools = new ArrayList<>();
public static List<List<String>> solveNQueens(int n) {
boolean[][] bool = new boolean[n][n];
method(0,n,bool);
for(boolean[][] b : bools){
System.out.println("============加入顺序表后===============");
for(boolean[] a : b){
for(boolean c :a){
System.out.print(c+" ");
}
System.out.println();
}
}
return change(n,bools);
}
public static List<List<String>> change(int n,List<boolean[][]> bools) {
List<List<String>> res = new ArrayList<>();
//拿到方案
for(boolean[][] bool : bools){
List<String> list = new ArrayList<>();
for(int i = 0 ; i <bool.length;i++){
String str = "";
for(int j = 0 ; j<bool[i].length;j++){
if(bool[i][j])
str+="Q";
str+=".";
}
list.add(str);
}
res.add(list);
}
return res;
}
public static void method(int row,int n,boolean[][] bool){
if(row==n){
boolean[][] bol = bool;
bools.add(bol);
// System.out.println("=========bol的内容================");
// for(boolean[] a : bol){
// for(boolean c :a){
// System.out.print(c+" ");
// }
// System.out.println();
// }
return;
}
for(int i = 0 ; i < n ;i++){
if(isVailed(row,i,bool)){
bool[row][i] = true;
method(row+1,n,bool);
bool[row][i] = false;
}
}
}
public static boolean isVailed(int row, int col, boolean[][] bool) {
for(int i = 0 ; i <bool.length;i++){
for(int j = 0 ; j<bool[i].length;j++){
if(bool[i][j]&&(
j==col||
i+j==col+row||
j-i==col-row)){
return false;
}
}
}
return true;
}
}