/*
    ID:chenjiong
    PROG:clocks
    LANG:C++
*/

#include <cstdio>
#include <string>
#include <queue>
#include <iostream>
#include <string.h>

using namespace std;

typedef struct {
    int clock[9];
    string opt;
}S;

S f;
queue<S> q;
bool vis[4][4][4][4][4][4][4][4][4];

void move(S& cur,int n)
{
    switch( n )
    {
        case 1: //ABDE
               cur.clock[0] = ( cur.clock[0] + 1 ) % 4;      
               cur.clock[1] = ( cur.clock[1] + 1 ) % 4;    
               cur.clock[3] = ( cur.clock[3] + 1 ) % 4;
               cur.clock[4] = ( cur.clock[4] + 1 ) % 4;
               cur.opt += "1";
               break;
        case 2: //ABC
               cur.clock[0] = ( cur.clock[0] + 1 ) % 4;    
               cur.clock[1] = ( cur.clock[1] + 1 ) % 4;    
               cur.clock[2] = ( cur.clock[2] + 1 ) % 4;
               cur.opt += "2";
               break;
        case 3: //BCEF
               cur.clock[1] = ( cur.clock[1] + 1 ) % 4;    
               cur.clock[2] = ( cur.clock[2] + 1 ) % 4;    
               cur.clock[4] = ( cur.clock[4] + 1 ) % 4;
               cur.clock[5] = ( cur.clock[5] + 1 ) % 4;
               cur.opt += "3";
               break;
        case 4: //ADG
               cur.clock[0] = ( cur.clock[0] + 1 ) % 4;    
               cur.clock[3] = ( cur.clock[3] + 1 ) % 4;    
               cur.clock[6] = ( cur.clock[6] + 1 ) % 4;
               cur.opt += "4";
               break;
        case 5: //BDEFH
               cur.clock[1] = ( cur.clock[1] + 1 ) % 4;    
               cur.clock[3] = ( cur.clock[3] + 1 ) % 4;    
               cur.clock[4] = ( cur.clock[4] + 1 ) % 4;
               cur.clock[5] = ( cur.clock[5] + 1 ) % 4;
               cur.clock[7] = ( cur.clock[7] + 1 ) % 4;
               cur.opt += "5";
               break;
        case 6: //CFI
               cur.clock[2] = ( cur.clock[2] + 1 ) % 4;    
               cur.clock[5] = ( cur.clock[5] + 1 ) % 4;    
               cur.clock[8] = ( cur.clock[8] + 1 ) % 4;
               cur.opt += "6";
               break;
        case 7: //DEGH
               cur.clock[3] = ( cur.clock[3] + 1 ) % 4;    
               cur.clock[4] = ( cur.clock[4] + 1 ) % 4;    
               cur.clock[6] = ( cur.clock[6] + 1 ) % 4;
               cur.clock[7] = ( cur.clock[7] + 1 ) % 4;
               cur.opt += "7";
               break;
        case 8: //GHI
               cur.clock[6] = ( cur.clock[6] + 1 ) % 4;    
               cur.clock[7] = ( cur.clock[7] + 1 ) % 4;    
               cur.clock[8] = ( cur.clock[8] + 1 ) % 4;
               cur.opt += "8";
               break;
        case 9: //EFHI
               cur.clock[4] = ( cur.clock[4] + 1 ) % 4;    
               cur.clock[5] = ( cur.clock[5] + 1 ) % 4;    
               cur.clock[7] = ( cur.clock[7] + 1 ) % 4;
               cur.clock[8] = ( cur.clock[8] + 1 ) % 4;
               cur.opt += "9";
               break;
    }
}

void bfs()
{
    while ( !q.empty() )
        q.pop();
    memset(vis,false,sizeof(vis));

    vis[f.clock[0]][f.clock[1]][f.clock[2]][f.clock[3]][f.clock[4]][f.clock[5]][f.clock[6]][f.clock[7]][f.clock[8]] = true;
    q.push(f);

    while ( !q.empty() )
    {
        S cur = q.front();
        q.pop();
        if ( cur.clock[0] == 0 && cur.clock[1] == 0 && cur.clock[2] ==  0 && cur.clock[3] == 0 && 
             cur.clock[4] == 0 && cur.clock[5] == 0 && cur.clock[6] ==  0 && cur.clock[7] == 0 && cur.clock[8] == 0 )
        {
            char ans[100];
            strcpy(ans,cur.opt.c_str());
            int len = strlen(ans);
            int i;
            for ( i = 0; i < len - 1; i++)
                printf("%c ",ans[i]);
            printf("%c\n",ans[len - 1]);
            break;
        }

        int k;
        for ( k = 1; k <= 9; k++)
        {
            S next = cur;
            move(next,k);
            if ( !vis[next.clock[0]][next.clock[1]][next.clock[2]][next.clock[3]][next.clock[4]][next.clock[5]][next.clock[6]][next.clock[7]][next.clock[8]] )
            {
                vis[next.clock[0]][next.clock[1]][next.clock[2]][next.clock[3]][next.clock[4]][next.clock[5]][next.clock[6]][next.clock[7]][next.clock[8]] = true;
                q.push(next);
            }
        }
    }
}

            
int main()
{
    freopen("clocks.in","r",stdin);
    freopen("clocks.out","w",stdout);

    int tmp;
    int i;
    for ( i = 0; i < 9; i++)
    {
        scanf("%d",&tmp);
         switch ( tmp )
        {
            case 3: f.clock[i] = 1; break;
            case 6: f.clock[i] = 2; break;
            case 9: f.clock[i] = 3; break;
            case 12: f.clock[i] = 0; break;
        }
    }
    
    bfs();

    return 0;
}
        
            

 

posted on 2012-10-18 22:43  Sinker  阅读(181)  评论(0编辑  收藏  举报