253 - Cube painting (UVA)

文章讲述了如何解决UVA253Cubepainting问题中的字符串旋转判断,介绍了两种不同的解法,一种是逐层旋转四次,另一种是嵌套循环进行三维旋转。作者对第二个看似乱来的代码逻辑提出了疑问。
摘要由CSDN通过智能技术生成

题目链接如下:

Online Judge

这个解法是参考题解 UVA253 【Cube painting】 - FP·荷兰猪 的博客 - 洛谷博客 写的,这应该是符合逻辑的解法。最外面的循环是代表分别以不同位置为顶,内部循环是水平旋转四次,看得到的结果。代码如下:

#include <string>
#include <iostream>
// #define debug

std::string str, s, t;
int dir[6][6] = {{0, 1, 2, 3, 4, 5}, {1, 5, 2, 3, 0, 4}, {2, 1, 5, 0, 4, 3},
                {3, 1, 0, 5, 4, 2}, {4, 3, 0, 5, 2, 1}, {5, 1, 3, 2, 4, 0}};

bool judge(std::string a, std::string b){
    for(int i = 0; i < 6; ++i){
        std::string mp;
        for(int j = 0; j < 6; ++j){
            mp.push_back(a[dir[i][j]]);
        }
        for(int j = 0; j < 4; ++j){
            char c = mp[2];
            mp[2] = mp[4];
            mp[4] = mp[3];
            mp[3] = mp[1];
            mp[1] = c;
            if(mp == b){
                return true;
            }
        }
    }
    return false;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(std::cin >> str){
        s = str.substr(0, 6);
        t = str.substr(6);
        printf("%s\n", judge(s, t) ? "TRUE" : "FALSE");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

下面这个代码是我写的,能AC,但我觉得逻辑不对,是乱拳打死老师傅AC的……今天脑子昏昏沉沉的,等我脑子好使一些再来检查。

代码如下:

#include <string>
#include <iostream>

std::string str, s, t;

void rotate1(std::string &a){
    std::string b = a;
    a[0] = b[1];
    a[1] = b[5];
    a[4] = b[0];
    a[5] = b[4];
}

void rotate2(std::string &a){
    std::string b = a;
    a[1] = b[2];
    a[2] = b[4];
    a[3] = b[1];
    a[4] = b[3];
}

void rotate3(std::string &a){
    std::string b = a;
    a[0] = b[2];
    a[2] = b[5];
    a[3] = b[0];
    a[5] = b[3];
}

bool judge(std::string a, std::string b){
    for(int i = 0; i <= 3; ++i){
        rotate1(a);
        for(int j = 0; j <= 3; ++j){
            rotate2(a);
            for(int k = 0; k <= 3; ++k){
                rotate3(a);
                if(a == b){
                    return true;
                }
            }
        }
    }
    return false;
}

int main(){
    while(std::cin >> str){
        s = str.substr(0, 6);
        t = str.substr(6);
        printf("%s\n", judge(s, t) ? "TRUE" : "FALSE");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值