zoukankan      html  css  js  c++  java
  • DFS:哈密顿绕行世界问题(HDU-2181)+HDU-1560 DNA sequence

    哈密顿绕行世界问题(HDU-2181)

    题解:一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市。

    裸DFS;

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=30;
    const int mod=142857;
    const int inf=0x3f3f3f3f;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int N=5e5+10;
    
    int a[maxn][maxn];
    int ans[maxn];
    int vis[maxn];
    int m,cnt;
    
    void dfs(int city,int step)
    {
        if(city==m&&step==20)
        {
            cout<<++cnt<<":  ";
            for(int i=0; i<20; i++)
             cout<<ans[i]<<" ";
            cout<<m<<endl;
            return ;
        }
        ans[step]=city;
        for(int i=1; i<=3; i++)
        {
            if(!vis[a[city][i]])
            {
                vis[a[city][i]]=1;
                dfs(a[city][i],step+1);
                vis[a[city][i]]=0;
            }
        }
    }
    
    int main()
    {
        int x,y,z;
        for(int i=1; i<=20; i++)
        {
            cin>>x>>y>>z;
            a[i][1]=x;
            a[i][2]=y;
            a[i][3]=z;
            sort(a[i],a[i]+3);
        }
        while(cin>>m&&m)
        {
            memset(vis,0,sizeof(vis));
            memset(ans,0,sizeof(ans));
            dfs(m,0);
        }
        system("pause");
        return 0;
    }

    迭代加深搜索IDA*

    HDU-1560 DNA sequence

    拼接所有字符串,问最短长度;

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=30;
    const int mod=142857;
    const int inf=0x3f3f3f3f;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int N=5e5+10;
    
    string c="ACGT";
    int n,depth;
    int t,k,maxx;
    int pos[20];//记录每个字符串已经拼接到哪一个(拼接的当前位置)
    
    struct node{
        string s;
        int len;
    }a[maxn];
    
    int get()//得到剩余序列未拼接的最长的一个长度
    {
        int ans=0;
        for(int i=0; i<n; i++)
         ans=max(ans,a[i].len-pos[i]);
        return ans;
     }
    
     int dfs(int step)
     {
         if(step+get()>depth) //预计步数超过最大值,搜索停止
           return false;
         if(!get())//已经拼完,退出
          return true;
          int vis[20];//保存pos的值
          for(int i=0; i<n; i++)
           vis[i]=pos[i];
         for(int i=0; i<4; i++)//遍历ACGT
         {
             int flag=0;
             for(int j=0; j<n; j++)
             {
                 if(a[j].s[pos[j]]==c[i])
                 {
                     flag=1;
                     pos[j]++;
                 }
             }
             if(flag)
             {
                 if(dfs(step+1))
                  return true;
                for(int j=0; j<n; j++)//复原pos的值
                 pos[j]=vis[j];
             }
         }
         return false;
     }
    
     int main()
     {
         cin>>t;
         while(t--)
         {
             depth=0;//每次一定要置零
             maxx=0;
             cin>>n;
             for(int i=0; i<n; i++)
             {
                 cin>>a[i].s;
                 a[i].len=a[i].s.length();
                 maxx=max(maxx,a[i].len);
                 pos[i]=0;
             }
             depth=maxx;//depth设初始值
             while(1)
             {
                 if(dfs(0)) break;
                 depth++;//false加深步数
             }
             cout<<depth<<endl;
         }
         system("pause");
         return 0;
     }
  • 相关阅读:
    谷歌技术"三宝"之BigTable
    谷歌技术"三宝"之谷歌文件系统
    谷歌技术"三宝"之MapReduce
    大话存储
    ASP.NET之旅--深入浅出解读IIS架构
    好网站
    ArcGIS API for Silverlight学习资料积累
    浏览器滚动条快到底部时自动加载数据
    MVC EF中Attach和Entry区别
    微信开发jssdk入门
  • 原文地址:https://www.cnblogs.com/sweetlittlebaby/p/13358137.html
Copyright © 2011-2022 走看看