|
| 1 | +package problems.leetcode; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +// https://leetcode.com/problems/n-queens/ |
| 9 | +public class NQueens { |
| 10 | + |
| 11 | + public static List<List<String>> solveNQueens(int n) { |
| 12 | + if (n == 0) { |
| 13 | + return Collections.emptyList(); |
| 14 | + } |
| 15 | + |
| 16 | + List<List<String>> result = new ArrayList<>(); |
| 17 | + for (int col = 0; col < n; col++) { |
| 18 | + // System.out.println("col=" + col); |
| 19 | + Occupied current = new Occupied(n); |
| 20 | + Pos pos = new Pos(0, col); |
| 21 | + current.occupy(pos); |
| 22 | + solve(n, result, current, pos); |
| 23 | + } |
| 24 | + |
| 25 | + return result; |
| 26 | + |
| 27 | + // n = 5 |
| 28 | + |
| 29 | + // x . . . . |
| 30 | + // . . . x . |
| 31 | + // . x . . . |
| 32 | + // . . . . x |
| 33 | + // . . x . . |
| 34 | + |
| 35 | + // x . . . . |
| 36 | + // . . x . . |
| 37 | + // . . . . x |
| 38 | + // . x . . . |
| 39 | + // . . . x . |
| 40 | + |
| 41 | + // . x . . . |
| 42 | + // . . . x . |
| 43 | + // x . . . . |
| 44 | + // . . x . . |
| 45 | + // . . . . x |
| 46 | + |
| 47 | + // . x . . . |
| 48 | + // . . . . x |
| 49 | + // . . x . . |
| 50 | + // x . . . . |
| 51 | + // . . . x . |
| 52 | + |
| 53 | + // . . x . . |
| 54 | + // . . . . x |
| 55 | + // x . . . . |
| 56 | + // . . . x . |
| 57 | + // . x . . . |
| 58 | + |
| 59 | + // . . x . . |
| 60 | + // x . . . . |
| 61 | + // . . . . x |
| 62 | + // . x . . . |
| 63 | + // . . . x . |
| 64 | + |
| 65 | + // . . . x . |
| 66 | + // . x . . . |
| 67 | + // . . . . x |
| 68 | + // . . x . . |
| 69 | + // x . . . . |
| 70 | + |
| 71 | + // . . . x . |
| 72 | + // x . . . . |
| 73 | + // . . x . . |
| 74 | + // . . . . x |
| 75 | + // . x . . . |
| 76 | + |
| 77 | + // . . . . x |
| 78 | + // . x . . . |
| 79 | + // . . . x . |
| 80 | + // x . . . . |
| 81 | + // . . x . . |
| 82 | + |
| 83 | + // . . . . x |
| 84 | + // . . x . . |
| 85 | + // x . . . . |
| 86 | + // . . . x . |
| 87 | + // . x . . . |
| 88 | + |
| 89 | + // solve it for cols = 0 to ceil(n/2) |
| 90 | + |
| 91 | + // d1 = x - y |
| 92 | + // |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + private static void solve(int boardSize, List<List<String>> result, Occupied current, Pos prev) { |
| 97 | + if (current.size() == boardSize) { |
| 98 | + result.add(current.toBoardString()); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + // System.out.println("current=" + current + ", prev=" + prev); |
| 103 | + // System.out.println("nexts-" + nextPositions); |
| 104 | + for (Pos next : prev.nextPositions(boardSize, current)) { |
| 105 | + current.occupy(next); |
| 106 | + solve(boardSize, result, current, next); |
| 107 | + current.deleteLast();; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static class Pos { |
| 112 | + int x; |
| 113 | + int y; |
| 114 | + Pos(int x, int y) { |
| 115 | + this.x = x; |
| 116 | + this.y = y; |
| 117 | + } |
| 118 | + |
| 119 | + List<Pos> nextPositions(int boardSize, Occupied occupied) { |
| 120 | + List<Pos> result = new ArrayList<>(); |
| 121 | + for (int j = y + 2; j < boardSize; j++) { |
| 122 | + if (x + 1 < boardSize && !occupied.canAttack(x + 1, j)) { |
| 123 | + result.add(new Pos(x + 1, j)); |
| 124 | + } |
| 125 | + if (x > 0 && !occupied.canAttack(x - 1, j)) { |
| 126 | + result.add(new Pos(x - 1, j)); |
| 127 | + } |
| 128 | + } |
| 129 | + for (int j = y - 2; j >= 0; j--) { |
| 130 | + if (x + 1 < boardSize && !occupied.canAttack(x + 1, j)) { |
| 131 | + result.add(new Pos(x + 1, j)); |
| 132 | + } |
| 133 | + if (x > 0 && !occupied.canAttack(x - 1, j)) { |
| 134 | + result.add(new Pos(x - 1, j)); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public String toString() { |
| 143 | + return "{x=" + x + ", y=" + y + "}"; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + private static class Occupied { |
| 150 | + int n; |
| 151 | + boolean rows[]; |
| 152 | + boolean cols[]; |
| 153 | + boolean diag1[]; |
| 154 | + boolean diag2[]; |
| 155 | + List<Pos> positions = new ArrayList<>(); |
| 156 | + |
| 157 | + Occupied(int n) { |
| 158 | + this.n = n; |
| 159 | + this.rows = new boolean[n]; |
| 160 | + this.cols = new boolean[n]; |
| 161 | + this.diag1 = new boolean[n * 2 - 1]; |
| 162 | + this.diag2 = new boolean[n * 2 - 1]; |
| 163 | + } |
| 164 | + |
| 165 | + boolean canAttack(int x, int y) { |
| 166 | + return rows[x] || cols[y] || diag1[n - 1 - (x - y)] || diag2[x + y]; |
| 167 | + } |
| 168 | + |
| 169 | + void occupy(Pos pos) { |
| 170 | + rows[pos.x] = true; |
| 171 | + cols[pos.y] = true; |
| 172 | + diag1[n - 1 - (pos.x - pos.y)] = true; |
| 173 | + diag2[pos.x + pos.y] = true; |
| 174 | + positions.add(pos); |
| 175 | + } |
| 176 | + |
| 177 | + void deleteLast() { |
| 178 | + Pos pos = positions.remove(positions.size() - 1); |
| 179 | + rows[pos.x] = false; |
| 180 | + cols[pos.y] = false; |
| 181 | + diag1[n - 1 - (pos.x - pos.y)] = false; |
| 182 | + diag2[pos.x + pos.y] = false; |
| 183 | + } |
| 184 | + |
| 185 | + int size() { |
| 186 | + return positions.size(); |
| 187 | + } |
| 188 | + |
| 189 | + List<String> toBoardString() { |
| 190 | + char[][] board = new char[n][n]; |
| 191 | + for (int i = 0; i < n; i++) { |
| 192 | + for (int j = 0; j < n; j++) { |
| 193 | + board[i][j] = '.'; |
| 194 | + } |
| 195 | + } |
| 196 | + for (Pos pos : positions) { |
| 197 | + board[pos.x][pos.y] = 'Q'; |
| 198 | + } |
| 199 | + |
| 200 | + List<String> rows = new ArrayList<>(); |
| 201 | + for (char[] b : board) { |
| 202 | + rows.add(new String(b)); |
| 203 | + } |
| 204 | + |
| 205 | + return rows; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + |
| 210 | + public static void main(String[] args) { |
| 211 | + // System.out.println(new Pos(0, 0).canAttack(new Pos(0, 0))); |
| 212 | + // System.out.println(new Pos(0, 0).canAttack(new Pos(2, 0))); |
| 213 | + // System.out.println(new Pos(0, 0).canAttack(new Pos(0, 3))); |
| 214 | + |
| 215 | + // System.out.println(new Pos(0, 0).canAttack(new Pos(2, 2))); |
| 216 | + // System.out.println(new Pos(1, 2).canAttack(new Pos(2, 3))); |
| 217 | + |
| 218 | + // System.out.println(new Pos(2, 0).canAttack(new Pos(0, 2))); |
| 219 | + |
| 220 | + |
| 221 | + for (List<String> l : solveNQueens(5)) { |
| 222 | + System.out.println("#############"); |
| 223 | + for (String s : l) { |
| 224 | + System.out.println(s); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + // [["Q....","..Q..","....Q",".Q...","...Q."], |
| 229 | + // ["Q....","...Q.",".Q...","....Q","..Q.."], |
| 230 | + // [".Q...","...Q.","Q....","..Q..","....Q"], |
| 231 | + // [".Q...","....Q","..Q..","Q....","...Q."], |
| 232 | + // ["..Q..","Q....","...Q.",".Q...","....Q"], |
| 233 | + // ["..Q..","....Q",".Q...","...Q.","Q...."], |
| 234 | + // ["...Q.","Q....","..Q..","....Q",".Q..."], |
| 235 | + // ["...Q.",".Q...","....Q","..Q..","Q...."], |
| 236 | + // ["....Q",".Q...","...Q.","Q....","..Q.."], |
| 237 | + // ["....Q","..Q..","Q....","...Q.",".Q..."]] |
| 238 | + |
| 239 | + |
| 240 | + // n-1-(x-y) |
| 241 | + // 8 0-4 |
| 242 | + // 7 0-3, 1-4 |
| 243 | + // 6 0-2, 1-3, 2-4 |
| 244 | + // 5 0-1, 1-2, 2-3, 3-4 |
| 245 | + // 4 0-0, 1-1, 2-2, 3-3, 4-4 |
| 246 | + // 3 1-0, 2-1, 3-2, 4-3 |
| 247 | + // 2 2-0, 3-1, 4-2 |
| 248 | + // 1 3-0, 4-1 |
| 249 | + // 0 4-0 |
| 250 | + |
| 251 | + // [".Q...", |
| 252 | + // "...Q.", |
| 253 | + // "Q....", |
| 254 | + // "..Q..", |
| 255 | + // "....Q"] |
| 256 | + } |
| 257 | +} |
0 commit comments