一道搜索题

来源:百度知道 编辑:UC知道 时间:2024/05/17 19:44:34
3.棋盘问题(CHESS)
提交文件名:CHESS.PAS

问题描述:
在5*5的方格棋盘中,若在某一个方格内放入一个黑棋子,则与该方格相邻的上、下、左、右四个方格内不能再放白棋子。

问题求解:
请你设计一个程序,寻找并打印出所有放置7个黑棋子后,再也不能放一个白棋子的方案和方案总数。

请用C语言!也可以把算法讲一下。

#include <stdio.h>

static int chess[25];

void clean(void) {
for (int i = 0; i < 25; i++)
chess[i] = 0;
}

int check(void) {
for (int i = 0; i < 25; i++)
if (chess[i] == 0)
return 0;

return 1;
}

void foo(void) {
for (int a = 0; a < 19; a++) {

for (int b = a + 1; b < 20; b++) {
for (int c = b + 1; c < 21; c++) {
for (int d = c + 1; d < 22; d++) {
for (int e = d + 1; e < 23; e++) {
for (int f = e + 1; f < 24; f++) {
for (int g = f + 1; g < 25; g++) {
chess[a] = 1;
chess[(a - 1) < 0 ? 0 : (a % 5 == 0) ? a : a - 1] = 1;
chess[(a - 5) < 0 ? a : a - 5] = 1;
chess[(a + 1) % 5 == 0 ? a : a + 1 ] = 1;
chess[a + 5] = 1;

chess[b] = 1;
chess[(b % 5 == 0) ? b : b - 1] = 1;
chess[(b - 5