JOJ 2687

JOJ
2687
刘备的连营
话说关羽败走麦城,张飞急兄仇反被害后,刘备在夷陵连营七百余里以战孙权。但当马
良将先主的布阵图交与诸葛亮后,孔明大怒,只呼喊:“汉室亡已”。于是连忙让马良劝说先
主赶快改换阵势,否则大事去矣。
现在蜀军已经摆好了如图的阵势,问要变成诸葛孔明所讲的阵势,最少要经过多少次“特
殊变换”。
张苞


刘备
关兴
傅彤
赵云
程畿
沙摩柯
黄权
原阵势
“特殊变换”:阵势中只有刘备的主营能和它上下左右的营地交换次序。
Input
There are several test cases in the input.
The first line of the input is a number N, standing for the number of the test
cases. Then there will be N block(s), each block has two battle array
representing the starting and ending status of this case. The status is composed
by 9 characters, A-I, each character appears only one time in each status. A
stands for 刘备in the board. The 9 characters are arranged on three lines, each
line has three characters. Each character is separated by one space. There is
a blank line between two statuses and blocks.
Output
Print the string " Battle array #ID :" first, where ID is a positive number, start
from 1. Then on the next line, print "The minimum move of the battle array is :
Times", where Times is the answer you get.
There is a blank line after each battle array.
Sample
Input
2
黄权
张苞
关索
傅彤
关兴
程畿
沙摩柯
赵云
刘备
改变后的
阵势
C I D
B G E
H A F
B C D
I A E
H G F
D E F
B C G
H A I
D E F
B C G
H A I
Sample
Output
Battle array #1 :
The minimum move of the battle array is : 5
Battle array #2 :
The minimum move of the battle array is : 0
Problem Source:allang

 

 

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int fac[]= {1,1,2,6,24,120,720,5040,40320};//n!
int hash[400000];
int dir[]={-1,-3,1,3};
bool move[][4] = {0,0,1,1, 1,0,1,1, 1,0,0,1, 0,1,1,1, 1,1,1,1, 1,1,0,1, 0,1,1,0, 1,1,1,0, 1,1,0,0};
struct ss
{
    int step;
    char x[10];
};
queue<ss> t;
int getkey(char *seq)
{
    int i,j,cnt,key=0;
    for(i=0; i<9; i++)
    {
        cnt=0;
        for(j=0; j<i; j++)
            if(seq[j]>seq[i]) cnt++;
        key+=cnt*fac[i];       // cnt<=i
    }
    return key;
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        while(!t.empty())  t.pop();
        ss  m;
        char x[15],y[15],dd[15];
        for(int i=0;i<9;i++)
        {
            scanf(" %s",dd);
            x[i]=dd[0];
        }
        x[9]=0;
        for(int i=0;i<9;i++)
        {
            scanf(" %s",dd);
            y[i]=dd[0];
        }
        y[9]=0;
        memset(hash,0,sizeof(hash));
       
        int head=0,end=1;
        int key=getkey(x);
        hash[key]=1;
       
     
        m.step=0;
        strcpy(m.x,x);
        t.push(m);
        int z,zz;
       
        while(!t.empty())
        {
            m=t.front();
            if(strcmp(m.x,y)==0) break;
            for(int i=0; i<9; i++)
                if(m.x[i]=='A')
                {
                    z=i;
                    break;
                }
            for(int i=0; i<4; i++)
            {
                strcpy(x,m.x);
                if(move[z][i]==0)  continue;
                int zz=z+dir[i];
                char temp=x[z];
                x[z]=x[zz];
                x[zz]=temp;
                int p=getkey(x);
                if(hash[p]==0)
                {
                    ss mm;
                    end++;
                    hash[p]=1;
                    mm.step=m.step+1;
                    strcpy(mm.x,x);
                    t.push(mm);
                }
            }
            t.pop();
        }
        printf("Battle array #%d :/n",cas++);
        printf("The minimum move of the battle array is : %d/n",m.step);
        printf("/n");
    }
    return 0;
}

你可能感兴趣的:(c,struct,String,input,character,each)