题解:B3761 [信息与未来 2021] 三角魔方

· · 题解

题意

B3761

对一个有 16 个字母的三角魔方,按给定操作序列执行 a^b 次操作。输出最终字母排列,一行 16 个字母。

解法

1. 定义

可以直接定义 16 个字母和 1 个辅助变量。

char swp,A='A',B='B',C='C',D='D',E='E',F='F',G='G',H='H',I='I',J='J',K='K',L='L',M='M',N='N',O='O',P='P';
//swp是辅助旋转操作的

2.转动函数

按题意赋值即可,以 U 操作为例:

void _U(int x)
{
    if(x==1) return;
    if(x==3) swp=E,E=K,K=L,L=swp;
    if(x==5) swp=B,B=F,F=G,G=M,M=N,N=swp;
    if(x==7) swp=A,A=C,C=D,D=H,H=I,I=O,O=P,P=swp;
    return;
}

R,D 操作与 U 类似: ::::info[R,D]

void _R(int x)
{
    if(x==1) return;
    if(x==3) swp=D,D=C,C=B,B=swp;
    if(x==5) swp=I,I=H,H=G,G=F,F=E,E=swp;
    if(x==7) swp=P,P=O,O=N,N=M,M=L,L=K,K=J,J=swp;
    return;
}
void _D(int x)
{
    if(x==1) return;
    if(x==3) swp=N,N=O,O=I,I=swp;
    if(x==5) swp=L,L=M,M=G,G=H,H=D,D=swp;
    if(x==7) swp=J,J=K,K=E,E=F,F=B,B=C,C=A,A=swp;
    return;
}

:::: 然后三种操作用函数再封装一下。

void change(string s)
{
    if(s[0]=='U') _U(s[1]-'0');
    if(s[0]=='R') _R(s[1]-'0');
    if(s[0]=='D') _D(s[1]-'0');
}

3.记录周期

上述代码已经可以完成旋转操作了,接下来处理 a,b 的问题。

显然,a^b 暴力是不可行的。

不过以相同序列转动魔方会产生循环,于是我们可以在找出循环后,对循环外余下的操作暴力模拟。

找循环很简单,就是先模拟一次循环,重复进行转动操作直到恢复原状为止。

bool check()
{
    if(A!='A' or B!='B' or C!='C' or D!='D' or E!='E' or F!='F' or G!='G' or H!='H' or I!='I' or J!='J' or K!='K' or L!='L' or M!='M' or N!='N' or O!='O' or P!='P') return 0;
    return 1;
}
do{
  for(int i=0;i<m.size();i++) change(m[i]);//m记录了每次操作的类型与编号
  cnt++;//记录次数
}while(!check());

思路就这么简单,下面是 AC 代码。

::::success[AC code]

#include<bits/stdc++.h>
using namespace std;

vector<string> m;
int a,b;
int sum=1;
char swp,A='A',B='B',C='C',D='D',E='E',F='F',G='G',H='H',I='I',J='J',K='K',L='L',M='M',N='N',O='O',P='P';

void _U(int x)
{
    if(x==1) return;
    if(x==3) swp=E,E=K,K=L,L=swp;
    if(x==5) swp=B,B=F,F=G,G=M,M=N,N=swp;
    if(x==7) swp=A,A=C,C=D,D=H,H=I,I=O,O=P,P=swp;
    return;
}
void _R(int x)
{
    if(x==1) return;
    if(x==3) swp=D,D=C,C=B,B=swp;
    if(x==5) swp=I,I=H,H=G,G=F,F=E,E=swp;
    if(x==7) swp=P,P=O,O=N,N=M,M=L,L=K,K=J,J=swp;
    return;
}
void _D(int x)
{
    if(x==1) return;
    if(x==3) swp=N,N=O,O=I,I=swp;
    if(x==5) swp=L,L=M,M=G,G=H,H=D,D=swp;
    if(x==7) swp=J,J=K,K=E,E=F,F=B,B=C,C=A,A=swp;
    return;
}
bool check()
{
    if(A!='A' or B!='B' or C!='C' or D!='D' or E!='E' or F!='F' or G!='G' or H!='H' or I!='I' or J!='J' or K!='K' or L!='L' or M!='M' or N!='N' or O!='O' or P!='P') return 0;
    return 1;
}
void change(string s)
{
    if(s[0]=='U') _U(s[1]-'0');
    if(s[0]=='R') _R(s[1]-'0');
    if(s[0]=='D') _D(s[1]-'0');
}
int main()
{
    string s;
    cin>>s;
    cin>>a>>b;
    int pos=0,cnt=0;
    while(pos<s.size())//这里是把操作序列拆开装进 m 里
    {
        string tmp="";
        tmp+=s[pos],tmp+=s[pos+1];
        m.push_back(tmp);
        pos+=2;
    }

    do{
        for(int i=0;i<m.size();i++) change(m[i]);
        cnt++;
    }while(!check());
    for(int i=1;i<=b;i++) sum=(sum*a)%cnt;
    while(sum--)
        for(int i=0;i<m.size();i++) change(m[i]);

    cout<<A<<B<<C<<D<<E<<F<<G<<H<<I<<J<<K<<L<<M<<N<<O<<P;
    return 0;
}

:::: 恭喜水绿 ^_^。