2286 The Rotation Game 迭代加深搜索

The Rotation Game
Time Limit: 15000MS   Memory Limit: 150000K
Total Submissions: 3112   Accepted: 997

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.

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<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int map[24];//保存序号为i的位置上的数字(0-23) 数字为0-2
int pos[8][7]={0,2,6,11,15,20,22,
   1,3,8,12,17,21,23,
   10,9,8,7,6,5,4,
   19,18,17,16,15,14,13,
   23,21,17,12,8,3,1,
   22,20,15,11,6,2,0,
   13,14,15,16,17,18,19,
   4,5,6,7,8,9,10};//保存第i行或列第j个数的序号,i从'A'(0)开始,包含了方向
int aim[8]={6,7,8,11,12,15,16,17};//保存中间目标8个的序号
int opp[8]={5,4,7,6,1,0,3,2};//保存第i行或列相反方向的第opp[i]行或列
int ans[50];//保存结果
int DEPTH;//保存迭代加深搜索的深度
int togoal_least()//返回至少还需要多少步才能到目标状态,如果ans已经为8,则返回0,表示已经到目标状态
{
    int _count[3]={0};
    for(int i=0;i<8;i++) _count[map[aim[i]]-1]++;//计算0-2的个数
    int ans=max(_count[0],_count[1]);
    ans=max(ans,_count[2]);//ans保存0-2中个数最多的数字的个数
    return 8-ans;//返回最少还需要多少不才能到目标状态,如果ans已经为8,则返回0,表示已经到目标状态
}
void move(int cur)//朝一定方向移动
{
    int temp=map[pos[cur][0]];
    for(int i=0;i<6;i++) map[pos[cur][i]]=map[pos[cur][i+1]];
    map[pos[cur][6]]=temp;
}
int dfs(int cur)
{
    if(cur>DEPTH) return 0;
    for(int i=0;i<8;i++)
    {
        move(i);//移动第i行或列
        int tmp;
        if((tmp=togoal_least())==0)//如果到达目标状态
        {
            ans[cur]=i;//保存结果
            return 1;
        }
        if(cur+tmp<DEPTH)//剪枝,如果当天步数加上到达目标状态的最小步数大于等于最大步数是,直接剪掉
        {
            ans[cur]=i;
            if(dfs(cur+1)) return 1;
        }
        move(opp[i]);//恢复第i行或列
    }
    return 0;
}
int main()
{
    while(cin>>map[0]&&map[0]!=0)
    {
        for(int i=1;i<24;i++) cin>>map[i];
        if(togoal_least()==0)//不需要移动
        {
            printf("No moves needed/n");
            printf("%d/n",map[aim[0]]);//中间的数字
            continue;
        }
        for(DEPTH=1;!dfs(0);DEPTH++);//迭代加深搜索,枚举深度
        for(int i=0;i<DEPTH;i++) printf("%c",ans[i]+'A');printf("/n");
        printf("%d/n",map[aim[0]]);
    }
    return 0;
}

你可能感兴趣的:(c,input,each,Dictionary,output,Numbers)