java:implement 8 queen using depth first search

后端 未结 3 1831
你的背包
你的背包 2020-12-12 06:09

i am try to implement 8 queen using depth search for any initial state it work fine for empty board(no queen on the board) ,but i need it to work for initial state if there

3条回答
  •  Happy的楠姐
    2020-12-12 06:44

    Try to alter your method IsValid in the lines where for (x = 0; x < board.length - 1; x++).

    public static boolean IsValid(int[][] board, int i, int j) {
        int x;
        //check the queens in column
        for (x = 0; x < board.length - 1; x++) {
            if (board[i][x] != 0) {
                return false;
            }
        }
        //check the queens in row
        for (x = 0; x < board.length - 1; x++) {
            if (board[x][j] != 0) {
                return false;
            }
        }
        //check the queens in the diagonals
        if (!SafeDiag(board, i, j)) {
            return false;
        }
        return true;
    }
    

提交回复
热议问题