when i tried to execute my own code it didn’t work but later when i tried to copy the code from video it didn’t work either (it is giving 0 as output)
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
boolean[][] arr1 = new boolean[4][4];
System.out.println(countNqueens(arr1, 0));
}
public static int countNqueens(boolean[][] arr, int row){
if(row == arr.length){
return 1;
}
int count =0;
for(int col=0; col<arr[row].length; col++){
if(isTrue(arr,row,col)){
arr[row][col] = true;
count = count+countNqueens(arr,row++);
arr[row][col] = false;
}
} return count;
}
public static boolean isTrue(boolean[][] arr, int row, int col){
for(int i=row; i>=0; i--){
if(arr[i][col]){
return false;
}
}
for(int i=row, j= col; i>=0&&j>=0; i--,j--){
if(arr[i][j]){
return false;
}
}
for(int i=row, j= col; i>=0&&j<=arr.length; i--,j++){
if(arr[i][j]){
return false;
}
}
return true;
}
}