Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1457 Accepted Submission(s): 746
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.
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.
上:653412
下:563421
左:431256
右:342156
#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;
}