字节跳动2018笔试附加题,二阶数字魔方

在这里插入图片描述
在这里插入图片描述

魔方的旋转方式大致可分为三个(顺时针可以通过对应逆时针旋转三次实现),右面(Y)逆时针,后面(X)逆时针,上面(Z)逆时针,封装成三个函数,分析发现,所有的旋转可以归结为一圈八个块的位置轮转(函数zheng)和一个侧面四个小块的位置轮换(函数ce)。
不超过5次的操作,可以通过一个类似深度优先遍历操作实现(这里会有很多重复,暂时没有优化)

#include
#include
using namespace std;#define DEPTH 5class moFang{private:vector<int> list;vector<vector<int>> pos = {{0,1,2,3},        //up 0{4,5,10,11},        //left 1{6,7,12,13},        //before 2{8,9,14,15},        //right 3{16,17,18,19},        //down 4{20,21,22,23}        //behind 5};void zheng(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8){int temp1 = list[p7], temp2 = list[p8];list[p7] = list[p5]; list[p8] = list[p6];list[p5] = list[p3]; list[p6] = list[p4];list[p3] = list[p1]; list[p4] = list[p2];list[p1] = temp1; list[p2] = temp2;}void ce(int p1, int p2, int p3, int p4){int temp = list[p4];list[p4] = list[p3];list[p3] = list[p2];list[p2] = list[p1];list[p1] = temp;}public:void add(int n){ list.push_back(n); }void turnY(){zheng(pos[0][1], pos[0][3], pos[2][1], pos[2][3], pos[4][1], pos[4][3], pos[5][1], pos[5][3]);ce(pos[3][0], pos[3][2], pos[3][3], pos[3][1]);}void turnX(){zheng(pos[0][0], pos[0][1], pos[3][1], pos[3][3], pos[4][3], pos[4][2], pos[1][2], pos[1][0]);ce(pos[5][3], pos[5][1], pos[5][0], pos[5][2]);}void turnZ(){zheng(pos[1][0], pos[1][1], pos[2][0], pos[2][1], pos[3][0], pos[3][1], pos[5][3], pos[5][2]);ce(pos[0][2], pos[0][3], pos[0][1], pos[0][0]);}int getSum(){int sum = 0, tempsum = 1;for(int i=0;i<6;i++){for(int j=0;j<4;j++){tempsum *= list[pos[i][j]];}sum += tempsum;tempsum = 1;}return sum;}int getMax(int dep){int max = getSum(), temp=0;turnY();temp = getSum();if(temp>max) max=temp;if(dep<DEPTH) {temp = getMax(dep+1);if(temp>max) max=temp;}turnY();turnY();temp = getSum();if(temp>max) max=temp;if(dep<DEPTH) {temp = getMax(dep+1);if(temp>max) max=temp;}turnY();turnX();temp = getSum();if(temp>max) max=temp;if(dep<DEPTH) {temp = getMax(dep+1);if(temp>max) max=temp;}turnX();turnX();temp = getSum();if(temp>max) max=temp;if(dep<DEPTH) {temp = getMax(dep+1);if(temp>max) max=temp;}turnX();turnZ();temp = getSum();if(temp>max) max=temp;if(dep<DEPTH) {temp = getMax(dep+1);if(temp>max) max=temp;}turnZ();turnZ();temp = getSum();if(temp>max) max=temp;if(dep<DEPTH) {temp = getMax(dep+1);if(temp>max) max=temp;}turnZ();return max;}
};int main(){moFang m;int n;for(int i=0;i<24;i++){cin>>n;m.add(n);}cout << m.getMax(1) << endl;return 0;
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部