3. What will be the output of the following Java code snippet for N=4?
public class NQueensTest {
static int count = 0;
public static void main(String[] args) {
int n = 4;
int[][] board = new int[n][n];
solveNQueens(0, n, board);
System.out.println(count);
}
static boolean solveNQueens(int row, int n, int[][] board) {
if (row == n) {
count++;
return true;
}
boolean res = false;
for (int col = 0; col < n; col++) {
if (isSafe(row, col, board, n)) {
board[row][col] = 1;
res = solveNQueens(row + 1, n, board) || res;
board[row][col] = 0;
}
}
return res;
}
static boolean isSafe(int row, int col, int[][] board, int n) {
for (int i = 0; i < row; i++) {
if (board[i][col] == 1) return false;
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 1) return false;
}
for (int i = row, j = col; i >= 0 && j < n; i--, j++) {
if (board[i][j] == 1) return false;
}
return true;
}
}