旋转游戏-题解
时间:2015-11-01 14:59:55
收藏:0
阅读:398
- 总时间限制: 15000ms 内存限制: 150000kB
- 描述
- The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1).
- The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.
- Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from some initial configuration.
- 输入
- The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single `0‘ after the last test case that ends the 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
- 样例输出
-
AC 2 DDHH 2
- 来源
- Shanghai 2004,Uva1343,Openjudge
- 本题使用IDA*算法,h()为乐观估价函数,当然也可以用BFS;
-
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
评论(0)