• 洛谷


    https://www.luogu.org/problemnew/show/P1379

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    
    static const int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880,3628800};   // 阶乘
    
    //康托展开
    int cantor(int *a,int n)
    {
        int code=0;
        for(int i=0;i<n;i++)
        {
            int x=0;int c=1,m=1;//c记录后面的阶乘
            for(int j=i+1;j<n;j++)
            {
                if(a[j]<a[i])x++;
                m*=c;c++;
            }
            code+=x*m;
        }
        //printf("cantor=%d
    ",code);
        return code;
    }
    
    //逆康托展开
    void decantor(int code,int *a,int n){
        bool vis[11]={};
        for(int i=0;i<n;i++){
            int r = code / fac[n-i-1];
            code %= fac[n-i-1];
            int cnt = 0;
            int j;
            for(j=1;j<=n;j++){
                if(vis[j]==0){
                    cnt++;
                    if(cnt==r+1){
                        a[i]=j;
                        vis[j]=1;
                        break;
                    }
                }
            }
        }
    
        /*printf("decantor=");
        for(int i=0;i<n;i++){
            printf(" %d",a[i]);
        }
        printf("
    ");*/
    }
    
    struct dat
    {
        int cur;
        int pre;
    } d,dt,data[362880];
    
    queue<dat> q;
    
    void show(dat d)
    {
        //cout<<"s"<<endl;
        stack<int> s;
        s.push(d.cur);
        while(1)
        {
            if(d.pre==-1)
                break;
            s.push(d.pre);
            d=data[d.pre];
        }
    
        printf("%d
    ",s.size()-1);
    }
    
    int s,t;
    
    inline bool found(int code)
    {
        return (code==t);
    }
    
    int up(int code){
        int a[9];
        decantor(code,a,9);
        for(int i=0;i<9;i++){
            if(a[i]==1){
                if(i>=3){
                    swap(a[i],a[i-3]);
                    return cantor(a,9);
                }
            }
        }
        return -1;
    }
    
    int down(int code){
        int a[9];
        decantor(code,a,9);
        for(int i=0;i<9;i++){
            if(a[i]==1){
                if(i<=5){
                    swap(a[i],a[i+3]);
                    return cantor(a,9);
                }
            }
        }
        return -1;
    }
    
    int right(int code){
        int a[9];
        decantor(code,a,9);
        for(int i=0;i<9;i++){
            if(a[i]==1){
                if(i%3!=2){
                    swap(a[i],a[i+1]);
                    return cantor(a,9);
                }
            }
        }
        return -1;
    }
    
    int left(int code){
        int a[9];
        decantor(code,a,9);
        for(int i=0;i<9;i++){
            if(a[i]==1){
                if(i%3){
                    swap(a[i],a[i-1]);
                    return cantor(a,9);
                }
            }
        }
        return -1;
    }
    
    void bfs()
    {
        memset(data,-1,sizeof(data));
    
        d.cur=s;
        d.pre=-1;
        data[d.cur]=d;
    
        if(found(d.cur))
        {
            printf("0
    ");
            return;
        }
        q.push(d);
    
        while(!q.empty())
        {
            d=q.front();
            q.pop();
    
    
            dt.cur=up(d.cur);
            if(dt.cur!=-1&&data[dt.cur].cur==-1){
                dt.pre=d.cur;
                data[dt.cur]=dt;
                if(found(dt.cur)){
                    show(dt);
                    return;
                }
                q.push(dt);
            }
    
            dt.cur=down(d.cur);
            if(dt.cur!=-1&&data[dt.cur].cur==-1){
                dt.pre=d.cur;
                data[dt.cur]=dt;
                if(found(dt.cur)){
                    show(dt);
                    return;
                }
                q.push(dt);
            }
    
            dt.cur=left(d.cur);
            if(dt.cur!=-1&&data[dt.cur].cur==-1){
                dt.pre=d.cur;
                data[dt.cur]=dt;
                if(found(dt.cur)){
                    show(dt);
                    return;
                }
                q.push(dt);
            }
    
            dt.cur=right(d.cur);
            if(dt.cur!=-1&&data[dt.cur].cur==-1){
                dt.pre=d.cur;
                data[dt.cur]=dt;
                if(found(dt.cur)){
                    show(dt);
                    return;
                }
                q.push(dt);
            }
        }
    
        cout<<"NOFOUND"<<endl;
    }
    
    int main()
    {
        int a[9]={1,2,3,8,0,4,7,6,5};
        for(int i=0; i<9; i++)
            a[i]++;
        t=cantor(a,9);
    
        for(int i=0; i<9; i++)
            scanf("%1d",&a[i]);
    
        s=cantor(a,9);
        //decantor(t,a,8);
    
        bfs();
    }
  • 相关阅读:
    Qt之添加QLabel的点击事件
    Qt之布局管理--基本布局
    Qt之界面实现技巧
    Qt之键盘讲解
    Qt之多窗口切换
    Qt之自定义信号和槽函数
    Qt之重写QLabel类
    mysql学习(四)-字段类型
    mysql学习(三)
    mysql 复习与学习(二)数据库及表结构的创建删除
  • 原文地址:https://www.cnblogs.com/Yinku/p/10542811.html
Copyright © 2020-2023  润新知