Dice

 

Dice

http://acm.hdu.edu.cn/showproblem.php?pid=5012

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


Problem Description
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a 1.a 2,a 3,a 4,a 5,a 6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b 1.b 2,b 3,b 4,b 5,b 6 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 a i ≠ a j and b i ≠ b j 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, a i ≠ b i). Ddy wants to make the two dices look the same from all directions(which means for all i, a i = b i) 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 a 1,a 2,a 3,a 4,a 5,a 6, representing the numbers on dice A. 

The second line consists of six integers b 1,b 2,b 3,b 4,b 5,b 6, 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
 
模拟搜索水题

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<string>
  6 #include<cstdio>
  7 #include<queue>
  8 #include<map>
  9 #include<vector>
 10 typedef long long ll;
 11 #define maxn 100005
 12 using namespace std;
 13 
 14 int a[15],b[15];
 15 map<int,int>mp;
 16 struct sair{
 17     int num,step;
 18 };
 19 
 20 sair Roll(sair tmp,int x){
 21     //左右前后
 22     sair ans;
 23     ans.num=0;
 24     int aa[8];
 25     int xx=tmp.num;
 26     for(int i=6;i>=1;i--){
 27         a[i]=xx%10;
 28         xx/=10;
 29     }
 30     if(x==0){
 31         ans.num=ans.num*10+a[4];
 32         ans.num=ans.num*10+a[3];
 33         ans.num=ans.num*10+a[1];
 34         ans.num=ans.num*10+a[2];
 35         ans.num=ans.num*10+a[5];
 36         ans.num=ans.num*10+a[6];
 37     }
 38     else if(x==1){
 39         ans.num=ans.num*10+a[3];
 40         ans.num=ans.num*10+a[4];
 41         ans.num=ans.num*10+a[2];
 42         ans.num=ans.num*10+a[1];
 43         ans.num=ans.num*10+a[5];
 44         ans.num=ans.num*10+a[6];
 45     }
 46     else if(x==2){
 47         ans.num=ans.num*10+a[6];
 48         ans.num=ans.num*10+a[5];
 49         ans.num=ans.num*10+a[3];
 50         ans.num=ans.num*10+a[4];
 51         ans.num=ans.num*10+a[1];
 52         ans.num=ans.num*10+a[2];
 53     }
 54     else{
 55         ans.num=ans.num*10+a[5];
 56         ans.num=ans.num*10+a[6];
 57         ans.num=ans.num*10+a[3];
 58         ans.num=ans.num*10+a[4];
 59         ans.num=ans.num*10+a[2];
 60         ans.num=ans.num*10+a[1];
 61     }
 62     ans.step=tmp.step+1;
 63     return ans;
 64 }
 65 
 66 void bfs(int S,int E){
 67     sair s,e;
 68     if(S==E){
 69         cout<<0<<endl;
 70         return;
 71     }
 72     s.num=S,s.step=0;
 73     queue<sair>Q;
 74     Q.push(s);
 75     while(!Q.empty()){
 76         s=Q.front();
 77         Q.pop();
 78         for(int i=0;i<4;i++){
 79             e=Roll(s,i);
 80             if(e.num==E){
 81                 cout<<e.step<<endl;
 82                 return;
 83             }
 84             if(!mp[e.num]){
 85                 Q.push(e);
 86                 mp[e.num]=1;
 87             }
 88         }
 89     }
 90     cout<<-1<<endl;
 91 }
 92 
 93 int main(){
 94     while(cin>>a[1]){
 95         mp.clear();
 96         for(int i=2;i<=6;i++) cin>>a[i];
 97         int S=0,E=0;
 98         for(int i=1;i<=6;i++) E=E*10+a[i];
 99         for(int i=1;i<=6;i++) cin>>b[i];
100         for(int i=1;i<=6;i++) S=S*10+b[i];
101         bfs(S,E);
102     }
103 }
View Code

猜你喜欢

转载自www.cnblogs.com/Fighting-sh/p/9943043.html