zoukankan      html  css  js  c++  java
  • Light OJ 1039

    题目链接

    http://www.lightoj.com/volume_showproblem.php?problem=1039

    题目大意

    两个字符串,一个是初始串,一个是目标串,都有且只有三个字符,初始化可以通过改变其中一个字母来变化,每次改变是将这个字母加一或者减一,问至少需要多少次才能变为目标串。变化过程中有一些串是禁止出现的。
    

    解题思路

    简单的BFS
    

    代码如下

     #include<bits/stdc++.h>
    using namespace std;
    
    const int N = 27;
    char bg[5], ed[5];
    int n;
    
    struct node
    {
        set<char> a, b, c;
    };
    node arr[57];
    
    struct element
    {
        int a, b, c;
        int t;
    };
    
    bool visited[N][N][N];
    
    bool judge(element &s)
    {
        for(int i=0; i<n; ++ i)
        {
            if(arr[i].a.count(s.a) && arr[i].b.count(s.b) && arr[i].c.count(s.c))
                return true;
        }
        return false;
    }
    
    int bfs()
    {
        memset(visited, false, sizeof(visited));
    
        element h;
        h.a = bg[0] - 'a', h.b = bg[1] - 'a', h.c = bg[2] - 'a', h.t = 0;
        queue<element> que;
        que.push(h);
        visited[h.a][h.b][h.c] = true;
        while(!que.empty())
        {
            h = que.front();
            que.pop();
    
            if(judge(h))
                continue;
    
            if(h.a == ed[0] - 'a' && h.b == ed[1] - 'a' && h.c == ed[2] - 'a')
                return h.t;
    
            element g;
    
            g.a = (h.a + 25) % 26, g.b = h.b, g.c = h.c, g.t = h.t + 1;
            if(visited[g.a][g.b][g.c] == false)
            {
                visited[g.a][g.b][g.c] = true;
                que.push(g);
            }
            g.a = (h.a + 1) % 26, g.b = h.b, g.c = h.c, g.t = h.t + 1;
            if(visited[g.a][g.b][g.c] == false)
            {
                visited[g.a][g.b][g.c] = true;
                que.push(g);
            }
            g.a = h.a, g.b = (h.b + 25) % 26, g.c = h.c, g.t = h.t + 1;
            if(visited[g.a][g.b][g.c] == false)
            {
                visited[g.a][g.b][g.c] = true;
                que.push(g);
            }
            g.a = h.a, g.b = (h.b + 1) % 26, g.c = h.c, g.t = h.t + 1;
            if(visited[g.a][g.b][g.c] == false)
            {
                visited[g.a][g.b][g.c] = true;
                que.push(g);
            }
            g.a = h.a, g.b = h.b, g.c = (h.c + 25) % 26, g.t = h.t + 1;
            if(visited[g.a][g.b][g.c] == false)
            {
                visited[g.a][g.b][g.c] = true;
                que.push(g);
            }
            g.a = h.a, g.b = h.b, g.c = (h.c + 1) % 26, g.t = h.t + 1;
            if(visited[g.a][g.b][g.c] == false)
            {
                visited[g.a][g.b][g.c] = true;
                que.push(g);
            }
        }
        return -1;
    }
    
    void solve(int cases)
    {
        scanf(" %s %s", bg, ed);
        scanf("%d", &n);
        char a[N], b[N], c[N];
    
        for(int i=0; i<n; ++ i)
        {
            arr[i].a.clear();
            arr[i].b.clear();
            arr[i].c.clear();
    
            scanf(" %s %s %s", a, b, c);
            for(int j=0; a[j]; ++ j)
                arr[i].a.insert(a[j] - 'a');
            for(int j=0; b[j]; ++ j)
                arr[i].b.insert(b[j] - 'a');
            for(int j=0; c[j]; ++ j)
                arr[i].c.insert(c[j] - 'a');
        }
        printf("Case %d: %d
    ", cases, bfs());
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        for(int i=1; i<=t; ++ i)
            solve(i);
        return 0;
    }
    
  • 相关阅读:
    BigInteger实现除法取余
    BigDecimal介绍及BigDecimal实现四舍五入
    Jenkins的凭证管理
    Adapter(适配器)-类对象结构型模式
    singleton(单件)-对象创建型模式
    ProtoType(原型)-对象创建型模式
    Factory Method(工厂方法)-对象创建型模式
    Builder(生成器)-对象创建型模式
    Abstract Factory(抽象工厂)模式
    设计模式-序
  • 原文地址:https://www.cnblogs.com/aiterator/p/6478554.html
Copyright © 2011-2022 走看看