Light OJ 1039 - A Toy Company (BFS)

题目链接

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\n", cases, bfs());
}

int main()
{
    int t;
    scanf("%d", &t);
    for(int i=1; i<=t; ++ i)
        solve(i);
    return 0;
}
posted @ 2017-02-28 13:51  aiterator  阅读(206)  评论(0编辑  收藏  举报