AC日记——魔方 洛谷 P2007

本文介绍了一种使用C++实现的魔方模拟转动算法。通过定义魔方的结构体及不同方向的转动操作,实现了对魔方状态的更新。代码中详细展示了右转90度、右转负90度、顶面转90度及其逆向操作的具体实现。

魔方

 

思路:

  模拟;

 

代码:

#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("\n");
    for(int i=1;i<=6;i++)
    {
        printf("%d\n",i);
        for(int j=1;j<=3;j++)
        {
            for(int v=1;v<=3;v++) printf("%d",ci[i].ai[j][v]);
            printf("\n");
        }
        printf("\n");
    }
    printf("\n");
}

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("\n");
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/IUUUUUUUskyyy/p/6912748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值