The Rotation Game
Source : ACM ICPC Shanghai Regional Contest 2004 | |||
Time limit : 10 sec | Memory limit : 128 M |
Submitted : 49, Accepted : 32
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.
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 0Sample Output
AC 2 DDHH 2
Shanghai 2004
#include<iostream>
#include<stdio.h>
#include<stack>
#include<vector>
#include<cstring>
#include<string.h>
using namespace std;
#define mp make_pair
char block[9][9];
int pos[] = { 2 , 4 , 9 , 11 , 14 ,15,16,17,18,19,20,
23,25,28,29,30,31,32,33,34,37,39,44,46 };
int c;
char S[10000];
struct State
{
char block[9][9];
char Move;
int g , h;
};
inline int max(int a,int b)
{
return a < b ? b : a;
}
inline int row(int x)
{
return x / 7;
}
inline int column(int x)
{
return x % 7;
}
int H(const State & st)
{
int one = 0 , two = 0 , three = 0;
for (int i = 2 ; i <= 4 ; ++i)
{
if (st.block[2][i]=='1') ++one;
else if (st.block[2][i]=='2') ++two;
else if (st.block[2][i]=='3') ++three;
if (st.block[4][i]=='1') ++one;
else if (st.block[4][i]=='2') ++two;
else if (st.block[4][i]=='3') ++three;
}
if (st.block[3][2]=='1') ++one;
else if (st.block[3][2]=='2') ++two;
else if (st.block[3][2]=='3') ++three;
if (st.block[3][4]=='1') ++one;
else if (st.block[3][4]=='2') ++two;
else if (st.block[3][4]=='3') ++three;
return 8-max(one,max(two,three));
}
void MoveA(State & st)
{
++st.g;
char tmp = st.block[0][2];
for (int i = 0 ; i < 6 ; ++i)
st.block[i][2] = st.block[i+1][2];
st.block[6][2] = tmp;
st.h = H(st);
st.Move = 'A';
}
void MoveF(State & st)
{
++st.g;
char tmp = st.block[6][2];
for (int i = 6 ; i > 0 ; --i)
st.block[i][2] = st.block[i-1][2];
st.block[0][2] = tmp;
st.h = H(st);
st.Move = 'F';
}
void MoveB(State & st)
{
++st.g;
char tmp = st.block[0][4];
for (int i = 0 ; i < 6 ; ++i)
st.block[i][4] = st.block[i+1][4];
st.block[6][4] = tmp;
st.h = H(st);
st.Move = 'B';
}
void MoveE(State & st)
{
++st.g;
char tmp = st.block[6][4];
for (int i = 6 ; i > 0 ; --i)
st.block[i][4] = st.block[i-1][4];
st.block[0][4] = tmp;
st.h = H(st);
st.Move = 'E';
}
void MoveH(State & st)
{
++st.g;
char tmp = st.block[2][0];
for (int j = 0 ; j < 6 ; ++j)
st.block[2][j] = st.block[2][j+1];
st.block[2][6] = tmp;
st.h = H(st);
st.Move = 'H';
}
void MoveG(State & st)
{
++st.g;
char tmp = st.block[4][0];
for (int j = 0 ; j < 6 ; ++j)
st.block[4][j] = st.block[4][j+1];
st.block[4][6] = tmp;
st.h = H(st);
st.Move = 'G';
}
void MoveC(State & st)
{
++st.g;
char tmp = st.block[2][6];
for (int j = 6 ; j > 0 ; --j)
st.block[2][j] = st.block[2][j-1];
st.block[2][0] = tmp;
st.h = H(st);
st.Move = 'C';
}
void MoveD(State & st)
{
++st.g;
char tmp = st.block[4][6];
for (int j = 6 ; j > 0 ; --j)
st.block[4][j] = st.block[4][j-1];
st.block[4][0] = tmp;
st.h = H(st);
st.Move = 'D';
}
void (*Move[8])(State & st) = { MoveA,MoveB,MoveC,MoveD,MoveE,
MoveF,MoveG,MoveH };
void output(stack<pair<int,char> >& path,char val)
{
stack<char> out;
while (path.size() > 1)
{
out.push(path.top().second);
path.pop();
}
while (out.size())
{
printf("%c",out.top());
out.pop();
}
cout << endl << val << endl;
}
bool dfs(int maxdep , const State & s)
{
if (maxdep < s.g+s.h) return false;
S[c++] = s.Move;
if (s.h==0) {
for (int i = 1 ; i < c ; ++i) printf("%c",S[i]);
if (c==1) printf("No moves needed");
printf("\n%c\n",s.block[2][2]);
return true;
}
for (int i = 0 ; i < 8 ; ++i)
{
State now = s;
Move[i](now);
if (dfs(maxdep,now)) return true;
}
--c;
return false;
}
void IDAStar()
{
int max_dep;
State start;
for (int i = 0 ; i < 9 ; ++i)
for (int j = 0 ; j < 9 ; ++j)
start.block[i][j] = block[i][j];
start.g = 0 , start.h = H(start);
start.Move = 'N';
bool success = false;
max_dep = start.h;
for ( ; ; ++max_dep)
{
c = 0;
if (dfs(max_dep,start)) break;
}
}
int main()
{
int a;
while (scanf("%d",&a),a)
{
memset(block,0,sizeof(block));
block[0][2] = a+'0';
for (int i = 1 ; i <= 23 ; ++i)
{
scanf("%d",&a);
block[row(pos[i])][column(pos[i])] = a+'0';
}
IDAStar();
}
}