|
| 1 | +// https://leetcode.com/problems/n-queens-ii |
| 2 | +// T: O(N!) |
| 3 | +// S: O(N^2) |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class NQueensII { |
| 9 | + private static boolean[] rows, columns; |
| 10 | + private static int result = 0; |
| 11 | + |
| 12 | + public int totalNQueens(int n) { |
| 13 | + result = 0; |
| 14 | + final List<String> board = getEmptyBoard(n); |
| 15 | + rows = new boolean[n]; |
| 16 | + columns = new boolean[n]; |
| 17 | + nQueens(0, n, board, 0); |
| 18 | + return result; |
| 19 | + } |
| 20 | + |
| 21 | + private static void nQueens(int row, int n, List<String> board, int queens) { |
| 22 | + if (row == n) { |
| 23 | + if (queens == n) { |
| 24 | + result++; |
| 25 | + } |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + for (int column = 0 ; column < n ; column++) { |
| 30 | + if (canPlace(board, row, column)) { |
| 31 | + placeQueen(board, row, column); |
| 32 | + nQueens(row + 1, n, board, queens + 1); |
| 33 | + removeQueen(board, row, column); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static void placeQueen(List<String> board, int row, int column) { |
| 39 | + board.set( |
| 40 | + row, |
| 41 | + board.get(row).substring(0, column) + 'Q' + board.get(row).substring(column + 1) |
| 42 | + ); |
| 43 | + rows[row] = true; |
| 44 | + columns[column] = true; |
| 45 | + } |
| 46 | + |
| 47 | + private static void removeQueen(List<String> board, int row, int column) { |
| 48 | + board.set( |
| 49 | + row, |
| 50 | + board.get(row).substring(0, column) + '.' + board.get(row).substring(column + 1) |
| 51 | + ); |
| 52 | + rows[row] = false; |
| 53 | + columns[column] = false; |
| 54 | + } |
| 55 | + |
| 56 | + private static boolean canPlace(List<String> board, int row, int column) { |
| 57 | + return !rows[row] && !columns[column] && !queenInLeftDiagonal(board, row, column) |
| 58 | + && !queenInRightDiagonal(board, row, column); |
| 59 | + } |
| 60 | + |
| 61 | + private static boolean queenInLeftDiagonal(List<String> board, int row, int column) { |
| 62 | + for (int i = row - 1, j = column - 1 ; i >= 0 && j >= 0 ; i--, j--) { |
| 63 | + if (board.get(i).charAt(j) == 'Q') { |
| 64 | + return true; |
| 65 | + } |
| 66 | + } |
| 67 | + for (int i = row + 1, j = column + 1 ; i < board.size() && j < board.size() ; i++, j++) { |
| 68 | + if (board.get(i).charAt(j) == 'Q') { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + private static boolean queenInRightDiagonal(List<String> board, int row, int column) { |
| 76 | + for (int i = row - 1, j = column + 1 ; i >= 0 && j < board.size() ; i--, j++) { |
| 77 | + if (board.get(i).charAt(j) == 'Q') { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + for (int i = row + 1, j = column - 1 ; i < board.size() && j >= 0 ; i++, j--) { |
| 82 | + if (board.get(i).charAt(j) == 'Q') { |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + private static List<String> getEmptyBoard(int n) { |
| 90 | + final List<String> board = new ArrayList<>(); |
| 91 | + final String line = ".".repeat(n); |
| 92 | + for (int i = 0 ; i < n ; i++) { |
| 93 | + board.add(line); |
| 94 | + } |
| 95 | + return board; |
| 96 | + } |
| 97 | +} |
0 commit comments