AC日记——魔方 洛谷 P2007

魔方

思路:

  模拟;

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

struct MFType {
    int ai[4][4];
};
struct MFType ci[7];

int len;

char done[500],ch[10];

/*

1.front
2.back
3.left
4.right
5.top
6.under

*/

void OperationType_right_turning90()
{
    MFType *now=&ci[4],tmp=ci[4];
    now->ai[1][1]=tmp.ai[3][1];
    now->ai[1][2]=tmp.ai[2][1];
    now->ai[1][3]=tmp.ai[1][1];
    now->ai[2][1]=tmp.ai[3][2];
    now->ai[2][3]=tmp.ai[1][2];
    now->ai[3][1]=tmp.ai[3][3];
    now->ai[3][2]=tmp.ai[2][3];
    now->ai[3][3]=tmp.ai[1][3];
    now=&ci[1],tmp=ci[1];
    MFType *pos=&ci[6];
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
    now=&ci[6],pos=&ci[2];
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
    now=&ci[2],pos=&ci[5];
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
    now=&ci[5],pos=&tmp;
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
}

void OperationType_right_turningback90()
{
    MFType *now=&ci[4],tmp=ci[4];
    now->ai[1][1]=tmp.ai[1][3];
    now->ai[1][2]=tmp.ai[2][3];
    now->ai[1][3]=tmp.ai[3][3];
    now->ai[2][1]=tmp.ai[1][2];
    now->ai[2][3]=tmp.ai[3][2];
    now->ai[3][1]=tmp.ai[1][1];
    now->ai[3][2]=tmp.ai[2][1];
    now->ai[3][3]=tmp.ai[3][1];
    now=&ci[1],tmp=ci[1];
    MFType *pos=&ci[5];
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
    now=&ci[5],pos=&ci[2];
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
    now=&ci[2],pos=&ci[6];
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
    now=&ci[6],pos=&tmp;
    now->ai[1][3]=pos->ai[1][3];
    now->ai[2][3]=pos->ai[2][3];
    now->ai[3][3]=pos->ai[3][3];
}

void OperationType_top_turning90()
{
    MFType *now=&ci[5],tmp=ci[5];
    now->ai[1][1]=tmp.ai[3][1];
    now->ai[1][2]=tmp.ai[2][1];
    now->ai[1][3]=tmp.ai[1][1];
    now->ai[2][1]=tmp.ai[3][2];
    now->ai[2][3]=tmp.ai[1][2];
    now->ai[3][1]=tmp.ai[3][3];
    now->ai[3][2]=tmp.ai[2][3];
    now->ai[3][3]=tmp.ai[1][3];
    now=&ci[1],tmp=ci[1];
    MFType *pos=&ci[3];
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
    now=&ci[3],pos=&ci[2];
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
    now=&ci[2],pos=&ci[4];
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
    now=&ci[4],pos=&tmp;
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
}

void OperationType_top_turningback90()
{
    MFType *now=&ci[5],tmp=ci[5];
    now->ai[1][1]=tmp.ai[1][3];
    now->ai[1][2]=tmp.ai[2][3];
    now->ai[1][3]=tmp.ai[3][3];
    now->ai[2][1]=tmp.ai[1][2];
    now->ai[2][3]=tmp.ai[3][2];
    now->ai[3][1]=tmp.ai[1][1];
    now->ai[3][2]=tmp.ai[2][1];
    now->ai[3][3]=tmp.ai[3][1];
    now=&ci[1],tmp=ci[1];
    MFType *pos=&ci[4];
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
    now=&ci[4],pos=&ci[2];
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
    now=&ci[2],pos=&ci[3];
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
    now=&ci[3],pos=&tmp;
    now->ai[1][1]=pos->ai[1][1];
    now->ai[1][2]=pos->ai[1][2];
    now->ai[1][3]=pos->ai[1][3];
}

void debug()
{
    printf("
");
    for(int i=1;i<=6;i++)
    {
        printf("%d
",i);
        for(int j=1;j<=3;j++)
        {
            for(int v=1;v<=3;v++) printf("%d",ci[i].ai[j][v]);
            printf("
");
        }
        printf("
");
    }
    printf("
");
}

int main()
{
    freopen("cube.in","r",stdin);
    freopen("cube.out","w",stdout);
    scanf("%s",done),len=strlen(done);
    for(int i=1;i<=6;i++)
    {
        for(int j=1;j<=3;j++)
        {
            scanf("%s",ch);
            for(int v=1;v<=3;v++) ci[i].ai[j][v]=ch[v-1]-'0';
        }
    }
//    debug();
    for(int i=0;i<len;i++)
    {
        if(done[i]=='1') OperationType_right_turning90();
        else if(done[i]=='2') OperationType_right_turningback90();
        else if(done[i]=='3') OperationType_top_turning90();
        else if(done[i]=='4') OperationType_top_turningback90();
//        debug();
    }
    for(int i=1;i<=6;i++)
    {
        for(int j=1;j<=3;j++)
        {
            for(int v=1;v<=3;v++) printf("%d",ci[i].ai[j][v]);
            printf("
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6912748.html