|
| 1 | +// class Solution { |
| 2 | +// public List<List<String>> solveNQueens(int n) { |
| 3 | +// List<List<String>> results = new ArrayList<>(); |
| 4 | +// boolean[][] board = new boolean[n][n]; |
| 5 | +// backtrack(board, 0 ,results); |
| 6 | +// return results; |
| 7 | +// } |
| 8 | +// private void backtrack(boolean[][] board, int row, List<List<String>> results){ |
| 9 | +// if(row == board.length){ |
| 10 | +// results.add(createBoard(board)); |
| 11 | +// return; |
| 12 | +// } |
| 13 | +// for(int col=0; col<board.length; col++){ |
| 14 | +// if(isSafe(board,row,col)){ |
| 15 | +// board[row][col] = true; |
| 16 | +// backtrack(board, row+1, results); |
| 17 | +// board[row][col] = false; |
| 18 | +// } |
| 19 | +// } |
| 20 | +// } |
| 21 | + |
| 22 | +// private boolean isSafe(boolean[][] board,int row,int col){ |
| 23 | +// for(int i=0; i < row; i++){ |
| 24 | +// if(board[i][col]) return false; |
| 25 | +// } |
| 26 | + |
| 27 | +// for(int i=row-1, j=col-1; i>=0 && j>=0; i--, j--){ |
| 28 | +// if(board[i][j]) return false; |
| 29 | +// } |
| 30 | + |
| 31 | +// for(int i=row-1, j=col+1; i>=0 && j < board.length; i--, j++ ){ |
| 32 | +// if(board[i][j]) return false; |
| 33 | +// } |
| 34 | + |
| 35 | +// return true; |
| 36 | +// } |
| 37 | + |
| 38 | +// private List<String> createBoard(boolean[][] board){ |
| 39 | +// List<String> b = new ArrayList<>(); |
| 40 | +// for(int i=0; i<board.length; i++){ |
| 41 | +// StringBuilder row = new StringBuilder(); |
| 42 | +// for(int j=0; j<board.length; j++){ |
| 43 | +// row.append(board[i][j] ? 'Q' : '.'); |
| 44 | +// } |
| 45 | +// b.add(row.toString()); |
| 46 | +// } |
| 47 | +// return b; |
| 48 | +// } |
| 49 | +// } |
| 50 | + |
| 51 | +class Solution { |
| 52 | + |
| 53 | + private int n; |
| 54 | + private int fullMask; |
| 55 | + private int[] queens; // queens[row] = col position |
| 56 | + private String[] templates; // templates[c] = board row with 'Q' at c |
| 57 | + private List<List<String>> results; |
| 58 | + |
| 59 | + public List<List<String>> solveNQueens(int n) { |
| 60 | + this.n = n; |
| 61 | + this.fullMask = (1 << n) - 1; |
| 62 | + this.queens = new int[n]; |
| 63 | + this.results = new ArrayList<>(); |
| 64 | + buildTemplates(); |
| 65 | + backtrack(0, 0, 0, 0); |
| 66 | + return results; |
| 67 | + } |
| 68 | + |
| 69 | + // Pre-build n Strings like "...Q.." for each column position |
| 70 | + private void buildTemplates() { |
| 71 | + templates = new String[n]; |
| 72 | + char[] row = new char[n]; |
| 73 | + for (int c = 0; c < n; c++) { |
| 74 | + Arrays.fill(row, '.'); |
| 75 | + row[c] = 'Q'; |
| 76 | + templates[c] = new String(row); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param row which row we're filling |
| 82 | + * @param colMask occupied columns bitmap |
| 83 | + * @param d1Mask occupied (row+col) diagonals bitmap |
| 84 | + * @param d2Mask occupied (row-col+n-1) diagonals bitmap |
| 85 | + */ |
| 86 | + private void backtrack(int row, int colMask, int d1Mask, int d2Mask) { |
| 87 | + if (row == n) { |
| 88 | + // build solution by referencing pre-made row‑strings |
| 89 | + List<String> sol = new ArrayList<>(n); |
| 90 | + for (int r = 0; r < n; r++) { |
| 91 | + sol.add(templates[queens[r]]); |
| 92 | + } |
| 93 | + results.add(sol); |
| 94 | + return; |
| 95 | + } |
| 96 | + // bits free in this row |
| 97 | + int avail = fullMask & ~(colMask | d1Mask | d2Mask); |
| 98 | + while (avail != 0) { |
| 99 | + int bit = avail & -avail; // rightmost free column |
| 100 | + avail ^= bit; // remove it |
| 101 | + int col = Integer.numberOfTrailingZeros(bit); |
| 102 | + queens[row] = col; |
| 103 | + |
| 104 | + backtrack(row + 1, colMask | bit, (d1Mask | bit) << 1, (d2Mask | bit) >>> 1); |
| 105 | + // no need to unset queens[row], it'll be overwritten |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments