您的位置:首页 > 其它

The 2013 ACM-ICPC Asia Changsha Regional Contest K Pocket Cube

2013-11-24 19:10 549 查看
Pocket Cube
Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 seperated
by a sinle 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

Author: FAN, Yuzhe;CHEN, Cong;GUAN, Yao
Contest: The 2013 ACM-ICPC Asia Changsha Regional Contest
Submit    Status

题意:给定一个魔方的状态,问你旋转不超过n次,最多可以有几面是可以还原的。

n<=7数据量不大。可以用递归。暴搜。。

这道题最主要的还是打表操作。

对我的每一种状态的下一种可能有多少种转法。打表。。

可以用一个魔方想一想一共是6种可能。打表这种可能的下一种状态是什么。。

#include<cstdio>
#include<string>
int map[30];
int Max;
int dir[][24] =
{
{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,11,5,4,16,12,6,2,9,10,17,13,7,3,15,14,8,18,19,20,21,22,23},
{0,1,8,14,4,3,7,13,17,9,10,2,6,12,16,15,5,11,18,19,20,21,22,23},
};
int max(int a,int b)
{
return a>b?a:b;
}
int check(int var[]) //检查有多少个面符合状态
{
int num = 0;
if(var[0]==var[1]&&var[0]==var[2]&&var[0]==var[3])
num++;
if(var[4]==var[5]&&var[4]==var[10]&&var[4]==var[11])
num++;
if(var[6]==var[7]&&var[6]==var[12]&&var[6]==var[13])
num++;
if(var[8]==var[9]&&var[8]==var[14]&&var[8]==var[15])
num++;
if(var[16]==var[17]&&var[16]==var[18]&&var[16]==var[19])
num++;
if(var[20]==var[21]&&var[20]==var[22]&&var[20]==var[23])
num++;
return num;
}
void DFS(int map[],int n) //递归暴搜
{
Max = max(Max,check(map));
if(n<=0)
return ;
int var[30];
for(int i=0;i<6;i++)
{
for(int j=0;j<24;j++)
var[j] = map[dir[i][j]];
DFS(var,n-1);
}
}
int main()
{
int N;
while(~scanf("%d",&N))
{
Max = 0;
for(int i=0;i<24;i++)
scanf("%d",&map[i]);
DFS(map,N);
printf("%d\n",Max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模拟