The Rotation Game
Time Limit: 45000/15000 MS (Java/Others) Memory Limit: 150000/150000 K (Java/Others)Total Submission(s): 1744 Accepted Submission(s): 727
Problem Description
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.
![](https://i-blog.csdnimg.cn/blog_migrate/1bdff3e484fb496af227e22eef7751ef.jpeg)
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.
Input
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.
Output
For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from `A' to `H', and there should not be any spaces between the letters in the line. If no moves are needed, output `No moves needed' instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.
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#include<stdio.h> #include<algorithm> #include<string.h> #define N 20002 using namespace std; typedef long long ll; int mp[7][7],to[8]={0,0,3,3,1,1,2,2},s[100],p[]={2,4,2,4,4,2,4,2};//to表示8种移法,配合p表示哪一行或列 int u[]={1,-1,0,0}; int v[]={0,0,1,-1}; void chage(int p,int x,int y)//x=1移行,y=1右移 { if(x&&y){ int w=mp[p][6]; for(int i=6;i;i--) mp[p][i]=mp[p][i-1]; mp[p][0]=w; } else if(x){ int w=mp[p][0]; for(int i=0;i<6;i++) mp[p][i]=mp[p][i+1]; mp[p][6]=w; } else if(y){ int w=mp[6][p]; for(int i=6;i;i--) mp[i][p]=mp[i-1][p]; mp[0][p]=w; } else { int w=mp[0][p]; for(int i=0;i<6;i++) mp[i][p]=mp[i+1][p]; mp[6][p]=w; } } int h()//求h估值函数 { int c[4]={0,0,0,0}; for(int i=2;i<=4;i++){ for(int j=2;j<=4;j++) c[mp[i][j]]++; } return 8-max(c[1],max(c[2],c[3])); } bool dfs(int depth) { int hh=h(); if(hh>depth)return 0; if(!depth)return hh==0; for(int i=0;i<8;i++) { chage(p[i],to[i]/2,to[i]%2); s[depth]=i; if(dfs(depth-1))return 1; chage(p[i],to[i]/2,(to[i]+1)%2); } return 0; } int main () { int x; while(~scanf("%d",&x)&&x) { mp[0][2]=x; scanf("%d",&mp[0][4]); for(int i=1;i<7;i++){ if(i!=4&&i!=2)scanf("%d%d",&mp[i][2],&mp[i][4]); else { for(int j=0;j<7;j++) scanf("%d",&mp[i][j]); } } int hh=h(); if(!hh){ printf("No moves needed\n%d\n",mp[2][2]); continue; } for(int i=1;;i++){ if(dfs(i)){ for(int j=i;j;j--) printf("%c",s[j]+'A'); printf("\n%d\n",mp[2][2]); break; } } } return 0; }