|
| 1 | +class Solution { |
| 2 | + public int totalNQueens(int n) { |
| 3 | + |
| 4 | + // TC - N^N |
| 5 | + List<List<String>> allBoards = new ArrayList<>(); // list of all possible solution i.e output |
| 6 | + // to avoid complexity instead of creating list of string |
| 7 | + // we will make a 2D character array for ease |
| 8 | + char[][] board = new char[n][n]; |
| 9 | + helper(allBoards, board, 0); // function to check a single board |
| 10 | + return allBoards.size(); // output result |
| 11 | + } |
| 12 | + |
| 13 | + public void helper(List<List<String>> allBoards, char[][] board, int col){ |
| 14 | + // base case |
| 15 | + // if the current board is valid save it and try next configuration |
| 16 | + if(col == board.length){ |
| 17 | + saveBoard(allBoards, board); // to save the current board in the allBoards list to send output of all the possible solutions to place queens |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + // to go to each cell and put or remove queen according to the validity |
| 22 | + for(int row = 0; row < board.length; row++){ |
| 23 | + if(isSafe(row, col, board)){ // isSafe helper function to check validity at current point |
| 24 | + board[row][col] = 'Q'; |
| 25 | + helper(allBoards, board, col + 1); |
| 26 | + // if the current place of queen for to be wrong after checking further cells |
| 27 | + // remove queen from that position |
| 28 | + board[row][col] = '.'; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public boolean isSafe(int row, int col, char[][] board){ |
| 34 | + // check all directions |
| 35 | + |
| 36 | + // FOR ROW |
| 37 | + for(int x = 0; x < board.length; x++){ |
| 38 | + if(board[row][x] == 'Q'){ |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + // FOR COLUMN |
| 46 | + for(int y = 0; y < board.length; y++){ |
| 47 | + if(board[y][col] == 'Q'){ |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + // FOR DIAGNOLS |
| 54 | + |
| 55 | + // upper left (r-1, c-1) |
| 56 | + int r = row; // original position |
| 57 | + for(int c = col; c >= 0 && r >= 0; c--, r--){ |
| 58 | + if(board[r][c] == 'Q'){ |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // upper right (r-1, c+1) |
| 64 | + r = row; |
| 65 | + for(int c = col; c < board.length && r >=0; c++, r--){ |
| 66 | + if(board[r][c] == 'Q'){ |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // lower left (r+1, c-1) |
| 72 | + r = row; |
| 73 | + for(int c = col; c >= 0 && r < board.length; r++, c--){ |
| 74 | + if(board[r][c] == 'Q'){ |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // lower right (r+1, c+1) |
| 80 | + r = row; |
| 81 | + for(int c = col; c < board.length && r < board.length; r++, c++){ |
| 82 | + if(board[r][c] == 'Q'){ |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + public void saveBoard(List<List<String>> allBoards, char[][] board){ |
| 91 | + String row = ""; |
| 92 | + List<String> FinalBoard = new ArrayList<>(); |
| 93 | + |
| 94 | + for(int i = 0; i < board.length; i++){ |
| 95 | + row = ""; // empty string |
| 96 | + for(int j = 0; j < board[0].length; j++){ |
| 97 | + if(board[i][j] == 'Q'){ |
| 98 | + row += 'Q'; |
| 99 | + } else{ |
| 100 | + row += '.'; |
| 101 | + } |
| 102 | + } |
| 103 | + FinalBoard.add(row); |
| 104 | + } |
| 105 | + |
| 106 | + allBoards.add(FinalBoard); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
0 commit comments