Second-order Rubik's Cube, only 0, 1
Ask at least how many steps can be turned to each face is 0, or 1
BFS can, corresponding to a good rotation time of the relationship, because clockwise turn three times and counterclockwise Turn 1 times the same effect, so as long as 6 kinds of rotation can be, sentenced to use map save space, or directly to the time
#include "stdio.h" #include "string.h" #include "map" #include "queue" using namespace Std;bool mp[17000000];struct node{I NT X[24],t;}; int A[25];int gethash (node cur) {int cnt,i; cnt=0; for (i=0;i<24;i++) cnt=cnt*2+cur.x[i]; return CNT;} int judge (node cur) {int i; for (i=0;i<24;i+=4) if (cur.x[i]!=cur.x[i+1] | | cur.x[i]!=cur.x[i+2] | | cur.x[i]!=cur.x[i+3]) return-1; return 1;} int BFs () {queue<node>q; Node Cur,next; int sum,i,temp; sum=0; for (i=0;i<24;i++) {cur.x[i]=a[i]; Sum+=a[i]; } if (sum%4!=0) return-1; cur.t=0; if (judge (cur) ==1) return 0; Q.push (cur); Memset (Mp,false,sizeof (MP)); Mp[gethash (cur)]=true; while (!q.empty ()) {Cur=q.front (); Q.pop (); Next=cur; Zheng Shun temp=next.x[0]; NEXT.X[0]=NEXT.X[2]; NEXT.X[2]=NEXT.X[3]; NEXT.X[3]=NEXT.X[1]; Next.x[1]=temp; TEMP=NEXT.X[9]; NEXT.X[9]=NEXT.X[15]; Next.x[15]=next.x[17];next.x[17]=next. x[4]; Next.x[4]=temp; TEMP=NEXT.X[8]; NEXT.X[8]=NEXT.X[13]; NEXT.X[13]=NEXT.X[16];NEXT.X[16]=NEXT.X[6]; Next.x[6]=temp; Temp=gethash (next); if (mp[temp]==false) {mp[temp]=true; next.t++; if (judge (next) ==1) return next.t; Q.push (next); } next=cur; positive inverse temp=next.x[0]; NEXT.X[0]=NEXT.X[1]; NEXT.X[1]=NEXT.X[3]; NEXT.X[3]=NEXT.X[2]; Next.x[2]=temp; TEMP=NEXT.X[9]; NEXT.X[9]=NEXT.X[4]; NEXT.X[4]=NEXT.X[17]; Next.x[17]=next.x[15];next.x[15]=temp; TEMP=NEXT.X[8]; NEXT.X[8]=NEXT.X[6]; NEXT.X[6]=NEXT.X[16]; Next.x[16]=next.x[13];next.x[13]=temp; Temp=gethash (next); if (mp[temp]==false) {mp[temp]=true; next.t++; if (judge (next) ==1) return next.t; Q.push (next); } next=cur; Right Shun temp=next.x[4]; NEXT.X[4]=NEXT.X[6]; NEXT.X[6]=NEXT.X[7]; NEXT.X[7]=NEXT.X[5]; Next.x[5]=temp; Temp=next. X[8]; NEXT.X[8]=NEXT.X[3]; NEXT.X[3]=NEXT.X[19]; Next.x[19]=next.x[20];next.x[20]=temp; TEMP=NEXT.X[10]; NEXT.X[10]=NEXT.X[1]; NEXT.X[1]=NEXT.X[17]; Next.x[17]=next.x[22];next.x[22]=temp; Temp=gethash (next); if (mp[temp]==false) {mp[temp]=true; next.t++; if (judge (next) ==1) return next.t; Q.push (next); } next=cur; Right inverse temp=next.x[4]; NEXT.X[4]=NEXT.X[5]; NEXT.X[5]=NEXT.X[7]; NEXT.X[7]=NEXT.X[6]; Next.x[6]=temp; TEMP=NEXT.X[8]; NEXT.X[8]=NEXT.X[20]; NEXT.X[20]=NEXT.X[19];NEXT.X[19]=NEXT.X[3]; Next.x[3]=temp; TEMP=NEXT.X[10]; NEXT.X[10]=NEXT.X[22];NEXT.X[22]=NEXT.X[17];NEXT.X[17]=NEXT.X[1]; Next.x[1]=temp; Temp=gethash (next); if (mp[temp]==false) {mp[temp]=true; next.t++; if (judge (next) ==1) return next.t; Q.push (next); } next=cur; Shang Shun temp=next.x[11]; NEXT.X[11]=NEXT.X[9]; NEXT.X[9]=NEXT.X[8]; NEXT.X[8]=NEXT.X[10]; Next.x[10]=temp; TEMP=NEXT.X[0]; NEXT.X[0]=NEXT.X[4]; NEXT.X[4]=NEXT.X[20]; Next.x[20]=next.x[12];next.x[12]=temp; TEMP=NEXT.X[1]; NEXT.X[1]=NEXT.X[5]; NEXT.X[5]=NEXT.X[21]; Next.x[21]=next.x[13];next.x[13]=temp; Temp=gethash (next); if (mp[temp]==false) {mp[temp]=true; next.t++; if (judge (next) ==1) return next.t; Q.push (next); } next=cur; Upper Inverse temp=next.x[11]; NEXT.X[11]=NEXT.X[10];NEXT.X[10]=NEXT.X[8]; NEXT.X[8]=NEXT.X[9]; Next.x[9]=temp; TEMP=NEXT.X[0]; NEXT.X[0]=NEXT.X[12]; NEXT.X[12]=NEXT.X[20];NEXT.X[20]=NEXT.X[4]; Next.x[4]=temp; TEMP=NEXT.X[1]; NEXT.X[1]=NEXT.X[13]; NEXT.X[13]=NEXT.X[21];NEXT.X[21]=NEXT.X[5]; Next.x[5]=temp; Temp=gethash (next); if (mp[temp]==false) {mp[temp]=true; next.t++; if (judge (next) ==1) return next.t; Q.push (next); }} RETurn-1;} int main () {int n,i,ans; scanf ("%d", &n); while (n--) {for (i=0;i<24;i++) scanf ("%d", &a[i]); Ans=bfs (); if (ans==-1) printf ("impossible!\n"); else printf ("%d\n", ans); } return 0;}
2013 Shandong Province Rubik ' s cube-cube BFS