###### XMU 1098 N皇后
#include<iostream>
#include<cmath>
#include<time.h>
using namespace std;
const unsigned long maxshort=65536L;
const unsigned long multiplier=1194211693L;
class RandomNumber
{
private:
unsigned long randSeed;
public:
RandomNumber(unsigned long s=0);
unsigned short Random(unsigned long n);
double fRandom(void);
};

RandomNumber::RandomNumber(unsigned long s)
{
if(s==0)
randSeed=time(0);
else
randSeed=s;
}
unsigned short RandomNumber::Random(unsigned long n)
{
randSeed = multiplier * randSeed + adder;
return (unsigned short) ((randSeed>>16) % n);
}
double RandomNumber::fRandom(void)
{
return Random(maxshort)/double(maxshort);
}
class Queen
{
friend bool nQueen(int);
private:
bool place(int k);
bool backtrack(int t);
bool queenLV(int stopLV);
int n ,*x,*y;
};

bool Queen::place(int k)
{
for(int j=1;j<k;j++)
if((abs(k-j)==abs(x[j]-x[k])) || (x[j]==x[k]))
return false;
return true;
}

bool Queen::backtrack(int t)
{
if(t>n)
{
for(int i=1;i<=n;i++)
y[i]=x[i];
return true;
}
else
for(int i=1;i<=n;i++)
{
x[t]=i;
if(place(t) && backtrack(t+1))
return true;
}
return false;
}

bool Queen::queenLV(int stopLV)
{
RandomNumber rnd;
int k=1;
int count=1;
while((k<= stopLV ) && (count>0))
{
count=0;
for(int i=1;i<=n;i++)
{
x[k]=i;
if(place(k))
y[count++]=i;
}
if(count>0)
x[k++]=y[rnd.Random(count)];
}
return (count>0);

}

bool nQueen(int n)
{
Queen X;
X.n=n;
int *p=new int[n+1];
int *q=new int[n+1];
for(int i=0;i<=n;i++)
{
p[i]=0;
q[i]=0;
}
X.y=p;
X.x=q;
int stop=5;
if(n<5)
stop=1;
if(n>15)
stop=n-15;
bool found = false;
while(!X.queenLV(stop));
if(X.backtrack(stop+1))
{
for(int i=1;i<=n;i++)
cout<<i<<" "<<p[i]<<endl;
found = true;
}
delete[] p;
delete[] q;
return found;
}

int main()
{
int n;
cin>>n;
while(!nQueen(n));
return 0;
}

#### N皇后的问题

2016-05-06 21:49:42

#### 【HPUoj】1218 - 2n皇后问题（dfs）

2017-10-10 11:05:24

#### 算法设计与分析

2013年12月22日 1KB 下载

#### NS2.3 在 fedora上的安装出现 can't find X headers 以及 nam的make找不到 winutil.h

2012-01-06 17:53:16

#### 【动态规划】HDU 1081 & XMU 1031 To the Max

2016-08-16 23:36:01

#### 1098 数学题

2015-02-18 15:10:50

#### 杭电acm1098

2015-10-04 15:24:29

#### lightoj 1098 - A New Function 因子和

2013-11-09 12:30:15

#### Lightoj 1098【数学/玄学】

2016-11-17 01:37:59

#### 计算智能历年卷

2018年01月25日 19.59MB 下载

XMU 1098 N皇后