5

以前、Java を使用して 8 クイーン問題を解決する方法について質問したことがあります。問題を解決するためのバックトラッキング アルゴリズムを取得しました。

このアルゴリズムを使用しようとしましたが、コードの何が問題なのかわかりません。クイーンは 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);
    }
}
4

3 に答える 3

5

In Java arrays are 0-indexed , つまり、最初の項目のインデックスは 0 です。この重要な事実を完全には把握していないようで、多くのoff-by-one エラーを含むコードを記述しています。

ここに 1 つの問題があります。

int row = 1;

そのはず:

int row = 0;

2番目の問題は次のとおりです。

if (column > BOARD_SIZE) {
    return true;
}

これは次のようになります。

if (column >= BOARD_SIZE) {
    return true;
}

コードの残りの部分を投稿していませんが、メソッドを呼び出すときにコードに 3 番目のエラーがあることは間違いありません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個のクイーンが配置されました

オンラインでの動作を確認してください: ideone

于 2012-11-15T13:59:19.747 に答える
-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 1 0 0 0
0 1 0 0 0 0 0 0 0 0
0 1 0 0
0 1 0 0 0 0 0 0 0
0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 1
__________________________
女王: 11
1 0 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 0 1 0 0 0
0 0 1 0 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 0 0 1 0 0 0
0 1 0 0 0 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


于 2018-07-15T04:11:02.870 に答える