题目链接: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)
Total Submission(s): 882 Accepted Submission(s): 271
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
Source
2013 Asia Changsha Regional Contest
解题:
转魔方,注意对应位置别弄错就好,深搜,广搜都可以。就是比较难调。
总结:
一些重复性代码,最好写成函数调用,减少代码的冗余性,同时提高代码的准确性。
代码:
#include <iostream> #include <cstdio> using namespace std; int cube[24],tmp,a,b,c,ans,n; int check() { int res=0; tmp=cube[0]; if(tmp==cube[1]&&tmp==cube[2]&&tmp==cube[3])res++; tmp=cube[4]; if(tmp==cube[5]&&tmp==cube[10]&&tmp==cube[11])res++; tmp=cube[6]; if(tmp==cube[7]&&tmp==cube[12]&&tmp==cube[13])res++; tmp=cube[8]; if(tmp==cube[9]&&tmp==cube[14]&&tmp==cube[15])res++; tmp=cube[16]; if(tmp==cube[17]&&tmp==cube[18]&&tmp==cube[19])res++; tmp=cube[20]; if(tmp==cube[21]&&tmp==cube[22]&&tmp==cube[23])res++; return res; } void rotate(int oper) { if(oper==1) { a=cube[1]; b=cube[3]; c=cube[9]; cube[1]=cube[7]; cube[3]=cube[13]; cube[7]=cube[17]; cube[13]=cube[19]; cube[17]=cube[21]; cube[19]=cube[23]; cube[21]=a; cube[23]=b; cube[9]=cube[8]; cube[8]=cube[14]; cube[14]=cube[15]; cube[15]=c; } else if(oper==6) { a=cube[7]; b=cube[13]; c=cube[14]; cube[7]=cube[1]; cube[13]=cube[3]; cube[1]=cube[21]; cube[3]=cube[23]; cube[21]=cube[17]; cube[23]=cube[19]; cube[17]=a; cube[19]=b; cube[14]=cube[8]; cube[8]=cube[9]; cube[9]=cube[15]; cube[15]=c; } else if(oper==2) { a=cube[8]; b=cube[14]; c=cube[7]; cube[8]=cube[17]; cube[14]=cube[16]; cube[17]=cube[11]; cube[16]=cube[5]; cube[11]=cube[2]; cube[5]=cube[3]; cube[2]=a; cube[3]=b; cube[7]=cube[13]; cube[13]=cube[12]; cube[12]=cube[6]; cube[6]=c; } else if(oper==5) { a=cube[16]; b=cube[17]; c=cube[12]; cube[17]=cube[8]; cube[16]=cube[14]; cube[8]=cube[2]; cube[14]=cube[3]; cube[2]=cube[11]; cube[3]=cube[5]; cube[11]=b; cube[5]=a; cube[12]=cube[13]; cube[13]=cube[7]; cube[7]=cube[6]; cube[6]=c; } else if(oper==3) { a=cube[12]; b=cube[13]; c=cube[16]; cube[12]=cube[14]; cube[13]=cube[15]; cube[14]=cube[21]; cube[15]=cube[20]; cube[21]=cube[10]; cube[20]=cube[11]; cube[10]=a; cube[11]=b; cube[16]=cube[17]; cube[17]=cube[19]; cube[19]=cube[18]; cube[18]=c; } else { a=cube[12]; b=cube[13]; c=cube[16]; cube[12]=cube[10]; cube[13]=cube[11]; cube[10]=cube[21]; cube[11]=cube[20]; cube[20]=cube[15]; cube[21]=cube[14]; cube[14]=a; cube[15]=b; cube[16]=cube[18]; cube[18]=cube[19]; cube[19]=cube[17]; cube[17]=c; } } void search(int oper,int step) { rotate(oper); tmp=check(); if(tmp>ans)ans=tmp; if(step==n) { rotate(7-oper); return; } for(int i=1;i<=6;i++) { if(i!=(7-oper)) { search(i,step+1); } } rotate(7-oper); } int main() { while(~scanf("%d",&n)) { for(int i=0;i<24;i++) scanf("%d",&cube[i]); ans=check(); for(int i=1;i<=6;i++) search(i,1); printf("%d\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。