zoukankan      html  css  js  c++  java
  • hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497

    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
     


    for( i=0;i<8;i++)
                pos[ch1[i]-'0']=i+1+'0';
            for( i=0;i<8;i++)
                des[i]=pos[str[i]-'0‘];

    s=cantor(des);

    上面一段代码的意思是:把起始目标看成了1,2,3,4,5,6,7,8  ;

    列如:位置: 12345678                 12345678

               起初: 63728145       变      12345678

               终点: 86372541       成       51234876

    解释一下:初:6在第1个位,那么在终点中找6用1代替,3在第2个位,在终点中找3用2代替,依次类推。

    一开始我们就先按 12345678 这样的顺序建立了一棵像树一样的,如果直接从初态不进行转变的话,那么我们的结果可能有很多的走法,有可能是先走A或B都可以到目标,有多条路时,但是先走了B的路径,必须要输出小的也就是从A开始的那条路,那怎么办呢,就可以用转化的思想了,把初始状态变成12345678,这样的话,我们一开始就是从这样的顺序算出来的!!所以必须先进行转换,在从目标往上找并记下路径,一直找到最终父节点:12345678.

    #include<stdio.h>
    #include<iostream>
    #include<queue>
    #include<stack>
    #include<string.h>
    using namespace std;
    typedef struct nn
    {
        char ch[9];
        int son;
    
    }node1;
    typedef struct n
    {
        char way;//记录从父节点到当前节点的走法
        int father;//记录当前节点的父节点的位置
    }node2;
    node2 Node[40321];//节点
    int fac[8]={1,1,2,6,24,120,720,5040};//阶层
    int cantor(char s[])//计算s在康托展开的位置,每一个都是独一无二的
    {
        int i,j,k,ans=0;
        for(i=0;i<8;i++)
        {
            k=0;
            for(j=i+1;j<8;j++)
            if(s[i]>s[j])
                k++;
            ans+=k*fac[7-i];
        }
        return ans;
    }
    void BFS(char ch1[])
    {
        queue<node1>Q;
        node1 now,next;
        int i,son,e=0;
        char tc;
        son=cantor(ch1);
        strcpy(now.ch,ch1);now.ch[8]='';
        now.son=son;Node[son].father=0;//最终父节点为本本身
        Q.push(now);
        while(!Q.empty())
        {
            now=Q.front(); Q.pop();
            //printf("%s ",now.ch);if(e==300)getchar();e++;用于调试
            next=now;
            for(i=0;i<4;i++) {tc=next.ch[i];next.ch[i]=next.ch[7-i];next.ch[7-i]=tc; }
            son=cantor(next.ch);   next.son=son;
            if(Node[son].father==-1)
            {
                Node[next.son].father=now.son; Node[next.son].way='A';
                Q.push(next);
            }
    
            next=now;
            tc=next.ch[3];for(i=3;i>0;i--) next.ch[i]=next.ch[i-1];   next.ch[0]=tc;
            tc=next.ch[4];for(i=4;i<7;i++) next.ch[i]=next.ch[i+1];   next.ch[7]=tc;
            son=cantor(next.ch);   next.son=son;
            if(Node[son].father==-1)
            {
                Node[next.son].father=now.son; Node[next.son].way='B';
                Q.push(next);
            }
    
            next=now;
            tc=next.ch[5];next.ch[5]=next.ch[2];next.ch[2]=next.ch[1];next.ch[1]=next.ch[6];next.ch[6]=tc;
            son=cantor(next.ch);   next.son=son;
            if(Node[son].father==-1)
            {
                Node[next.son].father=now.son; Node[next.son].way='C';
                Q.push(next);
            }
        }
    }
    int main()
    {
        int c,s,i;
        char str[9];
        char ch1[9]={'1','2','3','4','5','6','7','8'};
            for(i=0;i<40321;i++)
            Node[i].father=-1;
    
            BFS(ch1);
        char Way[10000];
        while(cin>>ch1>>str)
        {
            char pos[10];
            char des[8];
            for( i=0;i<8;i++)//转换
                pos[ch1[i]-'0']=i+1+'0';
            for( i=0;i<8;i++)
                des[i]=pos[str[i]-'0'];
            s=cantor(des);
            i=0;
            while(0!=s)//从目标到初太搜索路径
            {
                Way[i++]=Node[s].way;//printf("7 ");
                s=Node[s].father;
            }
            while(i--)//输出从初态到目标的路径
            {
                printf("%c",Way[i]);
            }
            printf("
    ");
        }
    }


    /*
    63728145
    86372541
    ACBBBCBBCBCBCABB

    下面是一步一步按顺序从队列中取300出来的:
    12345678 87654321 41236785 17245368 58763214 82754631 34127856 48136275 86354271
     41723685 16745238 65872143 51863724 13645728 58276314 83254761 23418567 3542718
    6 57263184 34812756 47836125 58632714 24176853 48123765 41672385 76581432 645728
    13 42736815 65187243 52163874 41367285 75823146 51876234 58327614 26318457 68172
    453 23541867 38527416 65721843 13487562 35412876 34781256 35867142 51832674 7241
    8536 25476183 56732184 24817653 46823175 74163852 48172635 73681542 31827546 764
    58132 61472583 34278156 86512437 64587123 65218743 64132857 48167325 27581463 74
    523816 43267815 75182346 53176824 25836147 51827364 75481362 12634578 25618347 7
    6814532 42358671 26341587 23854167 26578431 64521783 81345627 16387452 67821453
    13548762 37512486 83472561 35481726 63581427 34567812 47623815 35186742 57132864
     73218456 38167452 72541836 28576413 35671842 12486537 25417863 24681753 6741852
    3 75463182 53627184 74816352 43872165 24518637 87365421 74381652 23185467 576413
    28 73658412 76145832 73421568 35478216 18654372 83612547 32178546 86451237 62487
    513 16527438 64518273 36418572 65432187 52376184 64813257 42867135 26781543 6183
    2547 27458163 71423586 64328157 87513462 74582136 75318246 32581476 24536817 463
    72815 25183647 56127834 87543621 31265784 17234658 12563478 17685324 73614852 54
    236718 47258361 78514362 42635871 28641357 52381674 26354817 72654318 23678541 3
    8712546 26457831 68421573 82145367 25478361 81634527 15687342 26784531 41357628
    16348572 13754862 78345612 86372451 62718453 83547261 62381547 21876543 63458127
     31467582 24768153 83517426 34586172 35718642 65481237 17324568 63814527 7324158
    6 72854136 73568421 34571682 81245376 13286457 36871452 12548637 26517483 824675
    31 25481673 28136457 67541823 78563412 25361847 17483526 75416832 12456378 68734
    215 82765341 87436521 82314675 26385147 45763281 52741638 21485637 57364128 7135
    8642 47618325 73645182 27345681 76321458 61287453 73542168 31578426 17854632 745
    21638 18365472 84312657 73215468 58642371 83651427 86245137 21654387 13627548 37
    281546 16452738 37618452 78123456 36541872 68532417 75231846 16482573 65413827 6
    4281357 34518762 82675431 36185472 26758413 27145863 26431578 65428317 18754623
    86713542 63128547 87451362 73482516 17532468 74518326 71863542 32458176 21436587
     74638152 82516473 24583167 48756213 82743561 63127845 38165274 85643271 3172658
    4 15734268 61254783 17263548 81763245 12785634 25841637 17368524 75314682 514362
    78 16385274 54723618 46758231 17853624 34268715 47235681 42863571 85236741 57281
    364 71845362 52638174 71254638 14587632 72365418 24378651 13875462 52648317 2365
    7481 26845731 76354128 48213675 72543618 82134657 81563427 82675314 23684751 541
    36287 42157368 27584361 41635728 17648352 51378624 16354782 15427368




    */


  • 相关阅读:
    Tomcat学习笔记(九)
    在Windows上编译最新的CURL,含有zlib,openssl
    【转】测试LibreOffice SDK 开发环境配置(Windows)
    [转]LibreOffice-SDK 开发实战:嵌入MFC-View 和 C# Winform
    C++内存分配及变长数组的动态分配
    C++ 取得系统当前时间
    C# Winform程序获取外网IP地址
    【转】让Chrome化身成为摸鱼神器,利用Chorme运行布卡漫画以及其他安卓APK应用教程
    tesseract-ocr 出现 错误 Please make sure the TESSDATA_PREFIX environment variable is set to the parent d irectory of your "tessdata" directory.解决方案
    使用adb 查询data/data下的数据库
  • 原文地址:https://www.cnblogs.com/pangblog/p/3253538.html
Copyright © 2011-2022 走看看