头条笔试题,转魔方
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<map>
#include<queue>
#include<vector>
#include<math.h>
using namespace std;
typedef long long lld;
const int N=1e5+10;
const int MOD = 1e9+7;
/*
顶面转动时数组变化
*/
int U1[4] ={0, 1, 3, 2};//arr[0]=arr[1] arr[1]=arr[3] arr[3]=arr[2] arr[2] = arr[0] 下同
int U2[4] = {23, 8, 6, 4};//{22, 23, 9 ,8,7,6,5,4};
int U3[4] = {22, 9, 7, 5};
//右面转动时变化
int R1[4] = {8,9,15,14};
int R2[4] = {21,17,7,1};//{23, 21,19,17,13,7,3,1};
int R3[4] = {23, 19,13,3};
//左面
int L1[4]={5,4,10,11};
int L2[4]={20,16, 6,0};//{22,20,18,16,12, 6,2,0};
int L3[4] = {22,18,12, 2};
//前面
int F1[4]={6,7,13,12};
int F2[4] = {3,14,16, 5};//{2,3,8,14,17,16, 11, 5};
int F3[4] = {2,8,17, 11};
//后面
int B1[4]={22, 23, 21, 20};
int B2[4]={1, 15, 18, 4};//{0, 1, 9, 15, 19, 18, 10, 4};
int B3[4] ={0, 9, 19, 10};
//底面,未用到,只为了计算结果
int D1[4] = {16,17, 18, 19};
lld arr[30];
//单个变化函数
void change(int d[])
{
int s=4;
lld a = arr[d[0]];
for(int i=0;i<s-1;++i)
arr[d[i]]=arr[d[i+1]];
arr[d[s-1]]=a;
}
//计算某面的结果
lld calone(int d[])
{
lld sum=1;
for(int i=0;i<4;++i) sum*=arr[d[i]];
return sum;
}
//六面结果相加
lld cal()
{
lld sum=0;
sum += calone(U1) + calone(R1) + calone(L1) + calone(F1)+ calone(B1) + calone(D1);
return sum;
}
lld ans =0;
//对某一面进行转动
void changeone(int d1[], int d2[], int d3[], int t)
{
while(t--){
change(d1);
change(d2);
change(d3);
}
}
void dfs(int);
void changeup(int cnt)
{
changeone(U1, U2, U3, 1);//顶面转动一次
dfs(cnt+1);
changeone(U1, U2, U3, 2);//转两次相当于原来一次逆转
dfs(cnt+1);
changeone(U1, U2, U3, 1);//再转一次还原了
}
void dfs(int cnt)
{
lld t=cal();
ans = max(t, ans);
if(cnt==5)return;
changeup(cnt);
//return;
//转动整个魔方,把每一个面转到朝上,然后在顶面进行90度转单面转动操作
//X轴
for(int i=0;i<4;++i)
{
changeone(L1, L2, L3, 1);
changeone(R1, R2, R3, 1);
changeup(cnt);
}
//Y轴
for(int i=0;i<4;++i)
{
changeone(B1, B2, B3, 1);
changeone(F1, F2, F3, 1);
changeup(cnt);
}
}
void solve()
{
int n=24, x;
for(int i=0;i<n;++i)
{
scanf("%lld", arr+i);
}
ans = cal();
dfs(0);
cout<<ans<<endl;
}
/*
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
*/
int main()
{
//cout<<(500&499)<<endl;
solve();
return 0;
}