列表

详情


ZJ4. 附加题

描述

二阶魔方又叫小魔方,是2*2*2的立方形结构。每一面都有4个块,共有24个块。每次操作可以将任意一面逆时针或者顺时针旋转90°,如将上面逆时针旋转90°操作如下。

Nero在小魔方上做了一些改动,用数字替换每个块上面的颜色,称之为数字魔方。魔方上每一面的优美度就是这个面上4个数字的乘积,而魔方的总优美度就是6个面优美度总和。
现在Nero有一个数字魔方,他想知道这个魔方在操作不超过5次的前提下能达到的最大优美度是多少。
魔方展开后每一块的序号如下图:

输入描述

输入一行包含24个数字,按序号顺序给出魔方每一块上面的数字。所有数大小范围为[-100,100]。

输出描述

输出一行包含一个数字,表示最大优美度。

示例1

输入:

2 -3 -2 3 7 -6 -6 -7 9 -5 -9 -3 -2 1 4 -9 -1 -10 -5 -5 -10 -4 8 2

输出:

8281

原站题解

C 解法, 执行用时: 2ms, 内存消耗: 364KB, 提交时间: 2020-08-21

#include <stdio.h>
#include <string.h>

typedef void (*operator)(int* c);

int calc(const int* cube) {
    int res;
    
    res = cube[0] * cube[1] * cube[2] * cube[3] + cube[4] * cube[5] * cube[10] * cube[11] +
        cube[6] * cube[7] * cube[12] * cube[13] + cube[8] * cube[9] * cube[14] * cube[15] + 
        cube[16] * cube[17] * cube[18] * cube[19] + cube[20] * cube[21] * cube[22] * cube[23];
    
    return res;
}

int max(int a, int b) {
    return a > b ? a : b;
}

void upLeft(int* c) {
    int c0 = c[0], c4 = c[4], c5 = c[5];
    c[0] = c[2];    c[2] = c[3];    c[3] = c[1];    c[1] = c0;
    c[4] = c[6];    c[5] = c[7];    c[6] = c[8];    c[7] = c[9];
    c[8] = c[23];    c[9] = c[22];    c[23] = c4;    c[22] = c5;
}

void upRight(int* c) {
    upLeft(c);    upLeft(c);    upLeft(c);
}

void rightUp(int* c) {
    int c8 = c[8], c1 = c[1], c3 = c[3];
    c[8] = c[14];    c[14] = c[15];    c[15] = c[9];    c[9] = c8;
    c[1] = c[7];    c[3] = c[13];    c[7] = c[17];    c[13] = c[19];
    c[17] = c[21];    c[19] = c[23];    c[21] = c1;    c[23] = c3;
}

void rightDown(int* c) {
    rightUp(c);    rightUp(c);    rightUp(c);
}

void frontRight(int* c) {
    int c6 = c[6], c2 = c[2], c3 = c[3];
    c[6] = c[12];    c[12] = c[13];    c[13] = c[7];    c[7] = c6;
    c[2] = c[11];    c[3] = c[5];    c[11] = c[17];    c[5] = c[16];
    c[16] = c[14];    c[17] = c[8];    c[14] = c3;    c[8] = c2;
}

void frontLeft(int* c) {
    frontRight(c);    frontRight(c);    frontRight(c);
}

operator op[6] = {&upLeft, &upRight, &rightUp, &rightDown, &frontRight, &frontLeft};

int res = 0;

void Solver(int* c, int opNum) {
    int temp[24], i;
    
    if (opNum > 5) {
        return;
    }
    
    for (i = 0; i < 6; i++) {
        memcpy(temp, c, 24 * sizeof(int));
        op[i](temp);
        res = max(res, calc(temp));
        Solver(temp, opNum + 1);
    }
}

int main() {
    int cube[24], i;
    
    for (i = 0; i < 24; i++) {
        scanf("%d", &cube[i]);
    }
    res = calc(cube);
    
    Solver(cube, 1);
    
    printf("%d", res);
    
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-07-22

#include <stdio.h>
#include "string.h"
#define LEN 24
#define INF 2147483647;
int s[LEN], ms = -INF;
  
int beautyValue(){
    return s[0] * s[1] * s[2] * s[3] +
           s[4] * s[5] * s[10] * s[11] +
           s[6] * s[7] * s[12] * s[13] +
           s[8] * s[9] * s[14] * s[15] +
           s[16] * s[17] * s[18] * s[19] +
           s[20] * s[21] * s[22] * s[23];
}
  
void upLeft(){
    int s6 = s[6] , s7 = s[7] , s0 = s[0];
    s[6] = s[8]; s[7] = s[9]; s[8] = s[23]; s[9] = s[22]; s[23] = s[4]; s[22] = s[5]; s[4] = s6; s[5] = s7; s[0] = s[2]; s[2] = s[3]; s[3] = s[1]; s[1] = s0;
}
  
void upRight(){
    upLeft();upLeft();upLeft();
}
//
//void downLeft(){
//    int s12 = s[12] , s13 = s[13] , s16 = s[16];
//    s[12] = s[14]; s[13] = s[15]; s[14] = s[21]; s[15] = s[20]; s[21] = s[10]; s[20] = s[11]; s[10] = s12; s[11] = s13; s[16] = s[17]; s[17] = s[19]; s[19] = s[18]; s[18] = s16;
//}
//
//void downRight(){
//    downLeft(); downLeft(); downLeft();
//}
  
void leftUp(){
    int s6 = s[6] , s12 = s[12] , s4 = s[4];
    s[6] = s[16]; s[12] = s[18]; s[16] = s[20]; s[18] = s[22]; s[20] = s[0]; s[22] = s[2]; s[0] = s6; s[2] = s12; s[4] = s[5]; s[5] = s[11]; s[11] = s[10]; s[10] = s4;
}
  
void leftDown(){
    leftUp();leftUp();leftUp();
}

void headLeft(){
    int s2=s[2] , s3 = s[3] , s6 = s[6];
    s[2] = s[8]; s[3] = s[14]; s[8] = s[17]; s[14] = s[16]; s[17] = s[11]; s[16] = s[5]; s[11] = s2; s[5] = s3; s[6] = s[7]; s[7] = s[13]; s[13] = s[12]; s[12] = s6;
}
  
void headRight(){
    headLeft();headLeft();headLeft();
}
  
void (*pf[6])() = {&upLeft,&upRight,&leftUp,&leftDown,&headLeft,&headRight};
  
void transform(int deep){
    int i , bv;
    if(deep > 5){
        return ;
    }
    for (i = 0; i < 6; ++i) {
        pf[i]();
        bv = beautyValue();
        ms = bv > ms ? bv : ms ;
        transform( deep + 1 );
        pf[ i^1 ]();
    }
}
  
int main(){
    int i ;
    ms = beautyValue();
    for (int i = 0; i <24 ; ++i) {
        scanf("%d" ,&s[i]);
    }
    transform(1);
    printf("%d\n" , ms);
}

C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2019-04-14

#include <stdio.h>
#include "string.h"
 
#define LEN 24
#define INF 2147483647;
int s[LEN] , ms = -INF;
 
int beautyValue(){
    return s[0] * s[1] * s[2] * s[3] +
           s[4] * s[5] * s[10] * s[11] +
           s[6] * s[7] * s[12] * s[13] +
           s[8] * s[9] * s[14] * s[15] +
           s[16] * s[17] * s[18] * s[19] +
           s[20] * s[21] * s[22] * s[23];
}
 
void upLeft(){
    int s6 = s[6] , s7 = s[7] , s0 = s[0];
    s[6] = s[8]; s[7] = s[9]; s[8] = s[23]; s[9] = s[22]; s[23] = s[4]; s[22] = s[5]; s[4] = s6; s[5] = s7; s[0] = s[2]; s[2] = s[3]; s[3] = s[1]; s[1] = s0;
}
 
void upRight(){
    upLeft();upLeft();upLeft();
}
//
//void downLeft(){
//    int s12 = s[12] , s13 = s[13] , s16 = s[16];
//    s[12] = s[14]; s[13] = s[15]; s[14] = s[21]; s[15] = s[20]; s[21] = s[10]; s[20] = s[11]; s[10] = s12; s[11] = s13; s[16] = s[17]; s[17] = s[19]; s[19] = s[18]; s[18] = s16;
//}
//
//void downRight(){
//    downLeft(); downLeft(); downLeft();
//}
 
void leftUp(){
    int s6 = s[6] , s12 = s[12] , s4 = s[4];
    s[6] = s[16]; s[12] = s[18]; s[16] = s[20]; s[18] = s[22]; s[20] = s[0]; s[22] = s[2]; s[0] = s6; s[2] = s12; s[4] = s[5]; s[5] = s[11]; s[11] = s[10]; s[10] = s4;
}
 
void leftDown(){
    leftUp();leftUp();leftUp();
}
//
//void rightUp(){
//    int s7 = s[7] , s13 = s[13] , s8 = s[8]; s[7] = s[17] ; s[13] = s[19]; s[17] = s[21]; s[19] = s[23]; s[21] = s[1]; s[23] = s[3]; s[1] = s7; s[3] = s13; s[8] = s[14]; s[14] = s[15]; s[15] = s[9]; s[9] = s8;
//}
//
//void rightDown(){
//    rightUp();rightUp();rightUp();
//}
 
void headLeft(){
    int s2=s[2] , s3 = s[3] , s6 = s[6];
    s[2] = s[8]; s[3] = s[14]; s[8] = s[17]; s[14] = s[16]; s[17] = s[11]; s[16] = s[5]; s[11] = s2; s[5] = s3; s[6] = s[7]; s[7] = s[13]; s[13] = s[12]; s[12] = s6;
}
 
void headRight(){
    headLeft();headLeft();headLeft();
}
 
void (*pf[6])() = {&upLeft,&upRight,&leftUp,&leftDown,&headLeft,&headRight};
 
void transform(int deep){
    int i , bv;
    if(deep > 5){
        return ;
    }
    for (i = 0; i < 6; ++i) {
        pf[i]();
        bv = beautyValue();
        ms = bv > ms ? bv : ms ;
        transform( deep + 1 );
        pf[ i^1 ]();
    }
}
 
int main(){
    int i ;
    ms = beautyValue();
    for (int i = 0; i <24 ; ++i) {
        scanf("%d" ,&s[i]);
    }
    transform(1);
    printf("%d\n" , ms );
}

C 解法, 执行用时: 2ms, 内存消耗: 380KB, 提交时间: 2020-08-11

#include <stdio.h>
#define max(a, b) ((a) > (b)) ? (a) : (b)
int get_score(int v[])
{
    return v[0]*v[1]*v[2]*v[3] + v[4]*v[5]*v[10]*v[11]
        + v[6]*v[7]*v[12]*v[13] + v[8]*v[9]*v[14]*v[15]
        + v[16]*v[17]*v[18]*v[19] + v[20]*v[21]*v[22]*v[23];
}
int solve(int v[], int deep)
{
    if(deep == 0)
        return get_score(v);
    int beauti_score = get_score(v);
    int v0[24] = {v[0], v[1], v[8], v[14], v[4], v[3], v[7], v[13],
                  v[17], v[9], v[10], v[2], v[6], v[12], v[16], v[15],
                  v[5], v[11], v[18], v[19], v[20], v[21], v[22], v[23] };
    int v1[24] = {v[0], v[1], v[11], v[5], v[4], v[16], v[12], v[6],
                  v[2], v[9], v[10], v[17], v[13], v[7], v[3], v[15],
                  v[14], v[8], v[18], v[19], v[20], v[21], v[22], v[23] };
    int v2[24] = {v[6], v[1], v[12], v[3], v[5], v[11], v[16], v[7],
                  v[8], v[9], v[4], v[10], v[18], v[13], v[14], v[15],
                  v[20], v[17], v[22], v[19], v[0], v[21], v[2], v[23] };
    int v3[24] = {v[20], v[1], v[22], v[3], v[10], v[4], v[0], v[7],
                  v[8], v[9], v[11], v[5], v[2], v[13], v[14], v[15],
                  v[6], v[17], v[12], v[19], v[16], v[21], v[18], v[23] };
    int v4[24] = {v[1], v[3], v[0], v[2], v[23], v[22], v[4], v[5],
                  v[6], v[7], v[10], v[11], v[12], v[13], v[14], v[15],
                  v[16], v[17], v[18], v[19], v[20], v[21], v[9], v[8] };
    int v5[24] = {v[2], v[0], v[3], v[1], v[6], v[7], v[8], v[9],
                  v[23], v[22], v[10], v[11], v[12], v[13], v[14], v[15],
                  v[16], v[17], v[18], v[19], v[20], v[21], v[5], v[4] };
    int *vx[6] = {v0, v1, v2, v3, v4, v5};
    for(int i = 0; i < 6; i++)
    {
        beauti_score = max(beauti_score, solve(vx[i], deep - 1));
    }
    return beauti_score;
}
int main()
{
    int v[24];
    for(int i = 0; i < 24; i++)
    {
        scanf("%d", &v[i]);
    }
    int beauti_score = solve(v, 5);
    printf("%d", beauti_score);
    return 0;
}

C++14 解法, 执行用时: 2ms, 内存消耗: 476KB, 提交时间: 2019-08-26

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 6
struct Node{
    int up[N], down[N], fnt[N], bck[N], left[N], right[N];
};
Node sta;
int ans;
int sum[10];
int cal(const Node &no)
{
    for (int i = 0; i < 6; i++)sum[i] = 1;
    for (int i = 0; i < 4; i++){
        sum[0] *= no.up[i];
        sum[1] *= no.down[i];
        sum[2] *= no.fnt[i];
        sum[3] *= no.bck[i];
        sum[4] *= no.left[i];
        sum[5] *= no.right[i];
    }
    for (int i = 1; i < 6; i++)sum[0] += sum[i];
    return sum[0];
}
  
  
void rota(Node &no, int dir)
{
    if (dir == 0){
        int tmp = no.up[0];
        no.up[0] = no.up[1];
        no.up[1] = no.up[3];
        no.up[3] = no.up[2];
        no.up[2] = tmp;
        int t0 = no.fnt[0], t1 = no.fnt[1];
        no.fnt[0] = no.left[0], no.fnt[1] = no.left[1];
        no.left[0] = no.bck[0], no.left[1] = no.bck[1];
        no.bck[0] = no.right[0], no.bck[1] = no.right[1];
        no.right[0] = t0, no.right[1] = t1;
    }
    else if (dir == 1){
        int tmp = no.up[0];
        no.up[0] = no.up[2];
        no.up[2] = no.up[3];
        no.up[3] = no.up[1];
        no.up[1] = tmp;
        int t0 = no.fnt[0], t1 = no.fnt[1];
        no.fnt[0] = no.right[0], no.fnt[1] = no.right[1];
        no.right[0] = no.bck[0], no.right[1] = no.bck[1];
        no.bck[0] = no.left[0], no.bck[1] = no.left[1];
        no.left[0] = t0, no.left[1] = t1;
    }
    else if (dir == 2){
        int tmp = no.right[0];
        no.right[0] = no.right[2];
        no.right[2] = no.right[3];
        no.right[3] = no.right[1];
        no.right[1] = tmp;
        int t0 = no.fnt[1], t1 = no.fnt[3];
        no.fnt[1] = no.down[1], no.fnt[3] = no.down[3];
        no.down[1] = no.bck[2], no.down[3] = no.bck[0];  ///
        no.bck[2] = no.up[1], no.bck[0] = no.up[3];
        no.up[1] = t0, no.up[3] = t1;
    }
    else if (dir == 3){
        int tmp = no.right[0];
        no.right[0] = no.right[1];
        no.right[1] = no.right[3];
        no.right[3] = no.right[2];
        no.right[2] = tmp;
        int t0 = no.fnt[1], t1 = no.fnt[3];
        no.fnt[1] = no.up[1], no.fnt[3] = no.up[3];
        no.up[1] = no.bck[2], no.up[3] = no.bck[0];
        no.bck[2] = no.down[1], no.bck[0] = no.down[3];
        no.down[1] = t0, no.down[3] = t1;
    }
    else if (dir == 4){
        int tmp = no.fnt[0];
        no.fnt[0] = no.fnt[2];
        no.fnt[2] = no.fnt[3];
        no.fnt[3] = no.fnt[1];
        no.fnt[1] = tmp;
        int t0 = no.up[2], t1 = no.up[3];
        no.up[2] = no.left[3], no.up[3] = no.left[1];
        no.left[3] = no.down[1], no.left[1] = no.down[0];  ///
        no.down[1] = no.right[0], no.down[0] = no.right[2];
        no.right[0] = t0, no.right[2] = t1;
    }
    else if (dir == 5){
        int tmp = no.fnt[0];
        no.fnt[0] = no.fnt[1];
        no.fnt[1] = no.fnt[3];
        no.fnt[3] = no.fnt[2];
        no.fnt[2] = tmp;
        int t0 = no.up[2], t1 = no.up[3];
        no.up[2] = no.right[0], no.up[3] = no.right[2];
        no.right[0] = no.down[1], no.right[2] = no.down[0];  ///
        no.down[1] = no.left[3], no.down[0] = no.left[1];
        no.left[3] = t0, no.left[1] = t1;
    }
}
  
int debug[10], top = 0;
void dfs(Node no, int lay)
{
    ans = max(ans, cal(no));
    if (cal(no) == 8){
//        for (int i = 0 ; i < top; i++){
//            printf("%d ", debug[i]);
//        }
//        printf("\n");
    }
    if (lay == 6){
        return;
    }
    for (int i = 0; i < 6; i++){
        Node nt = no;
        debug[top++] = i;
        if (top == 4 && debug[0] == 1 && debug[1] == 3 && debug[2] == 3 && debug[3] == 2){
            int a = 6;
        }
        rota(nt, i );
        dfs(nt, lay + 1);
        top--;
    }
}
  
int main()
{
    int a[30];
    for (int i = 0; i < 24; i++){
        scanf("%d", &a[i]);
    }
    sta.bck[0] = a[1];
    sta.bck[1] = a[0];
    sta.bck[2] = a[3];
    sta.bck[3] = a[2];
  
    sta.down[0] = a[12];
    sta.down[1] = a[13];
    sta.down[2] = a[6];
    sta.down[3] = a[7];
  
    sta.fnt[0] = a[18];
    sta.fnt[1] = a[19];
    sta.fnt[2] = a[16];
    sta.fnt[3] = a[17];
  
    sta.up[0] = a[22];
    sta.up[1] = a[23];
    sta.up[2] = a[20];
    sta.up[3] = a[21];
  
    sta.left[0] = a[4];
    sta.left[1] = a[10];
    sta.left[2] = a[5];
    sta.left[3] = a[11];
  
    sta.right[0] = a[15];
    sta.right[1] = a[9];
    sta.right[2] = a[14];
    sta.right[3] = a[8];
    ans = cal(sta);
    dfs(sta, 1);
    printf("%d\n", ans);
    return 0;
}

上一题