hdu 5012__Dice

Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1457    Accepted Submission(s): 746


Problem Description
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6 to be numbers on specific faces of dice B. It’s guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while ai ≠ aj and bi ≠ bj for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7.

At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)


Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.
 

Input
There are multiple test cases. Please process till EOF. 

For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A. 

The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.
 

Output
For each test case, print a line with a number representing the answer. If there’s no way to make two dices exactly the same, output -1.
 

Sample Input
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 6 4 3 1 2 3 4 5 6 1 4 2 5 3 6
 

Sample Output
0 3 -1
 

Source
 
題意:給出一個骰子的起始態跟終態,問初始態能夠通過幾次的上下左右翻轉變換得到終態。得不出就輸出0.

想法:可以手寫推出當起始態爲123456的時候,向上下左右翻轉的結果分別爲:
上:653412
下:563421
左:431256
右:342156




這樣就確定了bfs遞推的方向。這裏我是使用STL裏的string類搞的。

代碼如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<string>
using namespace std;

string fun(int n,string a)
{
    int t;
    string b=a;
    if(n==1) {
        b[0]=a[5];
        b[1]=a[4];
        b[2]=a[2];
        b[3]=a[3];
        b[4]=a[0];
        b[5]=a[1];
    }
    else if(n==2) {
        b[0]=a[4];
        b[1]=a[5];
        b[2]=a[2];
        b[3]=a[3];
        b[4]=a[1];
        b[5]=a[0];
    }
    else if(n==3) {
        b[0]=a[3];
        b[1]=a[2];
        b[2]=a[0];
        b[3]=a[1];
        b[4]=a[4];
        b[5]=a[5];
    }
    else if(n==4) {
        b[0]=a[2];
        b[1]=a[3];
        b[2]=a[1];
        b[3]=a[0];
        b[4]=a[4];
        b[5]=a[5];
    }
    return b;
}
int main()
{
    string ch,ch1;
    string::iterator it;
    queue<string> q;
    map<string ,int> vis,path;  //path記錄走到這步需要多少步,vis記錄這種狀態是否已出現過
    string t,a;
    int i;
    char aa[10],bb[10];
    while(cin>>aa[0]) {     //輸入這裏開始使用的是scanf,發現即使使用%*c這樣的格式控制符還是會wa,所以這裏數據應該會有結尾有空格之類的情況。
        for(i=1;i<6;i++) {
            cin>>aa[i];
        }
        for(i=0;i<6;i++) {
            cin>>bb[i];
        }
        aa[6]='\0';bb[6]='\0';
        ch=aa;ch1=bb;
        if(ch==ch1) {   //特判兩種狀態開始就是一樣
            printf("0\n");
            continue;
        }
        while(!q.empty()) {
            q.pop();
        }
        q.push(ch);
        vis.clear();
        path[ch]=0;     
        int ans=-1;
        while(!q.empty()) {
            t=q.front();
            vis[t]=1;
            q.pop();
            for(i=1;i<=4;i++) {
                a=fun(i,t);
                if(a==ch1) {
                    ans=path[t]+1;
                    goto fff;       //使用goto的做法真是極醜的QAQ
                }
                if(vis[a]==0) {
                    path[a]=path[t]+1;
                    q.push(a);
                    vis[t]=1;
                }
            }
        }
        fff:
        if(ans!=-1)
            printf("%d\n",ans);
        else {
            printf("-1\n");
        }
    }
    return 0;
}


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