package com.fishercoder.solutions;

/**
 * 52. N-Queens II
 *
 * Follow up for N-Queens problem.
 * Now, instead outputting board configurations, return the total number of distinct solutions.
 */
public class _52 {

  public static class Solution1 {
    /**credit: https://discuss.leetcode.com/topic/29626/easiest-java-solution-1ms-98-22*/
    int count = 0;

    public int totalNQueens(int n) {
      boolean[] cols = new boolean[n];
      boolean[] diagnol = new boolean[2 * n];
      boolean[] antiDiagnol = new boolean[2 * n];
      backtracking(0, cols, diagnol, antiDiagnol, n);
      return count;
    }

    private void backtracking(int row, boolean[] cols, boolean[] diagnol, boolean[] antiDiagnol,
        int n) {
      if (row == n) {
        count++;
      }
      for (int col = 0; col < n; col++) {
        int x = col - row + n;
        int y = col + row;
        if (cols[col] || diagnol[x] || antiDiagnol[y]) {
          continue;
        }
        cols[col] = true;
        diagnol[x] = true;
        antiDiagnol[y] = true;
        backtracking(row + 1, cols, diagnol, antiDiagnol, n);
        cols[col] = false;
        diagnol[x] = false;
        antiDiagnol[y] = false;
      }
    }
  }
}

@@ -33,169 +33,8 @@
 s;%0A%0A
-/**%0A * 52. N-Queens II%0A *%0A * Follow up for N-Queens problem.%0A * Now, instead outputting board configurations, return the total number of distinct solutions.%0A */%0A
 publ
@@ -49,16 +49,18 @@
  _52 %7B%0A%0A
+  
   public
@@ -93,11 +93,27 @@
     
-/**
+    /**%0A         * 
 cred
@@ -192,11 +192,25 @@
 8-22
-*/%0A
+%0A         */%0A    
     
@@ -225,16 +225,20 @@
 t = 0;%0A%0A
+    
     publ
@@ -268,24 +268,30 @@
  n) %7B%0A      
+  
+    
 boolean%5B%5D co
@@ -307,24 +307,30 @@
 boolean%5Bn%5D;%0A
+      
       boolea
@@ -365,24 +365,30 @@
 * n%5D;%0A      
+   
+   
 boolean%5B%5D an
@@ -421,24 +421,30 @@
 * n%5D;%0A      
+   
+   
 backtracking
@@ -485,16 +485,22 @@
 ;%0A      
+   
+   
 return c
@@ -513,11 +513,19 @@
     
-%7D%0A%0A
+    %7D%0A%0A    
     
@@ -617,16 +617,42 @@
 iagnol,%0A
+                          
         
@@ -662,24 +662,30 @@
  n) %7B%0A      
+ 
+     
 if (row == n
@@ -696,16 +696,24 @@
         
+       
+ 
 count++;
@@ -719,24 +719,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A     
       
+ 
 for (int
@@ -771,24 +771,32 @@
 n; col++) %7B%0A
+        
         int 
@@ -818,24 +818,32 @@
  n;%0A        
+   
+     
 int y = col 
@@ -849,16 +849,24 @@
  + row;%0A
+        
         
@@ -910,24 +910,34 @@
 agnol%5By%5D) %7B%0A
+          
           co
@@ -952,26 +952,41 @@
         
+  
-%7D%0A
+      %7D%0A       
         
 cols%5Bcol
@@ -969,32 +969,33 @@
 %0A               
+ 
 cols%5Bcol%5D = true
@@ -996,32 +996,40 @@
 = true;%0A        
+     
+   
 diagnol%5Bx%5D = tru
@@ -1031,32 +1031,40 @@
 = true;%0A        
+   
+     
 antiDiagnol%5By%5D =
@@ -1062,32 +1062,40 @@
 gnol%5By%5D = true;%0A
+        
         backtrac
@@ -1132,32 +1132,40 @@
 ntiDiagnol, n);%0A
+        
         cols%5Bcol
@@ -1175,32 +1175,40 @@
  false;%0A        
+    
+    
 diagnol%5Bx%5D = fal
@@ -1203,32 +1203,40 @@
 nol%5Bx%5D = false;%0A
+        
         antiDiag
@@ -1261,16 +1261,28 @@
     
+      %7D%0A        
 %7D%0A  
-  %7D%0A
   %7D%0A

refactor 52