Codeforces Round #444 (Div. 2) - C - Solution for Cube

传送门:点击打开链接

题意:给出一个魔方,每个数字代表一种颜色,可以将每个面顺(逆)时针翻转九十度,问是否能让魔方的每个面颜色都相同。

分析:乍一看就是个模拟,总共有6种翻转情况,将每次翻转的情况进行判断就行了,结果写了很久的代码,代码有两百多行,精简之后还有一百多行,看来下别人的代码,代码很精简,只有几十行,学到了,后面发现并不需要代码去模拟翻转,手动模拟一下,直接判断6种情况下6个面的颜色是否一致就行了,代码要少很多,平时思维训练太少了,还是要多打cf。另外需要注意一个,将数组作为形参,传入的是地址,改变参数数组,原数组也会跟着改变,这是需要在函数内部加开一个数组复制数值,害得我改了很久的bug,基础不扎实啊。

代码一:

#include<bits/stdc++.h>
using namespace std ;
const int maxn = 10000 + 20 ;
typedef long long ll;
int a[25],b[25],fg;
int ok( int b[] ){
    int f = 1;
    for(int i=1; i<24; i+=4)
        if( !(b[i] == b[i+1] && b[i+1] == b[i+2] && b[i+2] == b[i+3]) ) f=0;
    return f;
}
void turn1()
{
    memcpy(b,a,sizeof(b));
    int tp = b[1];
    b[1] = b[5];
    b[5] = b[9];
    b[9] = b[24];
    b[24] = tp;
    tp = b[3];
    b[3] = b[7];
    b[7] = b[11];
    b[11] = b[22];
    b[22] = tp;
    if( ok(b) ) fg=1;
}
void turn2(  )
{
    memcpy(b,a,sizeof(b));
    int tp = b[24];
    b[24] = b[9];
    b[9] = b[5];
    b[5] = b[1];
    b[1] = tp;
    tp  = b[22];
    b[22] = b[11];
    b[11] = b[7];
    b[7] = b[3];
    b[3] = tp;
    if( ok(b) ) fg=1;
}
void turn3( )
{
    memcpy(b,a,sizeof(b));
    int tp = b[13];
    b[13] = b[5];
    b[5] = b[17];
    b[17] = b[21];
    b[21] = tp;
    tp = b[14];
    b[14] = b[6];
    b[6] = b[18];
    b[18] = b[22];
    b[22] = tp;
    if( ok(b) ) fg=1;
}
void turn4(  )
{
    memcpy(b,a,sizeof(b));
    int tp = b[21];
    b[21] = b[17];
    b[17] = b[5];
    b[5] = b[13];
    b[13] = tp;
    tp = b[22];
    b[22] = b[18];
    b[18] = b[6];
    b[6] = b[14];
    b[14] = tp;
    if( ok(b) ) fg=1;
}
void turn5( )
{
    memcpy(b,a,sizeof(b));
    int tp = b[4];
    b[4] = b[14];
    b[14] = b[9];
    b[9] = b[19];
    b[19] = tp;
    tp = b[3];
    b[3] = b[16];
    b[16] = b[10];
    b[10] = b[17];
    b[17] = tp;
    if( ok(b) ) fg=1;
}
void turn6(  )
{
    memcpy(b,a,sizeof(b));
    int tp = b[19];
    b[19] = b[9];
    b[9] = b[14];
    b[14] = b[4];
    b[4] = tp;
    tp = b[17];
    b[17] = b[10];
    b[10] = b[16];
    b[16] = b[3];
    b[3] = tp;
    if( ok(b) ) fg=1;
}
int main() {
    for( int i = 1; i <= 24; i ++ ) cin >> a[i];
    turn1();
    turn2();
    turn3();
    turn4();
    turn5();
    turn6();
    if( fg ) cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0 ;
}


代码二:

#include<bits/stdc++.h>
using namespace std ;
const int maxn = 10000 + 20 ;
typedef long long ll;
int a[25],b[25],f=1,fg=0;
int c[3][8]={ {1,3,5,7,9,11,24,22},
              {5,6,17,18,21,22,13,14},
              {3,4,17,19,10,9,16,14}
            };

void ok(){
    f=1;
    for(int i=1; i<24; i+=4)
        for(int j=1;j<=3;j++)
            if(b[i+j]!=b[i]) f=0;
}

void turn(int x,int d) {
    memcpy(b,a,sizeof(b));
    for(int i=0;i<8;i++)
        b[c[x][i]]=a[c[x][(i+d+8)%8]];
    ok();
    if(f) fg=1;
}

void sv() {
    for(int i=0;i<3;i++) {
        turn(i,2);
        turn(i,-2);
    }
    if(fg) cout << "YES" << endl;
    else cout << "NO" << endl;
}

int main() {
    for( int i = 1; i <= 24; i ++ ) cin >> a[i];
    sv();
}

代码三:

#include<bits/stdc++.h>
using namespace std ;
int a[25];

int ok(int i,int j,int k,int l){
    if(a[i]==a[j] && a[j]==a[k] && a[k]==a[l]) return 1;
    return 0;
}

void sv() {
    int f=0;
    if( ok(13,14,15,16) && ok(17,18,19,20) && ok(1,3,21,23) && ok(2,4,5,7) && ok(6,8,9,11) && ok(10,12,22,24)) f=1;
    else if( ok(13,14,15,16) && ok(17,18,19,20) && ok(1,3,6,8) && ok(5,7,10,12) && ok(9,11,21,23) && ok(2,4,22,24) ) f=1;
    else if( ok(1,2,3,4) && ok(9,10,11,12) && ok(5,6,19,20) && ok(17,18,23,24) && ok(15,16,21,22) && ok(7,8,13,14) ) f=1;
    else if( ok(1,2,3,4) && ok(9,10,11,12) && ok(5,6,15,16) && ok(7,8,17,18) && ok(19,20,21,22) && ok(13,14,23,24) ) f=1;
    else if( ok(5,6,7,8) && ok(21,22,23,24) && ok(3,4,13,15) && ok(11,12,14,16) && ok(9,10,18,20) && ok(1,2,17,19) ) f=1;
    else if( ok(5,6,7,8) && ok(21,22,23,24) && ok(1,2,14,16) && ok(9,10,13,15) && ok(11,12,17,19) && ok(3,4,18,20) ) f=1;
    if( f ) cout << "YES" << endl;
    else cout << "NO" << endl;
}

int main() {
    for( int i = 1; i <= 24; i ++ ) cin >> a[i];
    sv();
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/tianwei0822/article/details/80534180