|
| 1 | +//Question - The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. |
| 2 | +//Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. |
| 3 | +//Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively. |
| 4 | + |
| 5 | +class Solution { |
| 6 | + public boolean isSafe(int row, int col, char[][] board) { |
| 7 | + // horizontal |
| 8 | + for (int j = 0; j < board.length; j++) { |
| 9 | + if (board[row][j] == 'Q') { |
| 10 | + return false; |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + // vertical |
| 15 | + for (int i = 0; i < board.length; i++) { |
| 16 | + if (board[i][col] == 'Q') { |
| 17 | + return false; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + // upper left |
| 22 | + int r = row; |
| 23 | + for (int c = col; c >= 0 && r >= 0; c--, r--) { |
| 24 | + if (board[r][c] == 'Q') { |
| 25 | + return false; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // upper right |
| 30 | + r = row; |
| 31 | + for (int c = col; c < board.length && r >= 0; r--, c++) { |
| 32 | + if (board[r][c] == 'Q') { |
| 33 | + return false; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // lower left |
| 38 | + r = row; |
| 39 | + for (int c = col; c >= 0 && r < board.length; r++, c--) { |
| 40 | + if (board[r][c] == 'Q') { |
| 41 | + return false; |
| 42 | + } |
| 43 | + } |
| 44 | + // lower right |
| 45 | + for (int c = col; c < board.length && r < board.length; c++, r++) { |
| 46 | + if (board[r][c] == 'Q') { |
| 47 | + return false; |
| 48 | + } |
| 49 | + } |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + public void saveBoard(char[][] board, List<List<String>> allBoards) { |
| 54 | + String row = ""; |
| 55 | + List<String> newBoard = new ArrayList<>(); |
| 56 | + for (int i = 0; i < board.length; i++) { // loop run on BOARD |
| 57 | + row = ""; // value of row is equal to EMPTY String; |
| 58 | + for (int j = 0; j < board[0].length; j++) { |
| 59 | + if (board[i][j] == 'Q') |
| 60 | + row += 'Q'; |
| 61 | + else |
| 62 | + row += '.'; |
| 63 | + } |
| 64 | + newBoard.add(row); |
| 65 | + } |
| 66 | + allBoards.add(newBoard); |
| 67 | + } |
| 68 | + |
| 69 | + public void helper(char[][] board, List<List<String>> allBoards, int col) { |
| 70 | + if (col == board.length) { |
| 71 | + saveBoard(board, allBoards); |
| 72 | + return; |
| 73 | + } |
| 74 | + for (int row = 0; row < board.length; row++) { |
| 75 | + if (isSafe(row, col, board)) { |
| 76 | + board[row][col] = 'Q'; |
| 77 | + helper(board, allBoards, col + 1); |
| 78 | + board[row][col] = '.'; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public List<List<String>> solveNQueens(int n) { |
| 84 | + List<List<String>> allBoards = new ArrayList<>(); |
| 85 | + char[][] board = new char[n][n]; |
| 86 | + |
| 87 | + helper(board, allBoards, 0); |
| 88 | + return allBoards; |
| 89 | + } |
| 90 | +} |
0 commit comments