1444. 交换【推荐】

Description

  给定1到N的一个排列,再给定一些允许的交换方法,要求用最少的交换次数把该排列变为1,2,3,,,N。

Input

  第一行包含两个整数N(1<=N<=12)和M(1<=M<=N*(N-1)/2),表示序列的长度以及允许的交换方案。
  第二行输入1到N个初始排列情况。
  接下来M行,每行两个整数A和B描述一个允许的交换方案,表示允许把当前排列中的第A个数和第B个数进行交换,保证A和B不相同。
  输入保证一定存在解。

Output

  输出一个整数表示最少需要的交换次数。

Sample Input

输入1:
2 1
2 1
1 2

输入2:
3 2
2 1 3
1 3
2 3

输入3:
5 5
1 2 3 4 5
1 5
2 5
1 4
1 1
3 5

Sample Output

输出1:
1

输出2:
3

输出3:
0

Solution

这道题其实直接双向BFS+哈希判重就可以了。

Code1

#include<cstdio>
using namespace std;
long long d1[500010][2],d2[500010][2],h1[1000010][2],h2[1000010][2],st,s,t,ss;
int a[100],b[100],c[22],mo=1000007;
int hash1(long long x){
    int y=(x-1)%mo+1;
    while(h1[y][1]>0&&h1[y][1]!=x) y=y%mo+1;
    return y;
}
int hash2(long long x){
    int y=(x-1)%mo+1;
    while(h2[y][1]>0&&h2[y][1]!=x) y=y%mo+1;
    return y;
}
int main(){
    int n,m,x,y;
    scanf("%d%d",&n,&m);
    st=1;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        s+=x*st;st*=13;
    }
    st=1;
    for(int i=1;i<=n;i++){t+=i*st;st*=13;}
    for(int i=1;i<=m;i++) scanf("%d%d",&a[i],&b[i]);
    int l1=0,l2=0,r1=1,r2=1;
    d1[1][1]=s;d1[1][2]=0;
    y=hash1(s);h1[y][1]=s;
    d2[1][1]=t;d2[1][2]=0;
    y=hash2(t);h2[y][1]=t;
    while(l1<r1||l2<r2){
        if(l1<r1){
            l1=(l1+1)%500000;
            c[0]=0;
            while(d1[l1][1]>0){c[++c[0]]=d1[l1][1]%13;d1[l1][1]/=13;}
            for(int i=1;i<=m;i++){
                x=c[a[i]];c[a[i]]=c[b[i]];c[b[i]]=x;
                st=1,s=0;
                for(int j=1;j<=n;j++){s+=c[j]*st;st*=13;}
                r1=(r1+1)%500000;
                d1[r1][1]=s;d1[r1][2]=d1[l1][2]+1;
                y=hash1(s);
                if(h1[y][1]==0){h1[y][1]=s;h1[y][2]=d1[r1][2];}
                else r1--;
                y=hash2(s);
                if(h2[y][1]!=0){printf("%lld",d1[r1][2]+h2[y][2]);return 0;}
                x=c[a[i]];c[a[i]]=c[b[i]];c[b[i]]=x;
            }
        }
        if(l2<r2){
            l2=(l2+1)%500000;
            c[0]=0;
            while(d2[l2][1]>0){c[++c[0]]=d2[l2][1]%13;d2[l2][1]/=13;}
            for(int i=1;i<=m;i++){
                x=c[a[i]];c[a[i]]=c[b[i]];c[b[i]]=x;
                st=1,s=0;
                for(int j=1;j<=n;j++){s+=c[j]*st;st*=13;}
                r2=(r2+1)%500000;
                d2[r2][1]=s;d2[r2][2]=d2[l2][2]+1;
                y=hash2(s);
                if(h2[y][1]==0){h2[y][1]=s;h2[y][2]=d2[r2][2];}
                else r2--;
                y=hash1(s);
                if(h1[y][1]!=0){printf("%lld",d2[r2][2]+h1[y][2]);return 0;}
                x=c[a[i]];c[a[i]]=c[b[i]];c[b[i]]=x;
            }
        }
    }
    return 0;
}

Code2

#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#define fo(i,x,y) for (int i=x;i<=y;++i)
using namespace std;

typedef long long LL;
struct node{
    LL x;
    int y;
};

const int maxn=15, INF=1010580540;

typedef int St[maxn];

int n,m,f[maxn][maxn];
St a;

priority_queue<node> q;
map<LL,int> F;

bool operator <(node x,node y){ return x.y>y.y; }

int H(LL a){
    int ret=F[a];
    for (int i=n;i;--i) ret+=f[i-1][a%n], a/=n;
    return ret;
}

LL Stnum(St a){
    LL ret=0;
    fo(i,1,n) ret=ret*n+a[i];
    return ret;
}
void numSt(LL a,St b){
    for (int i=n;i;--i) b[i]=a%n, a/=n;
}

int Astar(LL st,LL ed){
    F[st]=0;
    node v={st,H(st)};
    while (F.find(ed)==F.end()){
        numSt(v.x,a);
        fo(i,1,n)
            fo(j,i+1,n){
                if (f[i-1][j-1]!=1) continue;
                a[i]^=a[j], a[j]=a[i]^a[j], a[i]^=a[j];
                LL nxt=Stnum(a);
                if (F.find(nxt)==F.end()){
                    F[nxt]=F[v.x]+1;
                    node now={nxt,H(nxt)};
                    q.push(now);
                }
                a[i]^=a[j], a[j]=a[i]^a[j], a[i]^=a[j];
            }
        v=q.top(); q.pop();
    }
    return F[ed];
}

int main(){
    scanf("%d%d",&n,&m);
    fo(i,1,n){ scanf("%d",&a[i]); --a[i]; }
    memset(f,60,sizeof(f));
    fo(i,1,m){
        int x,y; scanf("%d%d",&x,&y); --x, --y;
        f[x][y]=f[y][x]=1;
    }
    fo(i,0,n-1) f[i][i]=0;
    fo(k,0,n-1)
        fo(i,0,n-1)
            fo(j,0,n-1)
                f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
    int ed[maxn];
    fo(i,1,n) ed[i]=i-1;
    printf("%d\n",Astar(Stnum(a),Stnum(ed)));
    return 0;
}

作者:zsjzliziyang
QQ:1634151125
转载及修改请注明
本文地址:https://blog.csdn.net/zsjzliziyang/article/details/81805117

猜你喜欢

转载自blog.csdn.net/zsjzliziyang/article/details/81805117