洛谷 - P1379 - 八数码难题 - bfs

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\n",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("\n");*/
}

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\n",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\n");
        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();
}

 

posted @ 2019-03-16 16:25  韵意  阅读(163)  评论(0编辑  收藏  举报