八皇后问题
public class Main{
public static List<List<Integer>> perm=new ArrayList<>();
public static int count=0;
public static void allPerm(List<Integer>list,int st,int en){
if (st==en){
perm.add(new ArrayList<Integer>(list));
}
for (int i=st;i<list.size();i++){
Collections.swap(list,st,i);
allPerm(list,st+1,en);
Collections.swap(list,st,i);
}
}
public static int EightQueen(){
List<Integer>l=new ArrayList<>();
for (int i=0;i<8;i++){
l.add(i+1);
}
allPerm(l,0,l.size());
for (List<Integer > x:perm
) {
boolean flag=false;
for (int i=0;i<7;i++){
for(int j=i+1;j<8;j++)
if (x.get(i)-x.get(j)==i-j||x.get(i)-x.get(j)==j-i){
flag=true;
break;
}
}
if (flag==false){
count++;
System.out.println(x.toString());
}
}
System.out.println(count);
return 0;
}
public static void main(String []args){
EightQueen();
}
}