标签:letter sam ber logs color put algo rand should
Input
Output
Sample Input
1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 0
Sample Output
AC 2 DDHH 2
“每次移动最多只会使得中央区域包含的数字种类减少1种。求出中央区域个数最多的那个数字的个数 n, 要达到中央区域数字都相同,至少需要 8-n次操作,此即估价函数值
可以用于可行性剪枝。已经移动的步数加上估价函数值,超过本次dfs限定的深度,则剪枝”
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <list> 11 #include <vector> 12 #include <stack> 13 #define mp make_pair 14 //#define P make_pair 15 #define MIN(a,b) (a>b?b:a) 16 //#define MAX(a,b) (a>b?a:b) 17 typedef long long ll; 18 typedef unsigned long long ull; 19 const int MAX=1e2+5; 20 const int INF=1e8+5; 21 using namespace std; 22 //const int MOD=1e9+7; 23 typedef pair<ll,int> pii; 24 const double eps=0.00000001; 25 int a[30]; 26 bool st=false; 27 int rot[9][8]={{},{1,3,7,12,16,21,23},{2,4,9,13,18,22,24},{11,10,9,8,7,6,5},{20,19,18,17,16,15,14}, 28 {14,15,16,17,18,19,20},{5,6,7,8,9,10,11},{24,22,18,13,9,4,2},{23,21,16,12,7,3,1}}; 29 int cen[10]={7,8,9,12,13,16,17,18}; 30 int num[4]; 31 char re[10]={‘A‘,‘A‘,‘B‘,‘C‘,‘D‘,‘G‘,‘H‘,‘E‘,‘F‘}; 32 int aim; 33 int se[10]={0,1,2,3,4,7,8,5,6}; 34 string ans; 35 int check()//返回估值 36 { 37 memset(num,0,sizeof(num)); 38 for(int i=0;i<8;i++) 39 { 40 ++num[a[cen[i]]]; 41 } 42 int re=max(max(num[1],num[2]),num[3]); 43 for(int i=1;i<=3;i++) 44 if(num[i]==re) 45 aim=i; 46 return 8-re; 47 } 48 void change(int i) 49 { 50 int tem=a[rot[i][0]]; 51 for(int j=0;j<6;j++) 52 a[rot[i][j]]=a[rot[i][j+1]]; 53 a[rot[i][6]]=tem; 54 } 55 void dfs(int last,int step,int limit)//9-last操作即为反过来的操作 56 { 57 int tem=check(); 58 if(tem==0) 59 { 60 st=true; 61 if(last) 62 ans+=re[last]; 63 return; 64 } 65 if(step+tem>limit) 66 return; 67 for(int i=1;i<=8;i++) 68 { 69 if(se[i]==9-last) 70 continue; 71 change(se[i]); 72 dfs(se[i],step+1,limit); 73 if(st) 74 { 75 if(last) 76 ans+=re[last]; 77 return; 78 } 79 change(9-se[i]); 80 } 81 } 82 int main() 83 { 84 while(scanf("%d",&a[1])&&a[1]) 85 { 86 for(int i=2;i<=24;i++) 87 scanf("%d",&a[i]); 88 ans=""; 89 st=false; 90 for(int i=0;!st;i++) 91 { 92 dfs(0,0,i); 93 } 94 reverse(ans.begin(),ans.end()); 95 if(ans.size()) 96 cout<<ans<<"\n"; 97 else 98 printf("No moves needed\n"); 99 printf("%d\n",aim); 100 } 101 }
(IDA*)POJ 2286 The Rotation Game
标签:letter sam ber logs color put algo rand should
原文地址:http://www.cnblogs.com/quintessence/p/7214655.html