标签:des style blog http color io os java ar

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
0 3 -1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=999999+10;
struct cube
{
int a1,a2,a3,a4,a5,a6;
int key;
}st,en,next;
queue<cube>Q;
int ans;
bool vis[maxn];
int gao(cube c)
{
int temp=0;
temp+=c.a1*1;
temp+=c.a2*10;
temp+=c.a3*100;
temp+=c.a4*1000;
temp+=c.a5*10000;
temp+=c.a6*100000;
return temp;
}
cube solve(cube c,int k)
{
cube temp;
if(k==1)
{
temp.a1=c.a4;
temp.a2=c.a3;
temp.a3=c.a1;
temp.a4=c.a2;
temp.a5=c.a5;
temp.a6=c.a6;
}
else if(k==2)
{
temp.a1=c.a3;
temp.a2=c.a4;
temp.a3=c.a2;
temp.a4=c.a1;
temp.a5=c.a5;
temp.a6=c.a6;
}
else if(k==3)
{
temp.a1=c.a6;
temp.a2=c.a5;
temp.a3=c.a3;
temp.a4=c.a4;
temp.a5=c.a1;
temp.a6=c.a2;
}
else
{
temp.a1=c.a5;
temp.a2=c.a6;
temp.a3=c.a3;
temp.a4=c.a4;
temp.a5=c.a2;
temp.a6=c.a1;
}
return temp;
}
int bfs()
{
ans=gao(en);
memset(vis,false,sizeof(vis));
while(!Q.empty()) Q.pop();
st.key=0;
Q.push(st);
int temp=gao(st);
vis[temp]=true;
while(!Q.empty())
{
cube current=Q.front();
Q.pop();
if(ans==gao(current))
return current.key;
for(int i=1;i<=4;i++)
{
next=solve(current,i);
next.key=current.key+1;
int temp=gao(next);
if(!vis[temp])
{
vis[temp]=true;
Q.push(next);
}
}
}
return -1;
}
int main()
{
while(~scanf("%d",&st.a1))
{
scanf("%d%d%d%d%d",&st.a2,&st.a3,&st.a4,&st.a5,&st.a6);
scanf("%d%d%d%d%d%d",&en.a1,&en.a2,&en.a3,&en.a4,&en.a5,&en.a6);
printf("%d\n",bfs());
}
return 0;
}
标签:des style blog http color io os java ar
原文地址:http://blog.csdn.net/u014303647/article/details/39272343