我之前问过一个关于使用Java解决八个皇后问题的问题。 我有一个回溯算法来解决这个问题。
我尝试使用此算法,但我不知道我的代码有什么问题。它最多只能放置7个皇后。
这是女王班:
public class Queen {
//Number of rows or columns
public static final int BOARD_SIZE = 8;
boolean[][] board;
//Indicate an empty square
public static final boolean EMPTY = false;
//Indicate a square which containing a queen
public static final boolean QUEEN = true;
//Number of moves
public static final int MOVES = 4;
//Horizontal moves
int[] horizontal;
//Vertical moves
int[] vertical;
public int queens = 0;
public Queen() {
//Constructor creates an empty board
board = new boolean[BOARD_SIZE][BOARD_SIZE];
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = EMPTY;
}
}
horizontal = new int[MOVES];
vertical = new int[MOVES];
// up right
horizontal[0] = -1;
vertical[0] = 1;
// down left
horizontal[1] = 1;
vertical[1] = -1;
// up left
horizontal[2] = -1;
vertical[2] = -1;
// down right
horizontal[3] = 1;
vertical[3] = 1;
}
public boolean placeQueens (int column) {
if (column > BOARD_SIZE) {
return true;
}
else {
boolean queenPlaced = false;
int row = 1;
while (!queenPlaced && row < BOARD_SIZE) {
if (isUnderAttack(row, column)) {
++row;
}// end if
else{
setQueen(row, column);
queenPlaced = placeQueens(column + 1);
if (!queenPlaced) {
removeQueen(row,column);
++row;
}// end if
}// end else
}// end while
return queenPlaced;
}// end else
}
private void removeQueen(int row, int column) {
board[row][column] = EMPTY;
System.out.printf("queen REMOVED from [%d][%d]\n", row, column);
--queens;
}
private void setQueen(int row, int column) {
board[row][column] = QUEEN;
System.out.printf("queen PLACED in [%d][%d]\n", row, column);
++queens;
}
public boolean isUnderAttack(int row, int col) {
boolean condition = false;
// check row
for (int column = 0; column < BOARD_SIZE; column++) {
if ((board[row][column] == true)) {
condition = true;
}
}
// check column
for (int row_ = 0; row_ < board.length; row_++) {
if (board[row_][col] == true) {
condition = true;
}
}
// check diagonal
for (int row_ = row, col_ = col; row_ >= 0 && col_ < 8; row_ += horizontal[0], col_ += vertical[0]) {
if (board[row_][col_] == true) {
condition = true;
}
}
for (int row_ = row, col_ = col; row_ < 8 && col_ >= 0; row_ += horizontal[1], col_ += vertical[1]) {
if (board[row_][col_] == true) {
condition = true;
}
}
for (int row_ = row, col_ = col; row_ >= 0 && col_ >= 0; row_ += horizontal[2], col_ += vertical[2]) {
if (board[row_][col_] == true) {
condition = true;
}
}
for (int row_ = row, col_ = col; row_ < 8 && col_ < 8; row_ += horizontal[3], col_ += vertical[3]) {
if (board[row_][col_] == true) {
condition = true;
}
}
return condition;
}
public void displayBoard () {
int counter = 0;
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == true) {
System.out.printf("|%s|", "x");
counter++;
}
else {
System.out.printf("|%s|", "o");
}
}
System.out.println();
}
System.out.printf("%d queens has been placed\n", counter);
}
}
答案 0 :(得分:5)
在Java数组中0-indexed,意味着第一个项目在索引0处。您似乎还没有完全掌握这个关键事实,这导致您编写了许多off-by-one errors的代码。 / p>
这里有一个问题:
int row = 1;
应该是:
int row = 0;
第二个问题在这里:
if (column > BOARD_SIZE) {
return true;
}
应该是这样的:
if (column >= BOARD_SIZE) {
return true;
}
您尚未发布其余代码,但我愿意打赌,当您调用placeQueens
方法时,代码中会出现第三个错误。如果我知道你是什么样的人,那么你可能会这样做:
queen.placeQueens(1);
但它应该是这样的:
queen.placeQueens(0);
通过所有这些更改,它可以按预期工作。最终结果是:
|x||o||o||o||o||o||o||o| |o||o||o||o||o||o||x||o| |o||o||o||o||x||o||o||o| |o||o||o||o||o||o||o||x| |o||x||o||o||o||o||o||o| |o||o||o||x||o||o||o||o| |o||o||o||o||o||x||o||o| |o||o||x||o||o||o||o||o| 8 queens has been placed
查看在线工作:ideone
答案 1 :(得分:0)
isUnderAttack方法中有一些硬编码:
//检查对角线 在BOARD_SIZE中使用8的地方
使用:
col_ < BOARD_SIZE
代替
col_ < 8
最好使BOARD_SIZE不是静态的,而应将其作为输入参数,使代码更通用,并测试boardSize = 4或12
答案 2 :(得分:-1)
我写了一个通用代码,适用于任何皇后区。
结果用0或1表示。1是皇后,0-是空正方形。
static int[][] setupEightQueens(int queensNum) {
if (queensNum <= 0)
return new int[][] { {} };
int[][] chessField = new int[queensNum][queensNum];
int n = chessField.length;
int startJ = 0;
for (int i = 0; i < n; i++) {
for (int j = startJ; j < n; j += 2) {
chessField[j][i] = 1;
i++;
}
i--;
startJ++;
}
return chessField;
}
经测试的4、8、11皇后数量的输出:
__________________________
皇后区:4
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
__________________________
皇后区:8
1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 1
__________________________
皇后区:11
1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 1 0 0 0 0 0