zoj 3736 Pocket Cube(2013亞洲區域賽 長沙站 K)


http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3736    


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:


題意:就是一個2*2*2的魔方,問旋轉不超過N(1<=N<=7)次最多能還原多少個面。
思路:因爲最多隻旋轉7次,所以我們可以直接暴力搜索求最大,六個面順時針逆時針一遍一共12中轉法,不過因爲只有2*2*2,所以對於相對的兩個面,一個面順時針旋轉等價於另一個面逆時針轉,所以可以縮減爲6種旋轉。只要把六種旋轉的面的對應關係搞清楚就行了。可以打表,不過我沒打,我用函數寫的,順時針逆時針可以化爲一種旋轉,一種轉一次,一種轉3次,詳見代碼。

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
void up(int *a)
{
    int tmp=a[6];a[6]=a[7];a[7]=a[13];a[13]=a[12];a[12]=tmp;
    tmp=a[11];a[11]=a[2];a[2]=a[8];a[8]=a[17];a[17]=tmp;
    tmp=a[5];a[5]=a[3];a[3]=a[14];a[14]=a[16];a[16]=tmp;
}
void left(int *a)
{
    int tmp=a[11];a[11]=a[5];a[5]=a[4];a[4]=a[10];a[10]=tmp;
    tmp=a[0];a[0]=a[20];a[20]=a[16];a[16]=a[6];a[6]=tmp;
    tmp=a[2];a[2]=a[22];a[22]=a[18];a[18]=a[12];a[12]=tmp;
}
void front(int *a)
{
    int tmp=a[0];a[0]=a[2];a[2]=a[3];a[3]=a[1];a[1]=tmp;
    tmp=a[22];a[22]=a[5];a[5]=a[7];a[7]=a[9];a[9]=tmp;
    tmp=a[23];a[23]=a[4];a[4]=a[6];a[6]=a[8];a[8]=tmp;
}
int check(int a,int b,int c,int d)
{
    if(a==b&&a==c&&a==d)
    return 1;
    return 0;
}
int ans;
int getnum(int *a)
{
    int num=0;
    num+=check(a[0],a[1],a[2],a[3]);
    num+=check(a[4],a[5],a[10],a[11]);
    num+=check(a[6],a[7],a[12],a[13]);
    num+=check(a[8],a[9],a[14],a[15]);
    num+=check(a[16],a[17],a[18],a[19]);
    num+=check(a[20],a[21],a[22],a[23]);
    return num;
}
void (*(func)[3])(int *);
void dfs(int *a,int lef)
{
    ans=max(ans,getnum(a));
    if(ans==6)
    return;
    if(lef==0)
    return;
    int b[24];
    for(int i=0;i<3;i++)
    {
        memcpy(b,a,sizeof(int)*24);
        func[i](b);
        dfs(b,lef-1);
        func[i](b);func[i](b);
        dfs(b,lef-1);
    }
}
int main()
{
    //freopen("dd.txt","r",stdin);
    int n;
    func[0]=front;func[1]=left;func[2]=up;
    while(scanf("%d",&n)!=EOF)
    {
        int a[24];
        for(int i=0;i<24;i++)
        {
            scanf("%d",&a[i]);
        }

        ans=0;
        dfs(a,n);
        printf("%d\n",ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章