Java递归解决八皇后问题

在从头开始自学Java基础,由于之前学习python和C的时候就对递归理解以及使用不是太熟练,所以在自己编写解决“八皇后”的时候费了好大的力气。盘了很久的逻辑才发现,原来从开就不是递归逻辑的问题,而是判断条件一直有一些大大小小的毛病。但是最终还是让我改了出来。

本文解决八皇后问题是使用循环遍历判定条件的方法,首先固定住前七行的一个正确点,然后循环遍历最后一行所有的位置点,找到在前七行固定的情况下的所有可行解。当遍历结束后,使用递归回溯到第七行,在第七行找到第二个可行的位置点,然后再次去遍历最后一行。

通过不断递归回溯遍历完成第一行后,则所有的解法就已经找到了。

(本文使用的是二维数组表示棋盘,其实更简洁的方式,使用长度为8的一维数组,数组中的每个位置取值为1~8,表示每一行棋子所处的列,也可以实现)

下面分享下代码:

// 主类
public class Recursion{
    public static void main(String [] args){
        int[][] map = new int[8][8];
        BaHuangHou A = new BaHuangHou();
        A.run(map,0);
        System.out.println("八皇后问题的解决思路个数:" + A.number);
    }
}
// 八皇后问题解决类
class BaHuangHou{
    public int number = 0;
    public void run(int map[][],int row){
        for(int i = 0;i < 8;i++){
            map[row][i] = 1;
            boolean Ju = judge(map,row,i);
            if(Ju){
                if(row == 7){
                    number++;
                    System.out.println("解法"+number+":");
                    print(map);
                }else{
                    run(map,row + 1);
                }
            }
            map[row][i] = 0;
        }
    }
    public boolean judge(int[][] map,int row,int lie){
        for(int i = 0;i < 8;i++){
            boolean condition = (map[i][lie] != 1||i == row )&& 
                (row - i < 0 || lie - i<0 || map[row-i][lie-i] != 1|| i==0) 
                && (row-i<0 || lie + i>=8 || map[row-i][lie+i] != 1|| i==0);
            if(!condition){
                return false;
            }
        }
        return true;
    }
    public void print(int[][] map){
        for(int k = 0;k < map.length;k++){
            for(int j = 0;j < map[0].length;j++){
                System.out.print(map[k][j]);
            }
            System.out.println();
        }
    }
}

结果:

由于目前还是新手,所以有什么写的不合理或者各位大神有更加简洁的方法,可以和我分享一下,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java八皇后问题是一个经典的回溯算法问题,目标是在一个8x8的棋盘上放置8个皇后,使得它们互相之间不能攻击到对方。在这个问题中,皇后可以攻击同一行、同一列以及同一对角线上的其他皇后。 递归解决八皇后问题的常用方法之一。下面是一个简单的Java代码示例: ```java public class EightQueens { private static final int SIZE = 8; private static int[] queens = new int[SIZE]; public static void main(String[] args) { solve(0); } private static void solve(int row) { if (row == SIZE) { printQueens(); return; } for (int col = 0; col < SIZE; col++) { if (isValid(row, col)) { queens[row] = col; solve(row + 1); } } } private static boolean isValid(int row, int col) { for (int i = 0; i < row; i++) { if (queens[i] == col || queens[i] - col == i - row || queens[i] - col == row - i) { return false; } } return true; } private static void printQueens() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (queens[i] == j) { System.out.print("Q "); } else { System.out.print(". "); } } System.out.println(); } System.out.println(); } } ``` 在这段代码中,我们使用一个一维数组`queens`来表示每一行皇后所在的列数。`solve`方法是递归的核心部分,它尝试在当前行的每一列放置皇后,并递归地调用下一行。`isValid`方法用于判断当前位置是否可以放置皇后,它检查同一列、同一对角线上是否已经存在皇后。当放置完最后一行的皇后时,我们就找到了一个解,通过`printQueens`方法打印出棋盘。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值