8皇后问题(回溯解法加javafx图形化界面,有一点小问题)

 修改 SIZE、queens1与solveNQueens的参数为你想要的个数

就可以是实现 n皇后问题(回溯解法加Javafx图形化界面)

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

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

public class EightQueensMin extends Application {
    public static final int SIZE = 8; // The size of the chess board
    public static List<String> res = new ArrayList<>();

    public static List<Integer> queens = new ArrayList<>();
    public static int GeShu = 0;

    public static int[] queens1 = new int[8];

    static Label[][] labels = new Label[SIZE][SIZE];


    // Override the start method in the Application class
    @Override
    public void start(Stage primaryStage) {

        res = solveNQueens(8, res);


        // Display chess board
        GridPane chessBoard = new GridPane();
        chessBoard.setAlignment(Pos.CENTER);
        for (int i = 0; i < SIZE; i++)
            for (int j = 0; j < SIZE; j++) {
                chessBoard.add(labels[i][j] = new Label(), j, i);
                labels[i][j].setStyle("-fx-border-color: black");
                labels[i][j].setPrefSize(55, 55);
            }

        System.out.println("点击Next下一个答案");

        // 按键事件
        Botten(chessBoard, SIZE);


//         Create a scene and place it in the stage
        Scene scene = new Scene(chessBoard, 55 * SIZE, 55 * SIZE);
        primaryStage.setTitle("EightQueens"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

    public static void Botten(GridPane chessBoard, int size) {
        Button b1 = new Button();
        b1.setText("Next");
        b1.setPrefWidth(size * 7);
        b1.setPrefHeight(size * 2);

        b1.setOnAction(event -> {
            Button button = (Button) event.getSource();
            for (int j = 0; j < 8; j++) {
                for (int i = 0; i < 8; i++) {
                    labels[j][i].setText(" ");
                }
            }
            ZhuangRu();
            GeShu++;
        });
        chessBoard.getChildren().add(b1);
    }

    public static void ZhuangRu() {
        String[] strs3 = res.toArray(new String[res.size()]);// 方式二

        for (; GeShu < strs3.length; ) {
            String str4 = strs3[GeShu];
//            System.out.println(str4);

            for (int j = 0, index = 1; j < 8; j++) {
                int zhong = str4.charAt(index);
                queens1[j] = zhong - 48;
                index += 3;
                labels[j][queens1[j]].setText("@");
            }
            queens1 = new int[8];
            break;
        }
    }

    public static List<String> solveNQueens(int n, List<String> res) {


        // 回溯模版
        res = backtrack(n, 0, res);

        return res;
    }

    // n: n皇后
    // row: 行
    private static List<String> backtrack(int n, int row, List<String> res) {

        if (row == n) {
            /*
            遍历完n行,将缓存到queue中的结果加入到全局结果里
            */
//            res.add(Collections.singletonList(queens.toString()));
            res.add(queens.toString());


            return res;
        }

        for (int column = 0; column < n; column++) {
                // 当前行,从左到右遍历列
            if (!isValid(row, column)) {
                // 判断当前行 当前列 是否满足
                // 不满足就去找下一列
                continue;
            }
            // 满足 将当前行 列 加入到缓存中
            queens.add(row, column);
            // 递归 下一行
            backtrack(n, row + 1, res);
            // 将之前加入到缓存中的数据清空
            queens.remove(row);
        }
        return res;
    }


    private static boolean isValid(int row, int column) {
        // 判断当前 行 列 是否满足
        boolean res = true;
        for (int r = 0; r < row; r++) {
            // 遍历行
            if (queens.get(r) == -1) {
                // 缓存冲当前行没有皇后 说明这行不影响
                continue;
            }
            // 以下是不满足的条件
            if (queens.get(r) == column || queens.get(r) + (row - r) == column || queens.get(r) - (row - r) == column) {
                // 当前行 皇后列 == 将要放入的皇后列
                // 当前行 皇后列 与 将要放入的皇后列 在对角线
                return false;
            }
        }
        return res;
    }

    /**
     * The main method is only needed for the IDE with limited
     * JavaFX support. Not needed for running from the command line.
     */
    public static void main(String[] args) {
        launch(args);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你这个年纪你是怎么睡得着的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值