/*回溯算法*///8皇后问题 /* 回溯算法 */ // 8皇后问题static const int n = 8; // 下标表示行,值表示列int Column_num[n+1] = {0}; int index = 0; // 解的个数int anwser_num = 0; int Palace(int Col[], int index){ int i; i
/*
回溯算法 */
// 8皇后问题static const int n = 8;
// 下标表示行,值表示列int Column_num[n+1] = {0};
int index = 0;
// 解的个数int anwser_num = 0;
int Palace(int Col[], int index){ int i; int Row_diff,Col_diff;
// 判断1,2,...index-1 个皇后是否相互兼容 for (i = 1; i void N_Queen(){ index = 1; while (index > 0) { Column_num[index]++; while (Column_num[index] <= n { Column_num[index]++; } if(Column_num[index] <= n) { if (index == n) { anwser_num++; // for (i = 1; i <= n; i++)// printf("%d\t",Column_num[i]);// printf("\n"); } else { index++; Column_num[index] = 0; } } else index--; } printf("anwser num=%d\n",anwser_num); } void main_nqueen(){ N_Queen();}