您的位置:首页 > 其它

HDU 1430 魔板

2013-04-23 11:30 375 查看


魔板

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1192 Accepted Submission(s): 241



Problem Description

在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板。魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示。任一时刻魔板的状态可用方块的颜色序列表示:从魔板的左上角开始,按顺时针方向依次写下各方块的颜色代号,所得到的数字序列即可表示此时魔板的状态。例如,序列(1,2,3,4,5,6,7,8)表示魔板状态为:

1 2 3 4

8 7 6 5

对于魔板,可施加三种不同的操作,具体操作方法如下:

A: 上下两行互换,如上图可变换为状态87654321

B: 每行同时循环右移一格,如上图可变换为41236785

C: 中间4个方块顺时针旋转一格,如上图可变换为17245368

给你魔板的初始状态与目标状态,请给出由初态到目态变换数最少的变换步骤,若有多种变换方案则取字典序最小的那种。



Input

每组测试数据包括两行,分别代表魔板的初态与目态。



Output

对每组测试数据输出满足题意的变换步骤。



Sample Input

12345678
17245368
12345678
82754631




Sample Output

C
AC




Author

LL



Source

ACM暑期集训队练习赛(三)



Recommend

linle

好长时间没有做搜索了,各种搓啊

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#define LL long long
#define DB double

using namespace std;
const int N = 40329;
struct nod{
    int v[8];
    void out(){for(int i=0;i<8;i++) cout<<v[i]<<" ";cout<<endl;}
};
int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};
int contor(nod &t)
{
    int i,j,tmp,num=0;
    for(i=0;i<8;i++)
    {
        tmp = 0;
        for(j=i+1;j<8;j++)
        if(t.v[j]<t.v[i]) tmp++;
        num+=fac[7-i]*tmp;
    }return num;
}
int pre
;
queue<nod> que;
void op1(nod &t)
{
    swap(t.v[0],t.v[7]);
    swap(t.v[1],t.v[6]);
    swap(t.v[2],t.v[5]);
    swap(t.v[3],t.v[4]);
}
void op2(nod &t)
{
    swap(t.v[3],t.v[2]);
    swap(t.v[2],t.v[1]);
    swap(t.v[1],t.v[0]);
    swap(t.v[4],t.v[5]);
    swap(t.v[5],t.v[6]);
    swap(t.v[6],t.v[7]);
}
void op3(nod &t)
{
    swap(t.v[1],t.v[6]);
    swap(t.v[6],t.v[5]);
    swap(t.v[5],t.v[2]);
}
string ans
;
void init()
{
    nod e,t;
    memset(pre,0,sizeof(pre));
    pre[0] = 1;
    for(int i=0;i<8;i++) e.v[i] = i+1;
    que.push(e);
    int k;
    while(!que.empty())
    {
        e = que.front(); que.pop();
        int f = contor(e);
        t = e;
        op1(t);
        k = contor(t);
        if(pre[k]==0)
        {
            ans[k] = ans[f]+"A";
            pre[k] = 1;
            que.push(t);
        }
        t = e;
        op2(t);
        k = contor(t);
        if(pre[k]==0)
        {
            ans[k] = ans[f]+"B";
            pre[k] = 1;
            que.push(t);
        }
        t = e;
        op3(t);
        k = contor(t);
        if(pre[k]==0)
        {
            ans[k] = ans[f]+"C";
            pre[k] = 1;
            que.push(t);
        }
    }
    //for(int i=0;i<8;i++) cout<<e.v[i]<<" ";cout<<endl;
}
int s[10];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    init();
    //ans[0]="AA";
    char a[10],b[10];
    nod t;
    while(~scanf("%s%s",a,b))
    {
        for(int i=0;i<8;i++)
        {
            s[a[i]-'0'] = i+1;
        }
        for(int i=0;i<8;i++)
        {
            t.v[i] = s[b[i]-'0'];
        }
        //t.out();
        int k = contor(t);
        cout<<ans[k]<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: