自己写的求解92中8皇后问题方法:

自己写的求解92中8皇后问题方法:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class eightQuene {
    static private int [][]cheeseBoard = new int[8][8];
    static private List <int[][]> AllEightQuene=new ArrayList<>();
    public static void main(String[] args) {
        System.out.println(calEightQuene(AllEightQuene));
        int i=0;
        for (int [][]t:AllEightQuene){
            System.out.println("第"+ ++i + "种方案:");
            System.out.println(Arrays.deepToString(t));
        };
    }

    /**
     * 计算8皇后问题
     * @return
     */
    static private boolean calEightQuene(List <int[][]> allEightQuene)
    {
        List<Integer> [] lineCanPut=new List[8];   //存储每一行可以落子的List数组
        for (int hang = 0; hang < 8; hang++) {
            if (hang<0)    //所有可能都找到 或者程序出错
                break;
            if (lineCanPut[hang]==null)
            {
                lineCanPut[hang]= checkIifThisLineCanPutQuene(hang,cheeseBoard);
            }

            if (lineCanPut[hang].size()>0) {
                luoZi(hang, lineCanPut[hang].get(0));

                if (hang==7)   //找到结果,存储
                {
                    allEightQuene.add(deepCopy(cheeseBoard));
                    Arrays.fill(cheeseBoard[7],0);    //清理最后一行
                    lineCanPut[7].remove(0);          //删掉最后一行的上一种可能
                    hang--;                        //再次计算最后一行
                    continue;
                }

                continue;    //该行已经落子,继续落下一行
            }else {    //该行已经无法落子,删掉上一行中的无法使用的元素,清理board中的该行和上一行
                if (hang-1<0)   //已经找到结果或者程序报错
                    break;
                lineCanPut[hang-1].remove(0);
                lineCanPut[hang]=null;                                    //删掉存储的本行的落子数列
                Arrays.fill(cheeseBoard[hang],0);   //清理board中的该行和上一行
                Arrays.fill(cheeseBoard[hang-1],0);
                hang=hang-2;             //先-2  for循环再+1  相当于返回到了上一行

            }
        }

        return true;
    }


    /**
     *
     * @param hang
     * @param cheeseBoard
     * @return 这一行可以放置皇后的位置坐标
     */
    static private List<Integer> checkIifThisLineCanPutQuene(int hang, int [][]cheeseBoard)
    {
        List <Integer> result = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            if (checkIfThisPointCanPutQuene(hang,i,cheeseBoard))
            {
                result.add(i);
            }
        }
        return result;
    }


    /**
     * 检查当前点可否放置皇后
     * @param hang
     * @param lie
     * @param cheeseBoard
     * @return
     */
    static private boolean checkIfThisPointCanPutQuene(int hang,int lie,int [][]cheeseBoard)
    {
        //判断竖直方向
        for (int i = 0; i < 8; i++) {
            if (i==hang)  //跳过自己和自己的判断
            {
                continue;
            }
            if (cheeseBoard[i][lie]==1)  //发现同一列上已经有了Quene,Return False
            {
                return false;
            }

        }
        //判断斜向 右下
        for (int i=hang,j=lie;i<8 && j<8;i++,j++)
        {
            if (i==hang && j==lie)
                continue;
            if (cheeseBoard[i][j]==1)
            {
                return false;
            }
        }
        //判断斜向 左上
        for (int i=hang,j=lie;i>=0 && j>=0;i--,j--)
        {
            if (i==hang && j==lie)
                continue;
            if (cheeseBoard[i][j]==1)
            {
                return false;
            }
        }
        //判断斜向 左下
        for (int i=hang,j=lie;i<8 && j>=0;i++,j--)
        {
            if (i==hang && j==lie)
                continue;
            if (cheeseBoard[i][j]==1)
            {
                return false;
            }
        }
        //判断斜向 右上
        for (int i=hang,j=lie;i>=0 && j<8;i--,j++)
        {
            if (i==hang && j==lie)
                continue;
            if (cheeseBoard[i][j]==1)
            {
                return false;
            }
        }



        return true;    //返回这个点可以放皇后
    }

    /**
     * 落子
     *
     */
    static private void luoZi(int hang,int lie)
    {
        cheeseBoard[hang][lie]=1;
    }

    /**
     * 提子
     * @param hang
     * @param lie
     */
    static private void tiZi(int hang,int lie)
    {
        cheeseBoard[hang][lie]=0;
    }
    /**
     * deep copy
     *
     */
    static private int [][]deepCopy(int [][]t)
    {
        int [][]result=new int[t.length][];
        for (int i = 0; i < result.length; i++) {
            result[i]=t[i].clone();
        }
        return result;
    }
}

运行结果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值