HDU - 5983 Pocket Cube 强行模拟

The Pocket Cube, also known as the Mini Cube or the Ice Cube, is the 2 × 2 × 2 equivalence of a Rubik’s Cube.
The cube consists of 8 pieces, all corners.
Each piece is labeled by a three dimensional coordinate (h, k, l) where h, k, l ∈ {0, 1}. Each of the six faces owns four small faces filled with a positive integer.
For each step, you can choose a certain face and turn the face ninety degrees clockwise or counterclockwise.
You should judge that if one can restore the pocket cube in one step. We say a pocket cube has been restored if each face owns four same integers.

啥话不说了,直接模拟吧,虽然写的一般,但是比较稳定了。
其实可以整体旋转,然后在转两次,这样写感觉代码量更少。
要注意一次不转也是YES

#include
#include
#define find fuck
#define tp a[1],a[2],a[3],a[4]
#define bt a[9],a[10],a[11],a[12]
#define fr a[5],a[6],a[7],a[8]
#define bk a[13],a[14],a[15],a[16]
#define lt a[17],a[18],a[19],a[20]
#define rt a[21],a[22],a[23],a[24]
using namespace std;
typedef long long ll;
int a[30];
bool eq(int a,int b,int c,int d)
{
    return a == b and a == c and a == d;
}
void sw(int &a,int &b,int &c,int &d)
{
    swap(a,c);
    swap(b,d);
}
bool check()
{
    return eq(tp) and eq(bt) and eq(fr) and eq(bk) and eq(rt) and eq(lt) ;
}
void R(int o)
{
    //printf("Rotate!\n");
    if(o == 1)
    {
        sw(a[2],a[4],a[6],a[8]);
        sw(a[6],a[8],a[10],a[12]);
        sw(a[10],a[12],a[14],a[16]);
    }
    else
    {
        int t1 = a[2],t2 = a[4];
        sw(a[2],a[4],a[14],a[16]);
        sw(a[10],a[12],a[14],a[16]);
        sw(a[6],a[8],a[10],a[12]);
    }
}
void L(int o)
{
    //printf("Rotate!\n");
    if(o == 1)
    {
        sw(a[1],a[3],a[5],a[7]);
        sw(a[5],a[7],a[9],a[11]);
        sw(a[9],a[11],a[13],a[15]);
    }
    else
    {
        sw(a[1],a[3],a[13],a[15]);
        sw(a[9],a[11],a[13],a[15]);
        sw(a[5],a[7],a[9],a[11]);
    }
}
void U(int o)
{
    if(o)
    {
        sw(a[5],a[6],a[21],a[23]);
        sw(a[18],a[20],a[5],a[6]);
        sw(a[15],a[16],a[18],a[20]);
    }
    else
    {
        sw(a[5],a[6],a[18],a[20]);
        sw(a[21],a[23],a[5],a[6]);
        sw(a[15],a[16],a[21],a[23]);
    }
}
void D(int o)
{
    if(o)
    {
        sw(a[7],a[8],a[22],a[24]);
        sw(a[17],a[19],a[7],a[8]);
        sw(a[13],a[14],a[17],a[19]);
    }
    else
    {
        sw(a[7],a[8],a[17],a[19]);
        sw(a[22],a[24],a[7],a[8]);
        sw(a[13],a[14],a[22],a[24]);
    }
}
void F(int o)
{
    if(o)
    {
        sw(a[3],a[4],a[23],a[24]);
        sw(a[20],a[19],a[3],a[4]);
        sw(a[9],a[10],a[20],a[19]);
    }
    else
    {
        sw(a[3],a[4],a[20],a[19]);
        sw(a[23],a[24],a[3],a[4]);
        sw(a[9],a[10],a[23],a[24]);
    }
}
void B(int o)
{
    if(o)
    {
        sw(a[1],a[2],a[22],a[24]);
        sw(a[17],a[19],a[1],a[2]);
        sw(a[11],a[12],a[17],a[19]);
    }
    else
    {
        sw(a[1],a[2],a[17],a[19]);
        sw(a[11],a[12],a[17],a[19]);
        sw(a[22],a[24],a[11],a[12]);
    }
}
void print()
{
    printf("top:\n%d-%d\n%d-%d\n",a[1],a[2],a[3],a[4]);
    printf("fr:\n%d-%d\n%d-%d\n",a[5],a[6],a[7],a[8]);
    printf("bottom:\n%d-%d\n%d-%d\n",a[9],a[10],a[11],a[12]);
    printf("back:\n%d-%d\n%d-%d\n",a[13],a[14],a[15],a[16]);
    printf("left:\n%d-%d\n%d-%d\n",a[17],a[18],a[19],a[20]);
    printf("right:\n%d-%d\n%d-%d\n",a[21],a[22],a[23],a[24]);
}
int main()
{

    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]);
        scanf("%d%d%d%d",&a[5],&a[6],&a[7],&a[8]);
        scanf("%d%d%d%d",&a[9],&a[10],&a[11],&a[12]);
        scanf("%d%d%d%d",&a[13],&a[14],&a[15],&a[16]);
        scanf("%d%d%d%d",&a[17],&a[18],&a[19],&a[20]);
        scanf("%d%d%d%d",&a[21],&a[22],&a[23],&a[24]);

        bool ans = check();
        R(0); ans |= check(); R(1);

        R(1); ans |= check(); R(0);

        L(0); ans |= check(); L(1);
        L(1); ans |= check(); L(0);

        U(1); ans |= check(); U(0);
        U(0); ans |= check(); U(1);

        D(1); ans |= check(); D(0);
        D(0); ans |= check(); D(1);

        F(0); ans |= check(); F(1);
        F(1); ans |= check(); F(0);

        B(0); ans |= check(); B(1);
        B(1); ans |= check(); B(0);

        printf("%s\n",( !ans ? "NO" :"YES" ));
    }
    return 0;
}

你可能感兴趣的:(ACM,Problems)