The Rotation Game
题目属于ID*搜索类型题目!主要还是转换坐标!其次就是ID*的那个剪枝!这是我的代码!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int s1[30]; 6 char sign[10001]; 7 bool foud; 8 int ant; 9 int hash[10]; 10 int index[20]={7,8,9,12,13,16,17,18,0}; 11 int reach[8][7]={{1,3,7,12,16,21,23},{2,4,9,13,18,22,24},{11,10,9,8,7,6,5}, 12 {20,19,18,17,16,15,14},{24,22,18,13,9,4,2},{23,21,16,12,7,3,1}, 13 {14,15,16,17,18,19,20},{5,6,7,8,9,10,11}}; 14 int HASH() 15 { 16 int i; 17 for(i=1;i<4;i++) 18 hash[i]=0; 19 for(i=0;i<8;i++) 20 { 21 hash[s1[index[i]]]++; 22 } 23 int max; 24 max=0; 25 for(i=1;i<4;i++) 26 if(max<hash[i])max=hash[i]; 27 return max; 28 } 29 void dfs(int step,int num) 30 { 31 if(8-HASH()+step>num)return ; 32 int i,j; 33 bool check=true; 34 for(i=1;i<8;i++) 35 { 36 if(s1[index[i]]!=s1[index[i-1]]) 37 { 38 check=false; 39 break; 40 } 41 } 42 if(check) 43 { 44 ant=s1[index[0]]; 45 foud=true; 46 return ; 47 } 48 if(step>=num)return; 49 int ans; 50 for(i=0;i<8;i++) 51 { 52 int s2[30]; 53 sign[step]=(i+'A'); 54 ans=s1[reach[i][0]]; 55 for(j=1;j<7;j++) 56 { 57 s1[reach[i][j-1]]=s1[reach[i][j]]; 58 } 59 s1[reach[i][j-1]]=ans; 60 dfs(step+1,num); 61 if(foud)return ; 62 ans=s1[reach[i][6]]; 63 for(j=6;j>=1;j--) 64 { 65 s1[reach[i][j]]=s1[reach[i][j-1]]; 66 } 67 s1[reach[i][0]]=ans; 68 sign[step]='\0'; 69 } 70 } 71 int main() 72 { 73 __int64 i,j; 74 int x; 75 while(scanf("%d",&x)&&x) 76 { 77 foud=true; 78 s1[1]=x; 79 for(i=2;i<=24;i++) 80 scanf("%d",&s1[i]); 81 memset(sign,'\0',sizeof(sign)); 82 for(i=1;i<8;i++) 83 { 84 if(s1[index[i]]!=s1[index[i-1]]) 85 { 86 foud=false; 87 break; 88 } 89 } 90 if(!foud) 91 { 92 for(i=1;;i++) 93 { 94 dfs(0,i); 95 if(foud)break; 96 } 97 if(foud) 98 { 99 cout<<sign<<endl; 100 cout<<s1[index[0]]<<endl; 101 } 102 } 103 else 104 { 105 cout<<"No moves needed"<<endl; 106 cout<<s1[index[0]]<<endl; 107 } 108 } 109 return 0; 110 }