ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UVA253 骰子涂色 Cube painting 题解

2021-12-19 16:02:18  阅读:150  来源: 互联网

标签:Cube struct int 题解 char 涂色 mmst 排序


直接模拟就行了。

我的方法似乎比其他题解麻烦(傻瓜式做法),但逻辑简单,容易理解。

用一个结构体来存骰子的每一个面的相邻面

char a[7], b[7];
struct node{
    char s;
    char b[5];
}an[7], bn[7];
an[1].s = a[1], an[1].b[1] = a[2], an[1].b[2] = a[3], an[1].b[3] = a[4], an[1].b[4] = a[5];
an[2].s = a[2], an[2].b[1] = a[1], an[2].b[2] = a[3], an[2].b[3] = a[4], an[2].b[4] = a[6];
an[3].s = a[3], an[3].b[1] = a[1], an[3].b[2] = a[2], an[3].b[3] = a[5], an[3].b[4] = a[6];
...省略9行(两个数组类似)

然后排序,

先把每个面的相邻面排序,

再把排序。

最后比较两个数组,判断输出就行了。

注意 UVA 的多组数据。

AC 完整代码

#include <bits/stdc++.h>
using namespace std;
char a[7], b[7];
struct node{
    char s;
    char b[5];
}an[7], bn[7];
void mmst() { //初始化是个好习惯
    for(int i = 1; i <= 6; i++) {
        a[i] = ' ', b[i] = ' ';
        for(int j = 1; j <= 4; j++) an[i].b[j] = ' ', bn[i].b[j] = ' ';
    }
}
bool cmp(node x, node y) { //较为保险的比较函数
    if(x.s == y.s) {
        if(x.b[1] == y.b[1]) {
            if(x.b[2] == y.b[2]) {
                if(x.b[3] == y. b[3]) {
                    if(x.b[4] == y.b[4]) {
                        if(x.b[5] == y.b[5]) return x.b[6] < y.b[6];
                        else return x.b[5] < y.b[5];
                    }else return x.b[4] < y.b[4];
                }else return x.b[3] < y.b[3];
            }else return x.b[2] < y.b[2];
        }else return x.b[1] < y.b[1];
    }
    return x.s < y.s;
}
bool check() {
    an[1].s = a[1], an[1].b[1] = a[2], an[1].b[2] = a[3], an[1].b[3] = a[4], an[1].b[4] = a[5];
    an[2].s = a[2], an[2].b[1] = a[1], an[2].b[2] = a[3], an[2].b[3] = a[4], an[2].b[4] = a[6];
    an[3].s = a[3], an[3].b[1] = a[1], an[3].b[2] = a[2], an[3].b[3] = a[5], an[3].b[4] = a[6];
    an[4].s = a[4], an[4].b[1] = a[1], an[4].b[2] = a[2], an[4].b[3] = a[5], an[4].b[4] = a[6];
    an[5].s = a[5], an[5].b[1] = a[1], an[5].b[2] = a[3], an[5].b[3] = a[4], an[5].b[4] = a[6];
    an[6].s = a[6], an[6].b[1] = a[2], an[6].b[2] = a[3], an[6].b[3] = a[4], an[6].b[4] = a[5];

    bn[1].s = b[1], bn[1].b[1] = b[2], bn[1].b[2] = b[3], bn[1].b[3] = b[4], bn[1].b[4] = b[5];
    bn[2].s = b[2], bn[2].b[1] = b[1], bn[2].b[2] = b[3], bn[2].b[3] = b[4], bn[2].b[4] = b[6];
    bn[3].s = b[3], bn[3].b[1] = b[1], bn[3].b[2] = b[2], bn[3].b[3] = b[5], bn[3].b[4] = b[6];
    bn[4].s = b[4], bn[4].b[1] = b[1], bn[4].b[2] = b[2], bn[4].b[3] = b[5], bn[4].b[4] = b[6];
    bn[5].s = b[5], bn[5].b[1] = b[1], bn[5].b[2] = b[3], bn[5].b[3] = b[4], bn[5].b[4] = b[6];
    bn[6].s = b[6], bn[6].b[1] = b[2], bn[6].b[2] = b[3], bn[6].b[3] = b[4], bn[6].b[4] = b[5];
    for(int i = 1; i <= 6; i++) 
        sort(an[i].b + 1, an[i].b + 5), sort(bn[i].b + 1, bn[i].b + 5);
    sort(an + 1, an + 7, cmp);
    sort(bn + 1, bn + 7, cmp);
    for(int i = 1; i <= 6; i++) for(int j = 1; j <= 4; j++)
        if(an[i].b[j] != bn[i].b[j]) return false;
    return true;
}
int main() {
    string str;
    while(cin >> str) {
        mmst();
        for(int i = 0; i <= 5; i++) a[i + 1] = str[i];
        for(int i = 6; i <= 11; i++) b[i - 5] = str[i];
        if(check()) printf("TRUE\n");
        else printf("FALSE\n");
    }
    return 0;
}

标签:Cube,struct,int,题解,char,涂色,mmst,排序
来源: https://www.cnblogs.com/sunskydp/p/solution-uva253.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有