蓝桥杯-2n皇后问题

题目

问题描述

  给定一个n*n的棋盘,棋盘中有一些位置不能放皇后。现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行、同一列或同一条对角线上,任意的两个白皇后都不在同一行、同一列或同一条对角线上。问总共有多少种放法?n小于等于8。

输入格式

  输入的第一行为一个整数n,表示棋盘的大小。
  接下来n行,每行n个0或1的整数,如果一个整数为1,表示对应的位置可以放皇后,如果一个整数为0,表示对应的位置不可以放皇后。

输出格式

  输出一个整数,表示总共有多少种放法。

样例输入

4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

样例输出

2

样例输入

4
1 0 1 1
1 1 1 1
1 1 1 1
1 1 1 1

样例输出

0

代码

#include <stdio.h>
#include <stdlib.h>

int check(int **que, int row, int col, int n);
void output(int count, int **que, int n);
void findWhiteQueue(int *count, int **que, int row, int n);
void findQueue(int *count, int **que, int row, int n);

int main(int argc, const char * argv[]) {
    int i, j;
    int count = 0;
    int n;
    int input;
    scanf("%d", &n);
    int **que = (int**)malloc(sizeof(int*) * n);
    for(i=0; i<n; i++){
        que[i] = (int*)malloc(sizeof(int) * n);
    }
    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            scanf("%d", &input);
            if(input == 0){
                que[i][j] = -1;
            }
            else{
                que[i][j] = 0;
            }
        }
    }
    findQueue(&count, que, 0, n);
    printf("%d", count);
    return 0;
}

int check(int **que, int row, int col, int n){
    int i, j;
    if(que[row][col] == -1){
        return 0;
    }
    for(i=0; i<n; i++){
        if(que[i][col] == 1){
            return 0;
        }
    }
    for(i=row, j=col; i<n && j<n; i++, j++){
        if(que[i][j] == 1){
            return 0;
        }
    }
    for(i=row, j=col; i>=0 && j>=0; i--, j--){
        if(que[i][j] == 1){
            return 0;
        }
    }
    for(i=row, j=col; i>=0 && j<n; i--, j++){
        if(que[i][j] == 1){
            return 0;
        }
    }
    for(i=row, j=col; i<8 && j>=n; i++, j--){
        if(que[i][j] == 1){
            return 0;
        }
    }
    return 1;
}

void output(int count, int **que, int n){
    int i, j;
    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            if(que[i][j] == 1){
                printf("@ ");
            }
            else if(que[i][j] == -1){
                printf("* ");
            }
            else{
                printf("O ");
            }
        }
        printf("\n");
    }
    printf("%d\n", count);
    printf("\n-------------------------\n");
}

void findWhiteQueue(int *count, int **que, int row, int n){
    if(row > n - 1){
        (*count)++;
        //output(*count, que, n);
        return;
    }
    int col;
    for(col=0; col<n; col++){
        if(check(que, row, col, n)){
            que[row][col] = 1;
            findWhiteQueue(count, que, row + 1, n);
            que[row][col] = 0;
        }
    }
}

void findQueue(int *count, int **que, int row, int n){
    int i, j;
    if(row > n - 1){
        int **queWhite = (int**)malloc(sizeof(int*) * n);
        for(i=0; i<n; i++){
            queWhite[i] = (int*)malloc(sizeof(int) * n);
        }
        for(i=0; i<n; i++){
            for(j=0; j<n; j++){
                if(que[i][j] == 1){
                    queWhite[i][j] = -1;
                }
                else if(que[i][j] == -1){
                    queWhite[i][j] = -1;
                }
                else{
                    queWhite[i][j] = 0;
                }
            }
        }
        //(*count)++;
        //output(*count, que, n);
        findWhiteQueue(count, queWhite, 0, n);
        return;
    }
    int col;
    for(col=0; col<n; col++){
        if(check(que, row, col, n)){
            que[row][col] = 1;
            findQueue(count, que, row + 1, n);
            que[row][col] = 0;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星耀浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值