N皇后

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;
    }


}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值