The main idea: There are eight digital three-way, to find the minimum number of rotations so that the number of graphs in the middle eight. Rotation rule: For each long row or column, each rotation is to move the data to the position of the head, and the number on the head is placed at the tail. If the number of times is the same, the minimum rotation order of dictionary order is found.
Title Analysis: ida*, if currently in the CUR layer, the number of the middle eight number of three-in-one is a,b,c. Then D=8-max (a,b,c) is the desired number of goals to achieve, if cur+d>maxd, then pruning. "Getting Started classic" provides a kind of BFS idea, respectively, with the goal of 3 wide search, but their code force is too weak, and did not use that way AC ...!!!
The code is as follows:
# include<iostream># include<cstdio># include<queue># include<cstring># include< Algorithm>using namespace Std;int a[24],d[8][7]={{0,2,6,11,15,20,22}, {1,3,8,12,17,21,23}, {10,9,8,7,6,5,4}, {19,18,17,16,15,14,13}, {23,21,17,12,8,3,1}, {22,20,15,11,6,2,0}, {13,14,15,16,17,18,19}, {4,5,6,7,8,9,10}, };int g[8]={6,7,8,11,12,15,16,17};int ans;string ansp;bool dfs (int cur,int maxd,string path) {if (cur==maxd) {int ok=1; for (int i=1;i<8;++i) if (A[g[i]]!=a[g[i-1]]) {ok=0; Break } if (!ok) return false; Ans=a[g[0]],ansp=path; return true; } int b[3]={0,0,0}; for (int i=0;i<8;++i) ++b[a[g[i]]-1]; int Dd=8-max (max (b[0],b[1]), b[2]); if (Cur+dd>maxd) return false; for (int i=0;i<8;++i) {int v=a[d[i][0]]; for (int j=0;j<6;++j) a[d[i][j]]=a[d[i][j+1]]; A[d[i][6]]=v; IfDFS (Cur+1,maxd,path+char (i+ ' A '))) return true; V=A[D[I][6]]; for (int j=6;j>0;--j) a[d[i][j]]=a[d[i][j-1]]; A[d[i][0]]=v; } return false;} int main () {while (scanf ("%d", &a[0]) &&a[0]) {for (int i=1;i<24;++i) scanf ("%d", a+i); Ans=-1; for (int maxd=0;; ++maxd) {if (Dfs (0,maxd, "")) {if (maxd==0) printf ("No moves needed\n"); else cout<<ansp<<endl; printf ("%d\n", ans); Break }}} return 0;}
UVA-1343 the Rotation Game (ida*)