POJ 2965 The Pilots Brothers' refrigerator 组合数递归算法

POJ 2965 The Pilots Brothers’ refrigerator 组合数递归算法

本题和POJ1753基本一样

#include<iostream>
#include <math.h>
using namespace std;
int a[16]={0};
int result[16]={0};
int p[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
bool check(int *a){
    for (int i = 0; i < 16; ++i) {
        if (a[i]==0)return false;
    }
    return true;
}
void zuhe(int *a,int totalNum,int selectNum,int start,int *result,int resultStart,int resultNum){
    if (selectNum==0){
        int a2[16];
        for (int i = 0; i < 16; ++i) {
            a2[i]=a[i];
        }

        for (int j = 0; j < resultNum; ++j) {
            if (a2[result[j]]==0)a2[result[j]]=1;
            else a2[result[j]]=0;
            int x=result[j]/4;
            int y=result[j]%4;
            for (int i = 0; i < 4; ++i) {
                int temp=i*4+y;
                if (temp!=result[j]){
                    if (a2[temp]==0)a2[temp]=1;
                    else a2[temp]=0;
                }
            }
            for (int k = 0; k <4 ; ++k) {
                int temp=x*4+k;
                if (temp!=result[j]){
                    if (a2[temp]==0)a2[temp]=1;
                    else a2[temp]=0;
                }
            }
        }
        if (check(a2)==true){
            cout<<resultNum<<endl;
            for (int i = 0; i < resultNum; ++i) {
                int x=result[i]/4;
                int y=result[i]%4;
                cout<<x+1<<" "<<y+1<<endl;
            }
            exit(0);
        }
        else return;
    }

    for (int i = start; i <= totalNum-selectNum; ++i) {
        result[resultStart]=p[i];
        zuhe(a,totalNum,selectNum-1,i+1,result,resultStart+1,resultNum);
    }
}
int main(){
    char input;
    for (int i = 0; i < 16; ++i) {
        cin>>input;
        if (input=='+')a[i]=0;
        else a[i]=1;
    }
    for (int selectNum = 1; selectNum <=16 ; ++selectNum) {
        zuhe(a,16,selectNum,0,result,0,selectNum);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值