修改 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);
}
}