RVBTCC/demo/queen.c
2024-12-07 12:24:00 +08:00

46 lines
813 B
C

int printf(char* format, ...);
int putchar(int ch);
int a[9];
void output() {
for (int i = 1; i <= 8; ++i) {
for (int j = 1; j <= 8; ++j) {
if (a[i] == j) {
putchar('x');
} else {
putchar('-');
}
}
putchar('\n');
}
putchar('\n');
}
int ok(int x, int y) {
for (int i = 1; i <= x - 1; ++i) {
if (a[i] == y || a[i] - i == y - x || a[i] + i == y + x) {
return 0;
}
}
return 1;
}
void queen(int x) {
if (x > 8) {
output();
a[0]++;
}
for (int y = 1; y <= 8; ++y) {
if (ok(x, y)) {
a[x] = y;
queen(x + 1);
a[x] = 0;
}
}
}
int main() {
queen(1);
printf("solutions: %d\n", a[0]);
}