postfix chain
This commit is contained in:
parent
3910fc3413
commit
fc9ac15c4b
2 changed files with 58 additions and 6 deletions
47
demo/queen.c
Normal file
47
demo/queen.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
int printf(const 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]);
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue