zoukankan      html  css  js  c++  java
  • 最少交换次数

    bfs,折半搜索,因为直接搜大概有(12)^13?因为每个状态都会扩展出m种状态大概是(12)^13,然而可以折半搜索,只搜一半,状态数变成(12)^7可以接受,但是事实上极限数据要跑很长很长时间,据说正解是启发式搜索?没学过

    #include<iostream>
    #include<map> 
    #include<set>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    string S,tar;
    map<string, int> mp;
    map<string, int> mp1;
    queue<string>q;
    queue<string>temp;
    int n,m,p;
    int a[30],b[30];
    ll Hash(string s)
    {
        ll ret=0;
        for(int i=1;i<=n;i++) {ret*=171;ret+=a[i];}
        return ret; 
    }
    
    bool bfs()
    {
        q.push(S);
        mp[S]=0;
        while(!q.empty())
        {
            string s = q.front();
            int step = mp[s];
            q.pop();
            for(int i=1;i<=m;i++)
            {
                swap(s[a[i]],s[b[i]]);
                if(!mp.count(s)) {
                    if(s==tar) {printf("%d
    ",step+1);return false;}
                    temp.push(s);mp[s] = step + 1;
                }
                swap(s[a[i]],s[b[i]]);
            }
        }
        return true;
    }
    
    void bfs1()
    {
        q.push(tar); 
        mp1[tar] = 0;
        while(!q.empty())
        {
            string s=q.front();q.pop();int step=mp1[s];
            for(int i=1;i<=m;i++)
            {
                swap(s[a[i]],s[b[i]]);
                if(mp.count(s)) 
                {
                    printf("%d
    ",mp[s]+step+1);
                    return;
                }
                if(!mp1.count(s)) 
                {    
                    q.push(s);
                    mp1[s]=step+1;
                }
                swap(s[a[i]],s[b[i]]);
            }
        }
    }
    int main()
    {
        
        cin>>n>>m;
        for(int i=1;i<=n;i++) 
        {
            cin>>p;
            S+=(char)(p+'a');
            tar+=(char)(i+'a');
        }
    //    cout<<S<<'
    '<<tar<<'
    ';
        for(int i=1;i<=m;i++) cin>>a[i]>>b[i],a[i]--,b[i]--;    
        if(S==tar) {printf("0
    ");return 0;}
        if(bfs()) bfs1(); 
        return 0;
    }
  • 相关阅读:
    2008北京奥运男子100M决赛黑人的竞技场
    Bit,Bytes,KB,MB,GB,TB,PB,EB,ZB,YB
    请教:TableLayoutPanel.Controls.Add中的下一个可行的单元位置(.net2.0,C#)
    ORACLE获取表所占用空间大小计算和展示
    C#中创建对象的方式
    一个怕360的病毒
    ubuntu以root权限登录
    安装ubootmkimage
    安装配置armlinuxgcc
    移动虚拟机注意问题
  • 原文地址:https://www.cnblogs.com/19992147orz/p/6034474.html
Copyright © 2011-2022 走看看