题解 | #N皇后问题#
N皇后问题
http://www.nowcoder.com/practice/c76408782512486d91eea181107293b6
import java.util.*;
public class Solution {
/**
*
* @param n int整型 the n
* @return int整型
*/
static int count = 0;
public static boolean place(int[] arr,int s){
for(int i=0;i<s;i++){
if(arr[i] == arr[s] || Math.abs(s-i) == Math.abs(arr[s]-arr[i])){
return false;
}
}
return true;
}
public static int GET(int[] queue,int i,int n){
if(i >= n){
count++;
}else{
for(int j=0;j<n;j++){
queue[i] = j;
if(place(queue,i)){
GET(queue,i+1,n);
}
}
}
return count;
}
public int Nqueen (int n) {
// write code here
int[] queue = new int[n];
return GET(queue,0,n);
}
}