| 2 | |
| 3 | class Solution { |
| 4 | public boolean isSafe(int[][] board, int row, int col) { |
| 5 | int x = row, y = col; |
| 6 | |
| 7 | // Check left side of the current row |
| 8 | while (y >= 0) { |
| 9 | if (board[x][y--] == 1) |
| 10 | return false; |
| 11 | } |
| 12 | |
| 13 | // Check upper diagonal on left side |
| 14 | y = col; |
| 15 | while (x < board.length && y >= 0) { |
| 16 | if (board[x++][y--] == 1) |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | // Check lower diagonal on left side |
| 21 | x = row; |
| 22 | y = col; |
| 23 | while (x >= 0 && y >= 0) { |
| 24 | if (board[x--][y--] == 1) |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | public void addSol(int[][] board, int n, List<List<String>> ans) { |
| 32 | List<String> arr = new ArrayList<>(); |