POJ1166 - The Clocks(拨钟问题)题解

题目链接拨钟问题
拨钟问题1
拨钟问题2
样例输入

3 3 0
2 2 2
2 1 2

样例输出

4 5 8 9

下图是9种移动会影响的时钟。
9个按钮影响的时钟
这道题还是枚举来解决,不要问我怎么想出来的,因为这是枚举的课后习题。
直接暴力枚举9种移动,判断是否都指向了12点。
放心,最多就是4^9次循环,小的很。
Then show the code.

#include <cstdio>

void OutputResult(int i, int &count){
    printf("%d", i);
    if(count > 1)
        printf(" ");
    count--;
}

int main(){
    //a数组存放初始时钟状况
    //b数组存放1-9个按钮被按下了多少次
    //c数组存放按钮按下后时钟状况
    int a[10], b[10], c[10] = {0};
    for(int i = 1; i <= 9; i += 3){
        scanf("%d %d %d", &a[i], &a[i+1], &a[i+2]);
    }

    for(b[1] = 0; b[1] < 4; b[1]++)
        for(b[2] = 0; b[2] < 4; b[2]++)
            for(b[3] = 0; b[3] < 4; b[3]++)
                for(b[4] = 0; b[4] < 4; b[4]++)
                    for(b[5] = 0; b[5] < 4; b[5]++)
                        for(b[6] = 0; b[6] < 4; b[6]++)
                            for(b[7] = 0; b[7] < 4; b[7]++)
                                for(b[8] = 0; b[8] < 4; b[8]++)
                                    for(b[9] = 0; b[9] < 4; b[9]++){
                                        c[1] = (a[1] + b[1] + b[2] + b[4])%4;
                                        c[2] = (a[2] + b[1] + b[2] + b[3] + b[5])%4;
                                        c[3] = (a[3] + b[2] + b[3] + b[6])%4;
                                        c[4] = (a[4] + b[1] + b[4] + b[5] + b[7])%4;
                                        c[5] = (a[5] + b[1] + b[3] + b[5] + b[7] + b[9])%4;
                                        c[6] = (a[6] + b[3] + b[5] + b[6] + b[9])%4;
                                        c[7] = (a[7] + b[4] + b[7] + b[8])%4;
                                        c[8] = (a[8] + b[5] + b[7] + b[8] + b[9])%4;
                                        c[9] = (a[9] + b[6] + b[8] + b[9])%4;
                                        //如果时钟都到达12点 那么c数组的元素都应该为0
                                        if((c[1]+c[2]+c[3]+c[4]+c[5]+c[6]+c[7]+c[8]+c[9]) == 0){
                                            int count = b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9];
                                            for(int i = 0; i < b[1]; i++) OutputResult(1, count);
                                            for(int i = 0; i < b[2]; i++) OutputResult(2, count);
                                            for(int i = 0; i < b[3]; i++) OutputResult(3, count);
                                            for(int i = 0; i < b[4]; i++) OutputResult(4, count);
                                            for(int i = 0; i < b[5]; i++) OutputResult(5, count);
                                            for(int i = 0; i < b[6]; i++) OutputResult(6, count);
                                            for(int i = 0; i < b[7]; i++) OutputResult(7, count);
                                            for(int i = 0; i < b[8]; i++) OutputResult(8, count);
                                            for(int i = 0; i < b[9]; i++) OutputResult(9, count);
                                            return 0;
                                        }
                                    }
    return 0;
}
posted @ 2020-10-25 17:22  1v7w  阅读(408)  评论(0编辑  收藏  举报