http://acm.hdu.edu.cn/showproblem.php?pid=5012
模拟出骰子四种反转方式,bfs,最多不会走超过6步
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <queue> #include <vector> #include <iostream> #include <algorithm> using namespace std; #define RD(x) scanf("%d",&x) #define RD2(x,y) scanf("%d%d",&x,&y) #define clr0(x) memset(x,0,sizeof(x)) typedef long long LL; int a[6],b[6]; bool ard(int x) { int c[8],d[4]; c[0] = a[4],c[1] = a[3],c[2] = a[5],c[3] = a[2]; for(int j = 0;j < 4;++j) c[j+4] = c[j]; if(x == 0){ d[0] = b[4],d[1] = b[3],d[2] = b[5],d[3] = b[2]; } else if(x == 1){ d[0] = b[4],d[1] = b[2],d[2] = b[5],d[3] = b[3]; } else if(x == 2){ d[0] = b[5],d[1] = b[1],d[2] = b[4],d[3] = b[0]; } else if(x == 3){ d[0] = b[5],d[1] = b[0],d[2] = b[4],d[3] = b[1]; } else if(x == 4){ d[0] = b[1],d[1] = b[3],d[2] = b[0],d[3] = b[2]; } else if(x == 5){ d[0] = b[1],d[1] = b[2],d[2] = b[0],d[3] = b[3]; } for(int i = 0;i < 8;++i) if(d[0] == c[i] && i + 3 < 8 && d[1] == c[i+1] && d[2] == c[i+2] && d[3] == c[i+3]) return true; return false; } bool check() { int top = a[0],bot = a[1]; for(int i = 0;i < 6;++i){ if(top == b[i] && bot == b[i+1] && ard(i)) return true; if(top == b[i+1] && bot == b[i] && ard(i+1)) return true; } return false; } bool same(int d[6]) { for(int i = 0;i < 6;++i){ if(a[i] != d[i]) return false; } return true; } int ans; bool vis[700000]; void dfs(int r,int da[6]) { //cout<<r<<endl; if(r > 9) return ; if(same(da)){ ans = min(ans,r); return ; } int f = da[0]*100000+da[1]*10000+da[2]*1000+da[3]*100+da[4]*10+da[5]; if(vis[f]) return ; vis[f] = 1; int c[6]; c[0] = da[3],c[1] = da[2],c[2] = da[0],c[3] = da[1],c[4] = da[4],c[5] = da[5]; dfs(r+1,c); c[0] = da[2],c[1] = da[3],c[2] = da[1],c[3] = da[0],c[4] = da[4],c[5] = da[5]; dfs(r+1,c); c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[0],c[5] = da[1]; dfs(r+1,c); c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[1],c[5] = da[0]; dfs(r+1,c); } void bfs() { queue< pair <int,int> > q; int f = b[0]*100000+b[1]*10000+b[2]*1000+b[3]*100+b[4]*10+b[5]; //int da[6] = {b[0],b[1],b[2],b[3],b[4],b[5]}; q.push(make_pair(0,f)); while(!q.empty()){ int r = q.front().first; f = q.front().second; vis[f] = 1; q.pop(); int da[6] = {f/100000,f/10000%10,f/1000%10,f/100%10,f/10%10,f%10}; if(same(da)){ ans = min(ans,r); return ; } f = da[3]*100000+da[2]*10000+da[0]*1000+da[1]*100+da[4]*10+da[5]; if(!vis[f]){ q.push(make_pair(r+1,f)); } f = da[2]*100000+da[3]*10000+da[1]*1000+da[0]*100+da[4]*10+da[5]; if(!vis[f]){ q.push(make_pair(r+1,f)); } f = da[5]*100000+da[4]*10000+da[2]*1000+da[3]*100+da[0]*10+da[1]; if(!vis[f]){ q.push(make_pair(r+1,f)); } f = da[4]*100000+da[5]*10000+da[2]*1000+da[3]*100+da[1]*10+da[0]; if(!vis[f]){ q.push(make_pair(r+1,f)); } //int c[6] = {da[3],da[2],da[0],da[1],da[4],da[5]}; //c[0] = da[3],c[1] = da[2],c[2] = da[0],c[3] = da[1],c[4] = da[4],c[5] = da[5]; //q.push(make_pair(r+1,c)); // int cc[6] = {da[2],da[3],da[1],da[0],da[4],da[5]}; // //c[0] = da[2],c[1] = da[3],c[2] = da[1],c[3] = da[0],c[4] = da[4],c[5] = da[5]; // q.push(make_pair(r+1,cc)); // int ccc[6] = {da[4],da[5],da[2], da[3], da[0],da[1]}; // //c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[0],c[5] = da[1]; // q.push(make_pair(r+1,ccc)); // int cccc[6] = {da[4],da[5],da[2],da[3],da[0],da[1]}; // //c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[0],c[5] = da[1]; // q.push(make_pair(r+1,cccc)); } } //top face, bottom face, left face, right face, front face and back face int main () { while(~RD(a[0])){ for(int i = 1;i < 6;++i) RD(a[i]); for(int i = 0;i < 6;++i) RD(b[i]); if(!check()){ puts("-1"); continue; } clr0(vis); ans = 10005; bfs(); if(ans >= 10005) puts("-1"); else printf("%d\n",ans); } return 0; }