蒙自市蒙自一中马晨烨 • 1年前
#include <cstdio>
#include <iostream>
using namespace std;
bool col[15], recline1[30], recline2[30];//列,左斜,右斜标记(想想为什么要把斜的设为两倍大?)
int queen[15], n;//记录每一行的横坐标
void backtracking(int idx) {
if (idx == n + 1) {
for (int k = 1; k <= n; k++) {
printf(" %d", queen[k]);
}
printf("\n");
return ;
}
for (int j = 1; j <= n; j++) {
queen[idx] = j;
if (!col[queen[idx]] && !recline1[n - queen[idx] + idx] && !recline2[queen[idx] + idx - 1]) {//毫无问题的完美公式(生物课上闲着无聊推的)
col[queen[idx]] = 1;
recline1[n - queen[idx] + idx] = 1;
recline2[queen[idx] + idx - 1] = 1;
backtracking(idx);
col[queen[idx]] = 0;
recline1[n - queen[idx] + idx] = 0;
recline2[queen[idx] + idx - 1] = 0;
}
}
}
int main() {
cin >> n;
if (n <= 3) {
cout << "no solute!";
return 0;
}//直接特判
backtracking(1);
return 0;
}
评论:
请先登录,才能进行评论