2814:拨钟问题

网址如下:

OpenJudge - 2814:拨钟问题

暴力枚举就行了

每个行为的次数不超过3,因为当次数为4的时候,相当于没动

然后就开始枚举

代码如下:

#include<cstdio>

const int maxn = 9;
//               A  B  C  D  E  F  G  H  I
int move[10][maxn]{{0, 0, 0, 0, 0, 0, 0, 0, 0},
                {1, 1, 0, 1, 1, 0, 0, 0, 0},
                {1, 1, 1, 0, 0, 0, 0, 0, 0},
                {0, 1, 1, 0, 1, 1, 0, 0, 0},
                {1, 0, 0, 1, 0, 0, 1, 0, 0},
                {0, 1, 0, 1, 1, 1, 0, 1, 0},
                {0, 0, 1, 0, 0, 1, 0, 0, 1},
                {0, 0, 0, 1, 1, 0, 1, 1, 0},
                {0, 0, 0, 0, 0, 0, 1, 1, 1},
                {0, 0, 0, 0, 1, 1, 0, 1, 1}};

struct Node{
    int clock[maxn];
    Node(){}
    void get_next(int m, int num){
        for(int i = 0; i < maxn; i++)
            clock[i] = clock[i] + move[m][i] * num;
    }
    bool is_end(void){
        for(int i = 0; i < maxn; i++)
            if(clock[i] % 4) return false;
        return true;
    }
};

int ans[10];
Node beg, end;

bool get_permutation(int maxd, int cur, int d){
    if(cur > maxn){
        end = beg;
        for(int i = 1; i <= maxn; i++) if(ans[i]) end.get_next(i, ans[i]);
        return end.is_end();
    }
    for(int i = 0; i < 4; i++){
        if(d + i > maxd) break;
        ans[cur] = i;
        if(get_permutation(maxd, cur + 1, d + i)) return true;
    }
    return false;
}

int main(void)
{
    for(int i = 0; i < maxn; i++) scanf("%d", &beg.clock[i]);
    for(int i = 0; i <= 27; i++) if(get_permutation(i, 1, 0)) break;
    for(int i = 1; i <= maxn; i++)
        if(ans[i]) while(ans[i]--) printf("%d ", i);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值