package com.zhuyu_deng.test;
class NQueues
{
int n;
int x[];
NQueues(int k)
{
n = k;
x = new int[n];
}
void trackback()
{
explore(0);
}
void explore(int k)
{
if (k >= n)
{
print();
return;
}
for (int colum = 0; colum < n; ++colum)
{
x[k] = colum;
if (isPartial(k))
explore(k+1);
}
}
boolean isPartial(int k)
{
for (int i = 0; i < k; ++i)
{
int dif = x[i] - x[k];
if (dif == 0 || dif == i - k || dif == k - i)
return false;
}
return true;
}
void print()
{
for (int i = 0; i < n; ++i)
System.out.print(x[i] + " ");
System.out.println();
}
}
public class Test
{
public static void main(String args[])
{
int c[][] = { { 0, 1, 1, 1, 0 }, { 1, 0, 1, 0, 1 }, { 1, 1, 0, 1, 0 },
{ 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0 } };
NQueues q = new NQueues(4);
q.trackback();
}
}
NQueues
最新推荐文章于 2021-03-21 11:50:14 发布