import java.util.*;

public class harshitmalik03_NQueens{

    public static void main(String[] args) throws Exception{
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
//        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        boolean vis[][] = new boolean[n][n];
        ans.clear();
        backtracking(0,n,vis);
    }
    static ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        static void backtracking(int i, int n, boolean vis[][]){
            if(i == n){
                ArrayList<Integer> temp = new ArrayList<>();
                for(int j=0;j<n;j++)
                    for(int k=0;k<n;k++)
                        if(vis[j][k]) temp.add(k+1);
                ans.add(temp);
                System.out.print(temp);
                return;
            }
            for(int j=0;j<n;j++)
                if(isSafe(i,j,vis)){
                    vis[i][j] = true;
                    backtracking(i+1,n,vis);
                    vis[i][j] = false;
                }
        }
        static boolean isSafe(int row, int col, boolean vis[][]){
            for(int i=row-1,j=col;i>=0;i--)
                if(vis[i][j]) return false;
            for(int i=row-1,j=col-1;i>=0 && j >=0;i--,j--)
                if(vis[i][j]) return false;
            for(int i=row-1,j=col+1;i>=0 && j<vis[0].length;i--,j++)
                if(vis[i][j]) return false;
            return true;
        }





}
@@ -52,16 +52,100 @@
 NQueens%7B
+                                                                                    
 %0D%0A%0D%0A    
@@ -283,248 +283,632 @@
 t();
-%0D%0A//        ArrayList%3CArrayList%3CInteger%3E%3E ans = new ArrayList%3C%3E();%0D%0A        boolean vis%5B%5D%5B%5D = new boolean%5Bn%5D%5Bn%5D;%0D%0A        ans.clear();%0D%0A        backtracking(0,n,vis);%0D%0A    %7D%0D%0A    static ArrayList%3CArrayList%3CInteger%3E%3E ans = new ArrayList%3C%3E();
+                                                                                          //Taking input n where n represents an n*n chessboard%0D%0A        boolean vis%5B%5D%5B%5D = new boolean%5Bn%5D%5Bn%5D;                                                                            //And n Queens needs to be placed in the given chess board so that no Queen can attack each other.%0D%0A        ans.clear();%0D%0A        backtracking(0,n,vis);%0D%0A    %7D%0D%0A    static ArrayList%3CArrayList%3CInteger%3E%3E ans = new ArrayList%3C%3E();                                                       //Creating an arraylist of arraylist that stores all the arraylists of answers
 %0D%0A  
@@ -1056,16 +1056,104 @@
 ist%3C%3E();
+                                                            //temp is the list of answer
 %0D%0A      
@@ -1355,342 +1355,1105 @@
 mp);
-%0D%0A                return;%0D%0A            %7D%0D%0A            for(int j=0;j%3Cn;j++)%0D%0A                if(isSafe(i,j,vis))%7B%0D%0A                    vis%5Bi%5D%5Bj%5D = true;%0D%0A                    backtracking(i+1,n,vis);%0D%0A                    vis%5Bi%5D%5Bj%5D = false;%0D%0A                %7D%0D%0A        %7D%0D%0A        static boolean isSafe(int row, int col, boolean vis%5B%5D%5B%5D)%7B
+                                                                                 //Printing temp as each answer is generated. We can also add it in the given ans arraylist as done and then print the ans at the end.%0D%0A                return;%0D%0A            %7D%0D%0A            for(int j=0;j%3Cn;j++)                                                                                        //For each row we will traverse all the columns and see if we can place the queen there.%0D%0A                if(isSafe(i,j,vis))%7B%0D%0A                    vis%5Bi%5D%5Bj%5D = true;                                                                                   //Marking the spot visited%0D%0A                    backtracking(i+1,n,vis);%0D%0A                    vis%5Bi%5D%5Bj%5D = false;                                                                                  //Unvisiting the spot so we can find all the distinct solutions.%0D%0A                %7D%0D%0A        %7D%0D%0A        static boolean isSafe(int row, int col, boolean vis%5B%5D%5B%5D)%7B                                                       //Checking if it is safe to place the queen at this given spot.
 %0D%0A  
@@ -2808,8 +2808,10 @@
 %0A%0D%0A%0D%0A%0D%0A%7D
+%0D%0A

Add Comments