gpt4 book ai didi

java - 八皇后启发式

转载 作者:搜寻专家 更新时间:2023-11-01 02:13:10 25 4
gpt4 key购买 nike

我正在开发一种启发式方法,将 8 个皇后放在 8x8 的棋盘上。每个方格都有自己的淘汰号(表示如果在该方格中放置一个皇后,则“消除”空棋盘的多少个方格。)并且每个皇后应放置在具有最低淘汰号的方格中。

我的问题是我不知道该怎么做才能不断减少相应方 block 的特定消除数,所以如果你能帮助我,我将不胜感激。另一个问题,我觉得我的代码很复杂,所以有什么注释可以使它更简单吗?

这是我的代码

public class Queen {
private final int SIZE = 8;
private int board[][] = new int[SIZE][SIZE]; // 8x8 board
private int hor[] = new int[SIZE]; // horizontal moves
private int ver[] = new int[SIZE]; // vertical moves
private int lowestValue = 22;
private int[][] queens = new int[SIZE][2]; // 8 queens

public Queen () {
// initialize each square with its own elimination number
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (row == 0 || row == 7 || ((row >= 1 && row <= 6) && (col == 0 || col == 7))) {
board[row][col] = 22;
}
else if ((row == 1 || row == 6) && (col >= 1 && col <= 6) || (row > 1 && row < 6) && (col == 1 || col == 6)) {
board[row][col] = 24;
}
else if ((row == 2 || row == 5) && (col >= 2 && col <= 5)|| (row > 2 && row < 5) && (col == 2 || col == 5)){
board[row][col] = 26;
}
else if ((row == 3 || row == 4) && (col >= 3 && col <= 4)){
board[row][col] = 28;
}

}
}// end initializing

// initialize moves
//right
hor[0] = 1;
ver[0] = 0;
//left
hor[1] = -1;
ver[1] = 0;
//up
hor[2] = 0;
ver[2] = -1;
//down
hor[3] = 0;
ver[3] = 1;
//up right
hor[4] = 1;
ver[4] = -1;
hor[7] = -1;
ver[7] = 1;
//up left
hor[5] = -1;
ver[5] = -1;
//down right
hor[6] = 1;
ver[6] = 1;
// down left

for (int queen = 0; queen < queens.length; queen++) {
placeQueens(queen);
}
displayBoard();
}// end constructor

// eliminate squares according to the place of the queen
private void eliminate (int queen_row, int queen_col) {

// eliminate diagonal
int rowCopy = queen_row;// helper row
int colCopy = queen_col;// helper column
try {
// eliminate up right
for (int move = 0; move < 8; move++) {
rowCopy += ver[4];
colCopy += hor[4];
if (board[rowCopy][colCopy] > 0) {
board[rowCopy][colCopy] = 0;
}
}
}
catch (ArrayIndexOutOfBoundsException e) {
}

try {
rowCopy = queen_row;
colCopy = queen_col;
// eliminate up left
for (int move = 0; move < 8; move++) {
rowCopy += ver[5];
colCopy += hor[5];
if (board[rowCopy][colCopy] > 0) {
board[rowCopy][colCopy] = 0;
}
}
}
catch (ArrayIndexOutOfBoundsException e) {
}

try {
rowCopy = queen_row;
colCopy = queen_col;
// eliminate down right
for (int move = 0; move < 8; move++) {
rowCopy += ver[6];
colCopy += hor[6];
if (board[rowCopy][colCopy] > 0) {
board[rowCopy][colCopy] = 0;
}
}
}
catch (ArrayIndexOutOfBoundsException e) {
}
try {
rowCopy = queen_row;
colCopy = queen_col;
// eliminate down left
for (int move = 0; move < 8; move++) {
rowCopy += ver[7];
colCopy += hor[7];
if (board[rowCopy][colCopy] > 0) {
board[rowCopy][colCopy] = 0;
}
}
}
catch (ArrayIndexOutOfBoundsException e) {
}

// eliminate row
for (int col = 0; col < 8;col++) {
if (board[queen_row][col] > 0) {
board[queen_row][col] = 0;
}
}
// eliminate col
for (int row = 0; row < 8; row++) {
if (board[row][queen_col] > 0) {
board[row][queen_col] = 0;
}
}

}// end elimination

// decrease elimination number of each square
public void decreaseElimination () {


}// end decrease elimination

public void countEliminated () {
int counter = 0;
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == 0) {
counter++;
}

}
}
System.out.printf("%d squares eliminated\n", counter);

}

public void placeQueens(int queenNum) {
int targetRow;
int targetCol;
// find lowest elimination number
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] > 0 && board[row][col] < lowestValue) {
lowestValue = board[row][col];
targetRow = row;
targetCol = col;

queens[queenNum][0] = targetRow;
queens[queenNum][1] = targetCol;
}
}
}

System.out.printf("queen %d has been placed in [%d][%d]\n", queenNum + 1, queens[queenNum][0], queens[queenNum][1]);
eliminate(queens[queenNum][0], queens[queenNum][1]);
decreaseElimination();
countEliminated();

}

// display board
public void displayBoard () {

for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
System.out.printf("|%2d|", board[row][col]); // display elimination number of each square
}
System.out.println();
}

}// end displayBoard

我的主要方法在单独的类中。

最佳答案

这段代码本身就有缺陷:

for (int queen = 0; queen < queens.length; queen++) {
placeQueens(queen);
}

如果不同时决定将皇后 1 到 8 放在哪里,就无法决定将皇后 0 放在哪里。您已经实现了“首次适配”:

First Fit

如您在上面的示例中所见,首次拟合不会产生可行的解决方案。 More info in this manual (including algorithms that do work).

这是一个有效的算法(但扩展性非常差): Backtracking

关于java - 八皇后启发式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13217979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com