countNqueen (what is wrong in it)
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean[][] arr =new boolean[n][n];
int store=countIqueen(arr,0);
System.out.println(store);
}
public static int countIqueen(boolean[][] arr,int row ){
if(row == arr.length){
return 1;
}
int count=0;
for(int col=0;col<arr[row].length;col++){
if(isItSafe(arr,row,col)){
arr[row][col]=true;
count=count+countIqueen(arr,row+1);
arr[row][col]=false;
}
}
return count;
}
public static boolean isItSafe(boolean[][] arr, int row, int col){
//direction:up
for(int i=row-1;i>=0;i--){
if(arr[i][col]){
return false;
}
}
//direction:up-east
for(int i=row-1, j=col+1;i>=0 && j<arr[0].length;i--,j++){
if(arr[i][j]){
return false;
}
}
//direction:up-west
for(int i=row-1, j=col-1;i>=0 && j>=0;i--,j--){
if(arr[i][j]){
return false;
}
}
return true;
}
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.