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