Cube painting,UVa 253题解

本文详细解析了UVa253立方体涂色问题,通过深度优先搜索(DFS)遍历所有可能的立方体旋转状态,判断两个立方体是否通过旋转达到相同的状态。代码实现清晰,提供了完整的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目】Cube painting,UVa 253

We have a machine for painting cubes. It is supplied with three different colors: blue, red and green. Each face of the cube gets one of these colors. The cube’s faces are numbered as in Figure 1. Since a cube has 6 faces, our machine can paint a face-numbered cube in 3 6 = 729 different ways. When ignoring the face-numbers, the number of different paintings is much less, because a cube can be rotated. See example below. We denote a painted cube by a string of 6 characters, where each character is a ‘b’, ‘r’, or ‘g’. The i-th character (1 ≤ i ≤ 6) from the left gives the color of face i. For example, Figure 2 is a picture of “rbgggr” and Figure 3 corresponds to “rggbgr”. Notice that both cubes are painted in the same way: by rotating it around the vertical axis by 90°, the one changes into the other.

input

The input of your program is a textfile that ends with the standard end-of-file marker. Each line is a string of 12 characters. The first 6 characters of this string are the representation of a painted cube, the remaining 6 characters give you the representation of another cube. Your program determines whether these two cubes are painted in the same way, that is, whether by any combination of rotations one can be turned into the other. (Reflections are not allowed.)

output

The output is a file of boolean. For each line of input, output contains ‘TRUE’ if the second half can be obtained from the first half by rotation as describes above, ‘FALSE’ otherwise.

Sample Input

rbgggrrggbgr

rrrbbbrrbbbr

rbgrbgrrrrrg

Sample Output

TRUE

FALSE

FALSE

 

题目大意:就是要你判断两个正方体是否是相同的。

我的思路:利用dfs遍历第一个正方体旋转后的状态,判断是否与第二个正方体相同。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <memory.h>
#include <stack>
typedef long long LL;
using namespace std;
string a,s1,s2;
map<string,int>mp;
bool ans = 0;
void dfs(string s1,string s2)
{
    if(s1==s2)
    {
        ans = 1;
        return;
    }
    if(ans == 1 || mp[s1]==1)
        return ;
    mp[s1] = 1;
    string s3=s1;
    for(int i = 0; i < 4; i++)
    {
        switch(i)
        {
            case 0:
                s3=s1;
                s3[0] = s1[4];
                s3[1] = s1[0];
                s3[2] = s1[2];
                s3[3] = s1[3];
                s3[4] = s1[5];
                s3[5] = s1[1];
                break;
            case 1:
                s3=s1;
                s3[0] = s1[3];
                s3[2] = s1[0];
                s3[5] = s1[2];
                s3[3] = s1[5];
                break;
            case 2:
                s3=s1;
                s3[3] = s1[0];
                s3[5] = s1[3];
                s3[2] = s1[5];
                s3[0] = s1[2];
                break;
            case 3:
                s3=s1;
                s3[4] = s1[0];
                s3[5] = s1[4];
                s3[1] = s1[5];
                s3[0] = s1[1];
                break;
        }
        if(mp[s3]==1)
            continue;
        if(s3==s2)
        {
            ans = 1;
            mp[s3] = 1;
            return;
        }
        else
        {
            mp[s3] = 1;
            dfs(s3,s2);
        }
    }
    if(ans == 1)
        return ;
    ans = 0;
    return ;
}
int main()
{
    while(cin >> a)
    {
        s1="";
        s2="";
        mp.clear();
        ans = 0;
        for(int i = 0; i < 6; i++)
            s1+=a[i];
        for(int i = 6; i < a.size(); i++)
            s2+=a[i];
        dfs(s1,s2);
        if(ans ==1)
            cout << "TRUE" << endl;
        else
            cout << "FALSE" << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值