题目大意
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
0
Sample Output
AC
2
DDHH
2
思路
神奇的IDA*做法。初始最大搜索深度为1,看能不能跑出正确结果,如果不行,深度加1,继续搜,直到搜出正确答案。这种做法适合深度不好确定的题目,如果能确定深度还是花时间需要分析的,即可以先打个暴力,如果TLE在尝试优化确定深度。大概能从N^2优化到N(N为最大深度),一般而言深度不太大的时候不优化更省时间。
代码
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN=30;
int a[MAXN];
int cnt[4];
vector<char> ans;
bool get(){
scanf("%d",&a[1]);
if(a[1]==0)return false;
for(int i=2;i<=24;i++){
scanf("%d",&a[i]);
}
return true;
}
int find_num(){
memset(cnt,0,sizeof(cnt));
cnt[a[7]]++;
cnt[a[8]]++;
cnt[a[9]]++;
cnt[a[12]]++;
cnt[a[13]]++;
cnt[a[16]]++;
cnt[a[17]]++;
cnt[a[18]]++;
int t=max(cnt[1],cnt[2]);
return max(t,cnt[3]);
}
void change(char x){
if(x=='A'){
int dis=a[1];a[1]=a[3];
a[3]=a[7];a[7]=a[12];
a[12]=a[16];a[16]=a[21];
a[21]=a[23];a[23]=dis;
}
if(x=='B'){
int dis=a[2];a[2]=a[4];
a[4]=a[9];a[9]=a[13];
a[13]=a[18];a[18]=a[22];
a[22]=a[24];a[24]=dis;
}
if(x=='C'){
int dis=a[11];a[11]=a[10];
a[10]=a[9];a[9]=a[8];
a[8]=a[7];a[7]=a[6];
a[6]=a[5];a[5]=dis;
}
if(x=='D'){
int dis=a[20];a[20]=a[19];
a[19]=a[18];a[18]=a[17];
a[17]=a[16];a[16]=a[15];
a[15]=a[14];a[14]=dis;
}
if(x=='E'){
int dis=a[24];a[24]=a[22];
a[22]=a[18];a[18]=a[13];
a[13]=a[9];a[9]=a[4];
a[4]=a[2];a[2]=dis;
}
if(x=='F'){
int dis=a[23];a[23]=a[21];
a[21]=a[16];a[16]=a[12];
a[12]=a[7];a[7]=a[3];
a[3]=a[1];a[1]=dis;
}
if(x=='G'){
int dis=a[14];a[14]=a[15];
a[15]=a[16];a[16]=a[17];
a[17]=a[18];a[18]=a[19];
a[19]=a[20];a[20]=dis;
}
if(x=='H'){
int dis=a[5];a[5]=a[6];
a[6]=a[7];a[7]=a[8];
a[8]=a[9];a[9]=a[10];
a[10]=a[11];a[11]=dis;
}
}
bool dfs(int d,int md){
int k=8-find_num();
if(k==0)return true;
if(d+k>md)return false;
change('A');
ans.push_back('A');
if(dfs(d+1,md))return true;
change('F');
ans.pop_back();
change('B');
ans.push_back('B');
if(dfs(d+1,md))return true;
change('E');
ans.pop_back();
change('C');
ans.push_back('C');
if(dfs(d+1,md))return true;
change('H');
ans.pop_back();
change('D');
ans.push_back('D');
if(dfs(d+1,md))return true;
change('G');
ans.pop_back();
change('E');
ans.push_back('E');
if(dfs(d+1,md))return true;
change('B');
ans.pop_back();
change('F');
ans.push_back('F');
if(dfs(d+1,md))return true;
change('A');
ans.pop_back();
change('G');
ans.push_back('G');
if(dfs(d+1,md))return true;
change('D');
ans.pop_back();
change('H');
ans.push_back('H');
if(dfs(d+1,md))return true;
change('C');
ans.pop_back();
return false;
}
int main()
{
//freopen("1.txt","r",stdin);
//freopen("2.txt","w",stdout);
int max_deep=1;
while(get()){
int k=find_num();
if(k==8){
printf("No moves needed");
printf("\n%d\n",a[7]);
continue;
}
max_deep=1;
while(1){
if(dfs(0,max_deep)){
for(int i=0;i<ans.size();i++)printf("%c",ans[i]);
ans.clear();
printf("\n%d\n",a[7]);
break;
}
max_deep++;
}
}
}