zoukankan      html  css  js  c++  java
  • 【搜索】魔板问题(BFS)

                                                                 【搜索】魔板问题

    时间限制: 1 Sec  内存限制: 64 MB
    提交: 5  解决: 3
    [提交][状态][讨论版]

    题目描述

    据说能使持有者成为世界之主的上古神器隐藏在魔板空间,魔板由8个同样大小的方块组成,每个方块颜色均不相同,按顺时针方向依次写下各方块的颜色代号,例如序列(1,2,3,4,5,6,7,8)即代表图所示的魔板状态。

    对于魔板可施加三种不同的操作,分别以A,B,C标识,具体操作方法如图所示。

    对于每种可能的状态,这三种基本操作都可以使用。你要编程计算用最少的基本操作完成基本状态到特殊状态的转换,输出基本操作序列。

    输入

    只有一行,包括8个整数,用空格分开(这些整数在范围 1~8 之间),表示目标状态。

    输出

    第一行包括一个整数,表示最短操作序列的长度。  

    第二行在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出60个字符。 

    样例输入

    2 6 8 4 5 7 3 1
    

    样例输出

    7
    BCABCCB
    

    提示

    样例的输入目标状态是由BCABCCB这7步操作获得的,如图所示。

    【分析】化为一维字符串,反正也只有八个,暴力BFS。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <time.h>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define inf 0x3f3f3f3f
    #define mod 1000000007
    typedef long long ll;
    using namespace std;
    const int N = 25;
    const int MAX = 2000;
    int n,m,maxn=-1;
    int vis[N];
    string s="",anss;
    bool flag=false;
    map<string,int>p;
    struct man
    {
        string str;
        string ans;
        int step;
    };
    queue<man>q;
    void bfs()
    {
        string str="12348765";
        string ans="";
        man d;d.ans=ans;d.step=0;d.str=str;
        p[str]=1;
        q.push(d);
        while(!q.empty()){
            man t=q.front();
           // cout<<t.str<<" "<<t.step<<endl;
           // if(t.str=="68423751")printf("!!!!!");
            q.pop();
            if(t.str==s){
               if(!flag){
                anss=t.ans;flag=true;
               }
               else anss=min(anss,t.ans);
               //cout<<t.ans<<endl;system("pause");
            }
            //操作A
           string l,r;
           l=t.str.substr(0,4);r=t.str.substr(4,4);
           string str1=r+l;
           if(!p[str1]){
        p[str1]=1;
            man k;k.ans=t.ans+'A';k.step=t.step+1;k.str=str1;q.push(k);
           }
           //操作B
           string a="";
          a=a+t.str[3]+t.str[0]+t.str[1]+t.str[2]+t.str[7]+t.str[4]+t.str[5]+t.str[6];
           if(!p[a]){
            p[a]=1;
              man k;k.ans=t.ans+'B';k.step=t.step+1;k.str=a;q.push(k);
           }
            //操作C
           string f="";
           f=f+t.str[0]+t.str[5]+t.str[1]+t.str[3]+t.str[4]+t.str[6]+t.str[2]+t.str[7];
           if(!p[f]){
            p[f]=1;
             man k;k.ans=t.ans+'C';k.step=t.step+1;k.str=f;q.push(k);
           }
        }
    }
    int main(){
        int aa[10];
        for(int i=1;i<=8;i++){
            scanf("%d",&aa[i-1]);
            if(i<=4)s+=aa[i-1]+'0';
        }
        for(int i=8;i>=5;i--){
             s+=aa[i-1]+'0';
        }
        bfs();
        cout<<anss.size()<<endl<<anss<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    如何打开“USB调试”模式?
    Eclipse常用配置
    【luogu P5363】移动金币(博弈论)(DP)(数位DP)(MTT)
    【luogu P4245】【模板】任意模数多项式乘法(拆系数FFT)(MTT)
    【ybtoj高效进阶 21178】星际大战(并查集)
    【ybtoj高效进阶 21177】小小网格(杜教筛)(数论分块)(莫比乌斯反演)
    【luogu P4213】【模板】杜教筛(Sum)(数学)(整除分块)
    【luogu P6860】象棋与马(数学)(杜教筛)
    【luogu AT2376】Black and White Tree(结论)(博弈论)(二分图)
    SAM入门三题:字符串水题, LCS, P5546 [POI2000]公共串
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5743757.html
Copyright © 2011-2022 走看看