转魔方

发布时间:2019年08月30日 阅读:301 次

https://blog.csdn.net/vocaloid01/article/details/94214217



二阶魔方又叫小魔方,是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
输出例子1:

8281


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int board[500];
long long Ans  = 0;
const long long inf = 1e18;
long long getsum(){
  long long ans =0;
  ans+=board[0]*board[1]*board[2]*board[3];
  ans+=board[4]*board[5]*board[10]*board[11];
  ans+=board[8]*board[9]*board[14]*board[15];
  ans+=board[6]*board[7]*board[12]*board[13];
  ans+=board[16]*board[17]*board[18]*board[19];
  ans+=board[20]*board[21]*board[22]*board[23];
//  ans+=board[12]*board[13]*board[14]*board[15];

  return ans;
}
void Swap(int a,int b,int c,int d){
//    s
    int t = board[a];
    board[a] = board[b];
    board[b] = board[c];
    board[c] = board[d];
    board[d] = t;
}
void Right(){
    Swap(6,8,23,4);
    Swap(7,9,22,5);
    Swap(2,3,1,0);
}
void Left(){
//    Swap(12,14,21,10);
//    Swap(13,15,20,11);
//    Swap(2,0,1,3);

    Swap(17,11,2,8);
    Swap(16,5,3,14);
    Swap(13,12,6,7);
}
void Up(){
    Swap(6,0,20,16);
    Swap(12,2,22,18);
    Swap(5,4,10,11);
}
void Down()
{
    Swap(7,1,21,17);
    Swap(13,3,23,19);
    Swap(8,9,15,14);
}
void Behind(){
    Swap(13,15,20,11);
    Swap(12,14,21,10);
    Swap(16,17,19,18);
}
void Front(){
    Swap(18,15,1,4);
    Swap(19,9,0,10);
    Swap(20,21,23,22);
}

void DFS(int step){

    long long sum = getsum();
    if(sum>Ans){
        Ans = sum;
    }
    if(step==5)return ;


    Right();
    DFS(step+1);
    Right();
    Right();
    DFS(step+1);
    Right();

    Left();
    DFS(step+1);
    Left();
    Left();
    DFS(step+1);
    Left();

    Up();
    DFS(step+1);
    Up();
    Up();
    DFS(step+1);
    Up();

    Down();
    DFS(step+1);
    Down();
   Down();
    DFS(step+1);
    Down();

    Front();
    DFS(step+1);
    Front();
    Front();
    DFS(step+1);
    Front();

    Behind();
    DFS(step+1);
    Behind();
    Behind();
    DFS(step+1);
    Behind();



}
int main()
{

    for(int i=0;i<24;i++)
        scanf("%d",&board[i]);
    Ans = -0x3f3f3f3f;
    DFS(0);
    cout<<Ans<<endl;
    return 0;
}



牛客网链接

Tag:
相关文章

发表评论: