棋盘问题 用C语言

Description

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input

输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。
Sample Input

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output

2
1

/**
 *  @author wowpH
 *  @date 2019-9-14 19:54:16
 */
#include<stdio.h>
#include<string.h>

#define TRUE 1
#define FALSE 0

#define MAX_N 8

#define BOARD TRUE
#define BLANK FALSE

int board[MAX_N][MAX_N];// 棋盘,BOARD表示棋盘,BLANK表示空白

int n, k, solution;

int row[MAX_N];// 每行是否有棋子,TRUE表示有棋子,FALSE表示无棋子
int column[MAX_N];// 每列...同上

void backTrack(int left, int x) {// 回溯,left表示剩余棋子,x表示当前行
    if (left == 0) {// 无多余棋子
        ++solution;// 方案数加1
        return;
    }

    // 遍历x行及下方的棋盘
    for (int i = x; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (board[i][j] == BLANK) {// 空白
                continue;// 不能放旗子
            }
            if (row[i] == TRUE || column[j] == TRUE) {// 行或列有棋子
                continue;// 不能放旗子
            }

            // 当前位置可以放子,设为TRUE
            row[i] = TRUE;
            column[j] = TRUE;
            // 棋子数减1,行数加1
            backTrack(left - 1, i + 1);
            // 复盘,设为无子
            row[i] = FALSE;
            column[j] = FALSE;
        }
    }
}

int main() {
    while (scanf("%d %d", &n, &k) && n != -1 && k != -1) {
        getchar();// '\n'

        // 输入棋盘
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                char ch = getchar();
                if (ch == '.') {
                    board[i][j] = BLANK;
                } else if (ch == '#') {
                    board[i][j] = BOARD;
                }
            }
            getchar();// '\n'
        }

        // 初始化
        memset(row, FALSE, sizeof(row));
        memset(column, FALSE, sizeof(column));
        solution = 0;

        backTrack(k, 0);// 回溯

        printf("%d\n", solution);
    }
    return 0;
}

图片说明