这是2013年长沙现场赛的一道题目,做法和八数码问题一样。
同样,这里也需要HASH判重,否则就是MLE或RE。这道题旋转起来后的结果不如八数码好计算,所以人工找规律并用const int数组保存起来,方便之后的旋转操作。
代码(C++):
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#define MAX 300000
#define MOD 30000
using namespace std;
typedef int Cube[24];
const int face[3][2]={{0,4},{16,6},{20,8}};
const int r[6][8]={{1,3,7,13,17,19,21,23},{23,21,19,17,13,7,3,1},{0,1,9,15,19,18,10,4},{4,10,18,19,15,9,1,0},{4,5,6,7,8,9,23,22},{22,23,9,8,7,6,5,4}};
const int rf[6][4]={{9,8,14,15},{8,9,15,14},{22,23,21,20},{22,20,21,23},{0,2,3,1},{0,1,3,2}};
struct Node{
int id;
int cf;
int d;
};
Cube cube[MAX];
int head[MOD],next[MAX];
int conut_face(int id)
{
int num,i,a,b;
num=0;
for(i=0;i<3;i++)
{
a=face[i][0];
b=face[i][1];
if(cube[id][a]==cube[id][a+1]&&cube[id][a]==cube[id][a+2]&&cube[id][a]==cube[id][a+3])
num++;
if(cube[id][b]==cube[id][b+1]&&cube[id][b]==cube[id][b+6]&&cube[id][b]==cube[id][b+7])
num++;
}
return num;
}
int twist(int id,int des,int k)
{
int i,a,b;
memcpy(cube[des],cube[id],sizeof(cube[id]));
for(i=0;i<8;i++)
{
a=r[k][i];
b=r[k][(i+2)%8];
cube[des][b]=cube[id][a];
}
for(i=0;i<4;i++)
{
a=rf[k][i];
b=rf[k][(i+1)%4];
cube[des][b]=cube[id][a];
}
return conut_face(des);
}
int hash(int id)
{
int i,num;
num=0;
for(i=0;i<9;i++)
{
num+=num*10+cube[id][i];
}
return num%MOD;
}
bool try_insert(int id)
{
int num,i;
num=hash(id);
for(i=head[num];i!=-1;i=next[i])
{
if(memcmp(cube[id],cube[i],sizeof(Cube))==0) return false;
}
next[id]=head[num];
head[num]=id;
return true;
}
int bfs(int n)
{
queue<Node> qn;
Node node,tmp;
int c,id,res,i;
tmp.id=0;
tmp.cf=res=conut_face(0);
tmp.d=0;
qn.push(tmp);
c=1;
while(!qn.empty())
{
node=qn.front();
qn.pop();
id=node.id;
for(i=0;i<6;i++)
{
tmp.cf=twist(id,c,i);
tmp.id=c;
tmp.d=node.d+1;
res=max(res,tmp.cf);
if(res==6) return res;
if(tmp.d<n&&try_insert(tmp.id))
{
c++;
qn.push(tmp);
}
}
}
return res;
}
int main()
{
//freopen("in.txt","r",stdin);
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<24;i++) scanf("%d",&cube[0][i]);
if(conut_face(0)==6) printf("6\n");
else{
memset(head,-1,sizeof(head));
printf("%d\n",bfs(n));
}
}
return 0;
}
题目( http://acm.hdu.edu.cn/showproblem.php?pid=4801):
Pocket Cube
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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:
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.
![](http://acm.hdu.edu.cn/data/images/4801-1.jpg)
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:
![](http://acm.hdu.edu.cn/data/images/4801-2.jpg)
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