题目链接
Problem Description
Pocket Cube is a 3-D combination puzzle. It is a 2 × 2 × 2 cube, which means it is constructed by 8 mini-cubes. For a combination of 2 × 2 mini-cubes which sharing a whole cube face, you can twist it 90 degrees in clockwise or counterclockwise direction, this twist operation is called one twist step.
Considering all faces of mini-cubes, there will be totally 24 faces painted in 6 different colors (Indexed from 0), and there will be exactly 4 faces painted in each kind of color. If 4 mini-cubes’ faces of same color rely on same large cube face, we can call the large cube face as a completed face.
Now giving you an color arrangement of all 24 faces from a scrambled Pocket Cube, please tell us the maximum possible number of completed faces in no more than N twist steps.
Index of each face is shown as below:

Input
There will be several test cases. In each test case, there will be 2 lines. One integer N (1 ≤ N ≤ 7) in the first line, then 24 integers Ci separated by a single space in the second line. For index 0 ≤ i < 24, Ci is color of the corresponding face. We guarantee that the color arrangement is a valid state which can be achieved by doing a finite number of twist steps from an initial cube whose all 6 large cube faces are completed faces.
Output
For each test case, please output the maximum number of completed faces during no more than N twist step(s).
Sample Input
1
0 0 0 0 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 5
1
0 4 0 4 1 1 2 5 3 3 1 1 2 5 3 3 4 0 4 0 5 2 5 2
Sample Output
6
2
题意
一个2阶魔方,给出初始状态和最大步数N,问在旋转N次及以内的情况下,最多能还原魔方的几个面。
题解
直接进行深搜枚举所有情况,然后求个最大值。魔方一个面有一种旋转方式(不考虑顺逆),六个面有六种,打表来记叙每一次旋转之后的魔方变化。
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int ans;
int mov[7][25]=
{
{6,1,12,3,5,11,16,7,8,9,4,10,18,13,14,15,20,17,22,19,0,21,2,23},
{20,1,22,3,10,4,0,7,8,9,11,5,2,13,14,15,6,17,12,19,16,21,18,23},
{1,3,0,2,23,22,4,5,6,7,10,11,12,13,14,15,16,17,18,19,20,21,9,8},
{2,0,3,1,6,7,8,9,23,22,10,11,12,13,14,15,16,17,18,19,20,21,5,4},
{0,1,8,14,4,3,7,13,17,9,10,2,6,12,16,15,5,11,18,19,20,21,22,23},
{0,1,11,5,4,16,12,6,2,9,10,17,13,7,3,15,14,8,18,19,20,21,22,23}
};
void judge(int *p)
{
int sum=0;
if (p[0]==p[1]&&p[1]==p[2]&& p[2]==p[3])
sum++;
if (p[4]==p[5]&&p[5]==p[10]&&p[10]==p[11])
sum++;
if (p[6]==p[7]&&p[7]==p[12]&&p[12]==p[13])
sum++;
if (p[8]==p[9]&&p[9]==p[14]&&p[14]==p[15])
sum++;
if (p[16]==p[17]&&p[17]==p[18]&&p[18]==p[19])
sum++;
if (p[20]==p[21]&&p[21]==p[22]&&p[22]==p[23])
sum++;
ans=max(ans,sum);
}
void dfs(int step,int *p)
{
judge(p);
if(step==0||ans==6)
return ;
int temp[30];
for(int i=0; i<6; i++)
{
for(int j=0; j<24; j++)
temp[j]=p[mov[i][j]];
dfs(step-1,temp);
}
return ;
}
int main()
{
int t;
while(~scanf("%d",&t))
{
ans=0;
int mag[30];
for(int i=0; i<24; i++)
scanf("%d",&mag[i]);
dfs(t,mag);
cout<<ans<<endl;
}
return 0;
}
本文介绍了一个2x2魔方的算法问题,旨在通过深度搜索算法找到在限定步数内能还原魔方的最大面数。文章提供了一段C++实现代码,通过预设的旋转步骤表来追踪每一步旋转后的魔方状态。

541

被折叠的 条评论
为什么被折叠?



