6x6的棋盘有4种情况
我承认这个判断斜角的方法不好,但是我没有好的方法
sh-5.2$ cat main.c
#include <stdio.h>
#include <stdbool.h>
void display(bool array[6][6]) {
for(size_t y = 0; y < 6; ++y) {
for(size_t x = 0; x < 6; ++x) {
printf("%d ", array[y][x]);
}
puts("");
}
puts("");
}
bool check(bool array[6][6]) {
for(size_t y = 0; y < 6; ++y) {
size_t sum = 0;
for(size_t x = 0; x < 6; ++x) {
sum += array[y][x];
}
if(sum > 1) return false;
}
for(size_t x = 0; x < 6; ++x) {
size_t sum = 0;
for(size_t y = 0; y < 6; ++y) {
sum += array[y][x];
}
if(sum > 1) return false;
}
for(size_t y = 0; y < 11; ++y) {
size_t sum = 0;
for(size_t x = 0, ny = y; x < 6; ++x, ++ny) {
if(ny < 5) continue;
if(ny >= 11) continue;
sum += array[ny - 5][x];
}
if(sum > 1) return false;
}
for(size_t y = 0; y < 11; ++y) {
size_t sum = 0;
for(size_t x = 5, ny = y; x < 6; --x, ++ny) {
if(ny < 5) continue;
if(ny >= 11) continue;
sum += array[ny - 5][x];
}
if(sum > 1) return false;
}
return true;
}
void chess(bool array[6][6], size_t y) {
if(y == 6) {
if(check(array)) display(array);
return;
}
for(size_t x = 0; x < 6; ++x) {
array[y][x] = true;
chess(array, y + 1);
array[y][x] = false;
}
}
int main(void) {
bool array[6][6] = {false};
chess(array, 0);
return 0;
}
sh-5.2$ ./main
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 1
1 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 1 0
0 0 1 0 0 0
0 0 0 0 0 1
0 1 0 0 0 0
0 0 0 0 1 0
1 0 0 0 0 0
0 0 0 1 0 0
0 0 0 1 0 0
1 0 0 0 0 0
0 0 0 0 1 0
0 1 0 0 0 0
0 0 0 0 0 1
0 0 1 0 0 0
0 0 0 0 1 0
0 0 1 0 0 0
1 0 0 0 0 0
0 0 0 0 0 1
0 0 0 1 0 0
0 1 0 0 0 0
sh-5.2$
|