恶魔附身女主复仇电影:小弟有一程序请各位高手指教!!

来源:百度文库 编辑:高校问答 时间:2024/04/30 02:03:15
小弟有一回溯法求8皇后的C程序,有谁能帮我改成:回溯+随机算法求8皇后问题呢?(急待解决)
代码如下:
/* 设一8*8棋盘如下图: */
/* 1 2 3 4 5 6 7 8 */
/* 1:0 0 0 0 0 0 0 * */
/* 2:0 * 0 0 0 0 0 0 */
/* 3:0 0 0 * 0 0 0 0 */
/* 4:* 0 0 0 0 0 0 0 */
/* 5:0 0 0 0 0 0 * 0 */
/* 6:0 0 0 0 * 0 0 0 */
/* 7:0 0 * 0 0 0 0 0 */
/* 8:0 0 0 0 0 * 0 0 */
/* 可用一8位序列表示8个皇后的位置,如“82417536”,其意为顺序地列出各行中皇后所在列的序号 */
/* 则问题转换为以下两点: */
/* 1.生成8位序列queen,并使序列中各位数字均不重复,即保证不同列(不同行在定义序列时已能避免). */
/* 2.在满足(1)的序列中找出一个序列,使序列中任意两位上数字之差的绝对值不等于他们的序号 */
/* 之差的绝对值,即在棋盘同一对角线上不存在两个皇后. */
/* 所有满足(2)的序列即为八皇后问题的解. */
//八皇后(回朔法)
#include <iostream.h>
# include <math.h>
#include <time.h>
#include <stdlib.h>
int total = 0; //方案计数
void backtrace(int queen[],int N)
{
int i, j, k;
for (i=1;;){ //首先安放第1行皇后
if(queen[i]<N){ //皇后还可调整
k=0; //检查与第k个皇后是否互相攻击
while(k<i&&abs(queen[k]-queen[i])&&(abs(queen[k]-queen[i])-abs(k-i))) k++;
if (k<=i-1){ //与第k个皇后互相攻击
queen[i]++; //第i个皇后右移一列,重测
continue;
}
i++; //无冲突,安置下一行皇后
if(i<N) continue;
for(j=0;j<N;j++) cout<<queen[j]; //已完成,输出结果
cout<<" ";
total++; //方案数加1
if(total%5==0) cout<<endl;
queen[N-1]++; // 将第8个皇后右移一列,前8个不动
i=N-1; //此处是制造机会,如不成功则回溯,关键一步
}
else //当前行皇后无法安置,回溯
{
queen[i]=0; //当前行皇后回归1列
i--; //回溯到前一行皇后
if(i<0){ //回溯到数组1行之前,结束
cout<<" 总数:"<<total<<endl;
return;
}
else queen[i]++; //前一行皇后右移一列
}
}

}

void main(){

while(1){

clock_t start, finish;
double duration;
int N;
cout<<"please input the number of queens"<<endl;
cin>>N;
int* queen=new int[N];
for (int i=0;i<N;i++) queen[i] = 0; //八皇后全放在第0列
int n=N; /* 定义数组p[N]用来存放结果序列,n为行号 */
start=clock();
total=0;
backtrace(queen,N);
finish=clock();
duration=(double)(finish-start);
cout<<"\n"<<"backtrace produce "<<total<<" kinds of permutate methods relative to "<<N<<" queens"<<endl;
cout<<"backtrace method spend time is "<<duration<<" seconds"<<endl;
delete []queen;
}

}

代码如下:
/* 设一8*8棋盘如下图: */
/* 1 2 3 4 5 6 7 8 */
/* 1:0 0 0 0 0 0 0 * */
/* 2:0 * 0 0 0 0 0 0 */
/* 3:0 0 0 * 0 0 0 0 */
/* 4:* 0 0 0 0 0 0 0 */
/* 5:0 0 0 0 0 0 * 0 */
/* 6:0 0 0 0 * 0 0 0 */
/* 7:0 0 * 0 0 0 0 0 */
/* 8:0 0 0 0 0 * 0 0 */
/* 可用一8位序列表示8个你妈妈的逼的位置,如“82417536”,其意为顺序地列出各行中皇后所在列的序号 */
/* 则问题转换为以下两点: */
/* 1.生成8位序列queen,并使序列中各位数字均不重复,即保证不同列(不同行在定义序列时已能避免). */
/* 2.在满足(你爸爸的性欲1)的序列中找出一个序列,使序列中任意两位上数字之差的绝对值不等于他们的序号 */
/* 之差的绝对值,即在棋盘同一对角线上不存在两个皇后. */
/* 所有满足(2)的序列即为八皇后问题的解. */
//你妈妈的逼(回朔法)
#include <iostream.h>
# include <math.h>
#include <time.h>
#include <stdlib.h>
int total = 0; //方案计数
void backtrace(int queen[],int N)
{
int i, j, k;
for (i=1;;){ //首先安放第1行你妈妈的逼 if(queen[i]<N){ //皇后还可调整
k=0; //检查与第k个皇后是否互相攻击
while(k<i&&abs(queen[k]-queen[i])&&(abs(queen[k]-queen[i])-abs(k-i))) k++;
if (k<=i-1){ //与第k个皇后互相攻击
queen[i]++; //第i个皇后右移一列,重测
continue;
}
i++; //无冲突,安置下一行皇后
if(i<N) continue;
for(j=0;j<N;j++) cout<<queen[j]; //已完成,输出结果
cout<<" ";
total++; //方案数加1
if(total%5==0) cout<<endl;
queen[N-1]++; // 将第8个皇后右移一列,前8个不动
i=N-1; //此处是制造机会,如不成功则你妈妈的逼,关键一步
}
else //当前行皇后无法安置,回溯
{
queen[i]=0; //当前行皇后回归1列
i--; //回溯到前一行皇后
if(i<0){ //回溯到数组1行之前,结束
cout<<" 总数:"<<total<<endl;
return;
}
else queen[i]++; //前一行皇后右移一列
}
}

}

void main(){

while(1){

clock_t start, finish;
double duration;
int N;
cout<<"please input the number of queens"<<endl;
cin>>N;
int* queen=new int[N];
for (int i=0;i<N;i++) queen[i] = 0; //八皇后全放在第0列
int n=N; /* 定义数组p[N]用来存放结果序列,n为行号 */
start=clock();
total=0;
backtrace(queen,N);
finish=clock();
duration=(double)(finish-start);
cout<<"\n"<<"backtrace produce "<<total<<" kinds of permutate methods relative to "<<N<<" queens"<<endl;你妈妈的逼,,你爸爸的性欲 cout<<"backtrace method spend time is "<<duration<<" seconds"<<endl;
delete []queen;
}

}