#include<iostream>
#include<cstdlib>
#include<ctime>
#define N 20
using namespace std
;
int T
[100];
int k
=0;
bool place(int j
);
bool backtrace(int row
){
T
[k
+1]=0;
if(row
==N
)
return true;
while(k
>=row
){
T
[k
+1]+=1;
while((k
+1)<=N
&&place(T
[k
+1])==false)
T
[k
+1]++;
if(T
[k
+1]<=N
){
if((k
+1)==N
)
return true;
else
{
k
++;
T
[k
+1]=0;
}
}
else
{
k
--;
}
}
return false;
}
bool place(int j
){
for(int i
=1;i
<=k
;i
++)
if((j
-T
[i
])==0||abs(j
-T
[i
])==k
+1-i
)
return false;
return true;
}
bool QueensLv(int stepVegas
){
k
=0;
int nb
;
int col
;
while(true){
nb
=0;
for(int j
=1;j
<=N
;j
++){
if(place(j
)){
nb
++;
if((rand()%nb
+1)==1)
col
=j
;
}
}
if(nb
>0){
k
++;
T
[k
]=col
;
}
if(nb
==0||k
==stepVegas
)
break;
}
if(nb
>0){
return backtrace(stepVegas
);
}
else
{
return false;
}
}
void print(){
for(int i
=1;i
<=N
;i
++)
cout
<<"("<<i
<<","<<T
[i
]<<")"<<endl
;
return;
}
void test(int m
){
double time
[N
+1]={0.0};
int correct
[N
+1]={0};
clock_t start
,ends
;
bool success
;
for(int i
=1;i
<=N
;i
++){
int count
=0;
while(true){
start
=clock();
success
=QueensLv(i
);
ends
=clock();
time
[i
]+=(ends
-start
);
count
++;
if(success
)
{
correct
[i
]++;
}
if(count
==m
){
break;
}
}
}
int count
=0;
while(1){
k
=0;
start
=clock();
success
=backtrace(0);
ends
=clock();
time
[0]+=(ends
- start
);
count
++;
if(success
){
correct
[0]++;
}
if(count
==m
){
break;
}
}
for(int i
=0;i
<=N
;i
++){
double t
=time
[i
]/CLOCKS_PER_SEC
;
cout
<<"stepVegas="<<i
<<",success:"<<correct
[i
]<<","<<"total time is "<<t
<<",average success time is "<<t
/correct
[i
]<<endl
;
}
return;
}
int main(){
srand(time(0));
cout
<<"input m:";
int m
;
cin
>>m
;
cout
<<"N="<<N
<<endl
;
test(m
);
return 0;
}
可以看到,最优stepVegas在N/2左右。
转载请注明原文地址: https://lol.8miu.com/read-30160.html