您的位置:首页 > 其它

hdu 5012 Dice

2015-08-20 02:29 337 查看
[align=left]Problem Description[/align]
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)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
struct Node
{
int a[7];
int t;
}st,ed;
int vis[10][10][10][10][10][10];
void bfs()
{

queue<Node>q;
q.push(st);

vis[st.a[0]][st.a[1]][st.a[2]][st.a[3]][st.a[4]][st.a[5]]=1;
Node t1,t2;
Node tmp;
while(!q.empty())
{
t1=q.front();
q.pop();
if(t1.a[0]==ed.a[0] && t1.a[1]==ed.a[1]&& t1.a[2]==ed.a[2]&& t1.a[3]==ed.a[3]&& t1.a[4]==ed.a[4] && t1.a[5]==ed.a[5])
{
printf("%d\n",t1.t);
return;
}

t2=t1;
t2.a[0]=t1.a[3];
t2.a[1]=t1.a[2];
t2.a[2]=t1.a[0];
t2.a[3]=t1.a[1];
if(vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]==0)
{
vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]=1;
t2.t=t1.t+1;
q.push(t2);
}

t2=t1;
t2.a[0]=t1.a[2];
t2.a[1]=t1.a[3];
t2.a[2]=t1.a[1];
t2.a[3]=t1.a[0];
if(vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]==0)
{
vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]=1;
t2.t=t1.t+1;
q.push(t2);
}

t2=t1;
t2.a[0]=t1.a[5];
t2.a[1]=t1.a[4];
t2.a[4]=t1.a[0];
t2.a[5]=t1.a[1];
if(vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]==0)
{
vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]=1;
t2.t=t1.t+1;
q.push(t2);
}

t2=t1;
t2.a[0]=t1.a[4];
t2.a[1]=t1.a[5];
t2.a[4]=t1.a[1];
t2.a[5]=t1.a[0];
if(vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]==0)
{
vis[t2.a[0]][t2.a[1]][t2.a[2]][t2.a[3]][t2.a[4]][t2.a[5]]=1;
t2.t=t1.t+1;
q.push(t2);
}
}
printf("-1\n");
}
int main()
{
while(scanf("%d%d%d%d%d%d",&st.a[0],&st.a[1],&st.a[2],&st.a[3],&st.a[4],&st.a[5])==6)
{
scanf("%d%d%d%d%d%d",&ed.a[0],&ed.a[1],&ed.a[2],&ed.a[3],&ed.a[4],&ed.a[5]);
st.t=0;
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: