因为老师提到所以去试试了
#include<stdio.h>
#include<math.h>
#include<stdlib.h>//为了g++编译器里的abs函数
using namespace std;
int queenPlaces[92][8];//存放92种皇后棋子的摆放方法
int count = 0;
int board[8][8];//仿真棋盘
void putQueen(int ithQueen);//递归函数,每次摆好一个棋子
int main()
{
int n, i, j;
for (i = 0; i < 8; i++){//初始化
for (j = 0; j < 8; j++){
board[i][j] = -1;
}
for (j = 0; j < 92; j++) {
queenPlaces[j][i] = 0;
}
}
putQueen(0);//从第0个棋子开始摆放,运行的结果是将queenPlaces生成好
scanf("%d", &n);//不可以用scanf_s()
for (i = 0; i < n; i++) {
int ith;
scanf("%d", &ith);
for (j = 0; j < 8; j++) {
printf("%d", queenPlaces[ith - 1][j]);
}
printf("\n");
}
}
void putQueen(int ithQueen) {
int i, k, r;
if (ithQueen == 8)//如果摆满八个就计数加一
{
count++;
return;
}
for (i = 0; i < 8; i++)
{
if (board[i][ithQueen] == -1) {//摆放皇后
board[i][ithQueen] = ithQueen;
for (k = count; k < 92; k++)//没满则继续寻找
{
queenPlaces[k][ithQueen] = i + 1;
}
for (k = 0; k < 8; k++)
for (r = 0; r < 8; r++)
if (board[k][r] == -1 && (k == i || r == ithQueen || abs(k - i) == abs(r - ithQueen)))
board[k][r] = ithQueen;
putQueen(ithQueen + 1);//向下级递归
for (k = 0; k < 8; k++)//回溯,撤销控制范围
for (r = 0; r < 8; r++)
if (board[k][r] == ithQueen)
board[k][r] = -1;
}
}
}
最后编译成功了,还挺开心的。qwq