#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] = '\0';
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("%s\n", strs[num - 1].str);
}
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有