java计算八皇后_java – 八皇后算法

我之前问过一个关于使用

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
8皇后问题JAVA算法 用递归实现,程序种有两种判定皇后可放的方法 一种采用辅助数组,一种采用斜率判断 代码比较简洁,对递归的理解和掌握有帮助 测试结果: 1 :1 5 8 6 3 7 2 4 2 :1 6 8 3 7 4 2 5 3 :1 7 4 6 8 2 5 3 4 :1 7 5 8 2 4 6 3 5 :2 4 6 8 3 1 7 5 6 :2 5 7 1 3 8 6 4 7 :2 5 7 4 1 8 6 3 8 :2 6 1 7 4 8 3 5 9 :2 6 8 3 1 4 7 5 10 :2 7 3 6 8 5 1 4 11 :2 7 5 8 1 4 6 3 12 :2 8 6 1 3 5 7 4 13 :3 1 7 5 8 2 4 6 14 :3 5 2 8 1 7 4 6 15 :3 5 2 8 6 4 7 1 16 :3 5 7 1 4 2 8 6 17 :3 5 8 4 1 7 2 6 18 :3 6 2 5 8 1 7 4 19 :3 6 2 7 1 4 8 5 20 :3 6 2 7 5 1 8 4 21 :3 6 4 1 8 5 7 2 22 :3 6 4 2 8 5 7 1 23 :3 6 8 1 4 7 5 2 24 :3 6 8 1 5 7 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5 2 8 79 :6 4 7 1 8 2 5 3 80 :6 8 2 4 1 7 5 3 81 :7 1 3 8 6 4 2 5 82 :7 2 4 1 8 5 3 6 83 :7 2 6 3 1 4 8 5 84 :7 3 1 6 8 5 2 4 85 :7 3 8 2 5 1 6 4 86 :7 4 2 5 8 1 3 6 87 :7 4 2 8 6 1 3 5 88 :7 5 3 1 6 8 2 4 89 :8 2 4 1 7 5 3 6 90 :8 2 5 3 1 7 4 6 91 :8 3 1 6 2 5 7 4 92 :8 4 1 3 6 2 7 5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值