八皇后问题的相关C++代码解答示例
八皇后问题即指在一个8*8的棋盘上放置8个皇后,不允许任何两个皇后在棋盘的同一行、同一列和同一对角线上。关键字:递归、上溯.通用技巧: 每行有且仅有一个皇后: 题目描述: 思路 由于八个皇后不能处在同一行,那么可以肯定每个皇后占据一行。我们可以先定义一个数组column[9],数组中的第i个数字表示位于第i行皇后的列号(因为数组下标从0开始,因此这里想表示1-8需要申请9个整型的数据空间)。 #include <stdio.h> #include <stdlib.h> #include <string.h> #define EIGHT 8 struct result { int total; int num[10]; }; int wzyindex,column[10]; struct result results[100]; /** * Description:预处理八皇后的下标数组 */ void pre_prosess(int n) { int i; for (i = 1; i <= n; i ++) { column[i] = i; } } /** * Description:column数组数字交换 */ void swap(int begin,int k) { int temp; temp = column[begin]; column[begin] = column[k]; column[k] = temp; } /** * Description:防止全排列出现重复数据 */ int check_swap(int begin,int k) { int i; for (i = begin; i < k; i ++) { if (column[i] == column[k]) { return 0; } } return 1; } int is_eightqueue(int n) { int i,j; for (i = 1; i <= n; i ++) { for (j = i + 1; j <= n; j ++) { if (i - j == column[i] - column[j] || i - j == column[j] - column[i]) return 0; } } return 1; } void permutation_queue(int begin,int end) { int k,total; if (begin == end) { // 检查八皇后排列正确性 if (is_eightqueue(end)) { for (k = 1,total = 0; k <= end; k ++) { total = 10 * total + column[k]; results[wzyindex].num[k] = column[k]; } results[wzyindex].total = total; wzyindex ++; } } else { // 全排列 for (k = begin; k <= end; k ++) { if (check_swap(begin,k)) { // 保证无重复的全排列 swap(begin,k); permutation_queue(begin + 1,end); swap(begin,k); } } } } int compare(const void *p,const void *q) { const struct result *a = p; const struct result *b = q; return a->total - b->total; } int main() { int i,n,m; pre_prosess(EIGHT); wzyindex = 0; permutation_queue(1,EIGHT); qsort(results,wzyindex,sizeof(results[0]),compare); while (scanf("%d",&n) != EOF) { while (n --) { scanf("%d",&m); m -= 1; for (i = 1; i <= EIGHT; i ++) { printf("%d",results[m].num[i]); } printf("n"); } } return 0; } /************************************************************** #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #define N 8 typedef struct point { int x,y; } point; point pts[N]; typedef struct string { char str[N + 1]; } string; string strs[93]; int windex,count; int isOk(int x,int y) { int i,flag = 1; for (i = 0; i < count; i ++) { if (pts[i].y == y || abs(y - pts[i].y) == abs(x - pts[i].x)) { flag = 0; break; } } return flag; } void bfsEight(int level) { int i; if (level > N) { for (i = 0; i < N; i ++) { strs[windex].str[i] = pts[i].y + '0'; } strs[windex].str[i] = ' '; windex ++; } else { point t; for (i = 1; i <= N; i ++) { t.x = level; t.y = i; if (isOk(t.x,t.y)) { pts[count ++] = t; bfsEight(level + 1); count -= 1; } } } } int cmp(const void *p,const void *q) { const string *a = p; const string *b = q; return strcmp(a->str,b->str); } int main(void) { int n,num; count = windex = 0; bfsEight(1); qsort(strs,count,sizeof(strs[0]),cmp); scanf("%d",&n); while (n --) { scanf("%d",&num); printf("%sn",strs[num - 1].str); } return 0; } /************************************************************** (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |