Mushroom_1965 @ 2023-05-31 17:15:14
自己感觉前后两次只是改了判断方式 结果第一次没过第二次过了(汗)
#include <cstdio>
bool a[14][14] = {0};
int n, cnt = 0, ans[14], tot = 1;
bool judge(int step, int col) {
for (int i = 1; i <= n; i++)
if (a[i][col] && i != step)
return 0;
for (int i = 1; i <= n; i++) {
int j = i - (step - col);
if (a[i][j] && i != step && j != col)
return 0;
}
for (int i = 1; i <= n; i++) {
int j = (step + col) - i;
if (a[i][j] && i != step && j != col)
return 0;
}
return 1;
}
void dfs(int step, int col) {
if (step == n + 1) {
if (tot <= 3) {
tot++;
for (int i = 2; i <= n + 1; i++)
printf("%d ", ans[i]);
printf("\n");
}
cnt++;
return;
}
for (int j = 1; j <= n; j++) {
if (judge(step, j)) {
a[step][j] = 1;
step++;
ans[step] = j;
dfs(step, j);
step--;
a[step][j] = 0;
}
}
}
int main() {
scanf("%d", &n);
dfs(1, 1);
printf("%d", cnt);
return 0;
}
#include <cstdio>
bool a[14][14] = {0}, forwardSlash[13] = {0}, backSlash[27] = {0}, colA[14] = {0};
int n, cnt = 0, ans[14], tot = 1;
bool judge(int step, int col) {
if (forwardSlash[step - col] || backSlash[step + col] || colA[col])
return 0;
return 1;
}
void dfs(int step, int col) {
if (step == n + 1) {
if (tot <= 3) {
tot++;
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
printf("\n");
}
cnt++;
return;
}
for (int j = 1; j <= n; j++) {
if (judge(step, j)) {
forwardSlash[step - j] = 1;
backSlash[step + j] = 1;
colA[j] = 1;
ans[step] = j;
step++;
dfs(step, j);
step--;
forwardSlash[step - j] = 0;
backSlash[step + j] = 0;
colA[j] = 0;
}
}
}
int main() {
scanf("%d", &n);
dfs(1, 1);
printf("%d", cnt);
return 0;
}