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
题目及代码:
直接光搜就可以了,最多搜索到5次就可以了。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; struct node { int s[6]; int step; }l,r,t,tmp; queue<node>Q; bool judge(node t) { for(int i=0;i<6;i++) if(t.s[i]!=r.s[i]) return false; return true; } void left(node &t) { int tmp=t.s[3]; t.s[3]=t.s[1]; t.s[1]=t.s[2]; t.s[2]=t.s[0]; t.s[0]=tmp; } void right(node &t) { int tmp=t.s[0]; t.s[0]=t.s[2]; t.s[2]=t.s[1]; t.s[1]=t.s[3]; t.s[3]=tmp; } void front(node &t) { int tmp=t.s[5]; t.s[5]=t.s[1]; t.s[1]=t.s[4]; t.s[4]=t.s[0]; t.s[0]=tmp; } void back(node &t) { int tmp=t.s[0]; t.s[0]=t.s[4]; t.s[4]=t.s[1]; t.s[1]=t.s[5]; t.s[5]=tmp; } int bfs() { while(!Q.empty()) { Q.pop(); } Q.push(l); while(!Q.empty()) { t=Q.front(); Q.pop(); if(t.step>=5) break; if(judge(t)) return t.step; t.step++; tmp=t; right(tmp); Q.push(tmp); tmp=t; left(tmp); Q.push(tmp); tmp=t; front(tmp); Q.push(tmp); tmp=t; back(tmp); Q.push(tmp); } return -1; } int main() { while(scanf("%d",&l.s[0])!=EOF) { l.step=0; for(int i=1;i<6;i++) { scanf("%d",&l.s[i]); } for(int i=0;i<6;i++) { scanf("%d",&r.s[i]); } int n=bfs(); printf("%d\n",n); } return 0; }
原文:http://blog.csdn.net/knight_kaka/article/details/39298517