HDU 4801 Pocket Cube (simulation problem--turn Rubik's Cube)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4801


Surface:

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 Descriptionpocket Cube is a combination puzzle. It is a 2x2x2 cube, which means it was constructed by 8 Mini-cubes. For a combination of 2×2 mini-cubes which sharing a whole cube face, can twist it all degrees in clockwise or counter Clockwise direction, this twist operation is called one twist step.
Considering all faces of Mini-cubes, there'll be totally for faces painted in 6 different colors (Indexed from 0), and th Ere would be is 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 a color arrangement of all faces from a scrambled Pocket Cube, please tell us the maximum possible numb Er of completed faces in no more than N twist steps.
Index of each face is shown as below:



Inputthere'll be several test cases. In each test case, there'll be 2 lines. One integer N (1≤n≤7) in the first line, then integers Ci separated by a single space on the second line. For index 0≤i <, Ci was color of the corresponding face. We guarantee that the color arrangement are a valid state which can being achieved by doing a finite number of twist steps fro M an initial cube whose all 6 large cube faces is completed faces.
Outputfor Each test case, please output the maximum number of completed faces during no more than N twist step (s).
Sample Input
10 0 0 0 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 510 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
62

Source2013 Asia Changsha Regional Contest


Solving:

Turn the Rubik's Cube, pay attention to the corresponding position do not mistake, deep search, wide search can be. is more difficult to adjust.

Summarize:

Some repetitive code, preferably written as function calls, reduces the redundancy of the code while improving the accuracy of the code.

Code:

#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] &AMP;&AMP;TMP==CUBE[10]&AMP;&AMP;TMP==CUBE[11]) res++;tmp=cube[6];if (tmp==cube[7]&&tmp==cube[12]&    &AMP;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);p rintf ("%d\n", ans);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4801 Pocket Cube (simulation problem--turn Rubik's Cube)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.