标签:
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
AC
2
DDHH
2
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 #define Rep(i,x,y) for(int i = x,end_ = y;i <= end_;i ++ ) 8 #define Cin(x) scanf("%d",&x); 9 int a[24],maxd; 10 char exper,ans[1000]; 11 const int dir[8][7] = 12 { 13 { 0, 2, 6,11,15,20,22},//A 14 { 1, 3, 8,12,17,21,23},//B 15 {10, 9, 8, 7, 6, 5, 4},//C 16 {19,18,17,16,15,14,13},//D 17 {23,21,17,12, 8, 3, 1},//E 18 {22,20,15,11, 6, 2, 0},//F 19 {13,14,15,16,17,18,19},//G 20 { 4, 5, 6, 7, 8, 9,10} //H 21 }; 22 23 const int center[8] = { 6, 7, 8,11,12,15,16,17}; 24 const int anti[8] = {5,4,7,6,1,0,3,2}; 25 26 void move(int v) 27 { 28 int temp = a[dir[v][0] ]; 29 Rep(i,0,5)a[dir[v][i] ] = a[dir[v][i + 1] ]; 30 a[dir[v][6] ] = temp; 31 } 32 33 int focus(int s) 34 { 35 int ans(0); 36 Rep(i,0,7)if(a[center[i]] != s)ans++; 37 return ans; 38 } 39 40 bool is_arr() 41 { 42 Rep(i,1,7)if(a[6] != a[center[i]])return false; 43 return true; 44 } 45 46 int h() 47 { 48 return min(focus(1),min(focus(2),focus(3) ) ); 49 } 50 51 bool dfs(int d) 52 { 53 if(is_arr()) 54 { 55 ans[d] = ‘\0‘; 56 printf("%s\n",ans); 57 return true; 58 } 59 if( d + h() > maxd )return false; 60 Rep(c,0,7) 61 { 62 ans[d] = ‘A‘ + c; 63 move(c); 64 if(dfs(d+1) )return true; 65 move(anti[c]); 66 } 67 return false; 68 } 69 70 int main() 71 { 72 while(true) 73 { 74 Cin(a[0]);if(!a[0])return 0; 75 Rep(i,1,23)Cin(a[i]); 76 if(is_arr()) printf("No moves needed\n"); 77 else 78 for(maxd = 1;true;maxd++ )if(dfs(0))break; 79 printf("%d\n",a[6]); 80 } 81 return 0; 82 }
原题站点:
Openjudge:http://noi.openjudge.cn/ch0407/1288/
标签:
原文地址:http://www.cnblogs.com/life-9lines/p/4927404.html