package com.nupt.lidata.interview;
/**
* Created by wyp on 2019/9/4.
*/
public class NQueue {
private static int N = 8;
private static int[] Nqueue = new int[N];
private static int count = 0;
private static boolean is_oK(int row)
{
for(int i = 0;i<row;i++)
{
if(Nqueue[i]== Nqueue[row]||Math.abs(Nqueue[row]-Nqueue[i])==row-i)
return false;
}
return true;
}
public static void queue(int row)
{
if(row == N)
{
count ++;
for(int i =0;i<N;i++)
{
System.out.print(Nqueue[i]+" ");
}
System.out.println();
return;
}
for(int i = 0;i<N;i++)
{
Nqueue[row] = i;
if(is_oK(row)){
queue(row+1);
}
}
}
public static void main(String[] args)
{
queue(0);
System.out.println(count);
}
}
参考:https://blog.csdn.net/qq_41033913/article/details/80054878·