资源简介
N皇后问题的启发式算法,N可为任意数.
N皇后问题的启发式算法,N可为任意数.
代码片段和文件信息
#include
#include
#include
#include
using namespace std;
void n_queens(int int[]);
void random_permutation(int int[]);
void swap(int[] int int);
int num_collision(int[] int);
int main() {
int num;
cout << “number of queens: “ ;
cin >> num;
cout << endl;
int q[num];
for (int i = 0; i < num; i++){
q[i] = i;
}
//clock_t startfinish;
//double totaltime;
//start=clock();
n_queens(num q);
//finish=clock();
//totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
//cout << “\nTime cost: “ << totaltime << “second!“ << endl;
int N = num;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (q[i] == j) cout << “Q “;
else cout << “* “;
}
cout << endl;
}
cout << endl;
system(“pause“);
return 0;
}
void n_queens(int num int q[]){
int collision = 0;
int swaps;
do{
random_permutation(num q
- 上一篇:C语言 文件压缩和解压
- 下一篇:jacobi符号计算
评论
共有 条评论