• HUST 1605 Gene recombination


    简单广搜。4进制对应的10进制数来表示这些状态,总共只有(4^12)种状态。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 15;
    bool m[20000000];
    struct P
    {
        int state;
        int tot;
    };
    queue<P>Q;
    char s1[maxn], s2[maxn];
    int tmp1[maxn], r1, tmp2[maxn], r2;
    int A, B;
    int n;
    int b[maxn];
    
    int f(char sign)
    {
        if (sign == 'A') return 0;
        if (sign == 'T') return 1;
        if (sign == 'G') return 2;
        if (sign == 'C') return 3;
    }
    
    void init()
    {
        while (!Q.empty()) Q.pop();
        memset(m, 0, sizeof m); A = B = 0;
        for (int i = 0; s1[i]; i++) A = A + f(s1[i])*b[i];
        for (int i = 0; s2[i]; i++) B = B + f(s2[i])*b[i];
    }
    
    void BFS()
    {
        P now; now.state = A; now.tot = 0; m[A] = 1; Q.push(now);
        while (!Q.empty())
        {
            P head = Q.front(); Q.pop();
            if (head.state == B)
            {
                printf("%d
    ", head.tot);
                break;
            }
    
            memset(tmp1, 0, sizeof tmp1);
            memset(tmp2, 0, sizeof tmp2);
    
            r2 = r1 = 0; int w = head.state;
            while (w) tmp2[r2++] = tmp1[r1++] = w % 4, w = w / 4;
    
            swap(tmp1[0], tmp1[1]);
            int new_state = 0;
            for (int i = 0; i < n; i++) new_state = new_state + tmp1[i] * b[i];
    
            if (m[new_state] == 0)
            {
                m[new_state] = 1;
                P d; d.state = new_state; d.tot = head.tot + 1;
                Q.push(d);
            }
    
            new_state = 0;
            tmp2[n] = tmp2[0];
            for (int i = 1; i <= n; i++) new_state = new_state + tmp2[i] * b[i - 1];
    
            if (m[new_state] == 0)
            {
                m[new_state] = 1;
                P d; d.state = new_state; d.tot = head.tot + 1;
                Q.push(d);
            }
        }
    }
    
    int main()
    {
        b[0] = 1; for (int i = 1; i <= 11; i++) b[i] = 4 * b[i - 1];
    
        while (~scanf("%d", &n))
        {
            scanf("%s%s", s1, s2);
            init();
            BFS();
        }
        return 0;
    }
  • 相关阅读:
    POJ
    POJ-2253 Frogger(最短路)
    背包问题(转自背包九讲+对应题目)
    POJ-1860 Currency Exchange (最短路)
    Bellman-Ford 最短路径算法
    POJ-3295 Tautology (构造)
    POJ-2586 Y2K Accounting Bug 贪心
    POJ 2965 The Pilots Brothers' refrigerator (暴力枚举)
    python(pymysql操作数据库)
    python复习概念__oop中
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5249312.html
Copyright © 2020-2023  润新知