POJ 2286 The Rotation Game

第一次写IDA*。。。。

The Rotation Game
Time Limit: 15000MS   Memory Limit: 150000K
Total Submissions: 4915   Accepted: 1635

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. 
POJ 2286 The Rotation Game_第1张图片
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

Source

Shanghai 2004


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int tu[25],deep;

void change(int& a1,int& a2,int& a3,int& a4,int& a5,int& a6,int& a7)
{
    int temp=a1;
    a1=a2;a2=a3;a3=a4;a4=a5;a5=a6;a6=a7;a7=temp;
}

int count(int tu[])
{
    int a[4]={0,0,0,0};
    a[tu[6]]++;a[tu[7]]++;a[tu[8]]++;
    a[tu[11]]++; a[tu[12]]++;
    a[tu[15]]++; a[tu[16]]++; a[tu[17]]++;
    return max(a[1],max(a[2],a[3]));
}

int OK(int tu[],int& ak)
{
    int temp=tu[6];
    ak=temp;
    if(temp!=tu[7]||temp!=tu[8]||temp!=tu[11]||temp!=tu[12]
       ||temp!=tu[15]||temp!=tu[16]||temp!=tu[17]) return 0;
    return 1;
}

void CHANGE_A(int tu[])
{
    change(tu[0],tu[2],tu[6],tu[11],tu[15],tu[20],tu[22]);
}

void CHANGE_F(int tu[])
{
    change(tu[22],tu[20],tu[15],tu[11],tu[6],tu[2],tu[0]);
}

void CHANGE_B(int tu[])
{
    change(tu[1],tu[3],tu[8],tu[12],tu[17],tu[21],tu[23]);
}

void CHANGE_E(int tu[])
{
    change(tu[23],tu[21],tu[17],tu[12],tu[8],tu[3],tu[1]);
}

void CHANGE_C(int tu[])
{
    change(tu[10],tu[9],tu[8],tu[7],tu[6],tu[5],tu[4]);
}

void CHANGE_H(int tu[])
{
    change(tu[4],tu[5],tu[6],tu[7],tu[8],tu[9],tu[10]);
}

void CHANGE_D(int tu[])
{
    change(tu[19],tu[18],tu[17],tu[16],tu[15],tu[14],tu[13]);
}

void CHANGE_G(int tu[])
{
    change(tu[13],tu[14],tu[15],tu[16],tu[17],tu[18],tu[19]);
}


char OUT[1100];

int dfs(int pre,int now,int *tu)
{
    if(now>deep) return 0;
    if(deep-now<8-count(tu)) return 0;

    int ak=0;
    int tr[25];
    for(int i=0;i<8;i++)
    {
        if((i==0&&pre==5)||(pre==0&&i==5)) continue;
         if((i==1&&pre==4)||(pre==1&&i==4)) continue;
          if((i==2&&pre==7)||(pre==2&&i==7)) continue;
           if((i==3&&pre==6)||(pre==3&&i==6)) continue;

        for(int j=0;j<25;j++) tr[j]=tu[j];

        if(i==0)
        {
            CHANGE_A(tr); OUT[now]='A';
        }
        else if(i==1)
        {
            CHANGE_B(tr); OUT[now]='B';
        }
        else if(i==2)
        {
            CHANGE_C(tr); OUT[now]='C';
        }
        else if(i==3)
        {
            CHANGE_D(tr); OUT[now]='D';
        }
        else if(i==4)
        {
            CHANGE_E(tr); OUT[now]='E';
        }
        else if(i==5)
        {
            CHANGE_F(tr); OUT[now]='F';
        }
        else if(i==6)
        {
            CHANGE_G(tr); OUT[now]='G';
        }
        else if(i==7)
        {
            CHANGE_H(tr); OUT[now]='H';
        }

        if(OK(tr,ak))
        {
            printf("%s\n%d\n",OUT,ak);
            return 1;
        }
        if(dfs(i,now+1,tr)) return 1;
    }
    return 0;
}


int main()
{
while(scanf("%d",tu)!=EOF&&tu[0])
{
    for(int i=1;i<24;i++) scanf("%d",tu+i);
    deep=1;
    int ak=0;
    if(OK(tu,ak))
    {
        puts("No moves needed");
        printf("%d\n",ak);
        continue;
    }
    memset(OUT,0,sizeof(OUT));
    while(true)
    {
        if(!dfs(-1,0,tu)) deep++;
        else break;
    }
}
    return 0;
}



你可能感兴趣的:(POJ 2286 The Rotation Game)