Light 1039 - A Toy Company (bfs)

題意

給一個初始串和目標串,一次只能變換相鄰的字母,問最少變換幾次,有一些禁止變換的排列。

思路

因爲總共才3個字母,所以可以直接用數字表示一個單詞。

然後處理一下禁止的單詞,丟set裏。

然後就開始bfs。

這題的思路很簡單,不過寫得很蛋疼。。不知道爲什麼。把狀態push到隊列的時候的寫法換了很多種才寫出來。。

這題也可以建圖跑最短路。不過感覺跑不跑都差不多,因爲邊權都是1.

代碼

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
#define TRAVERSAL(u, i) for (int i = head[u]; i != -1; i = edge[i].nxt)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e7+10;
const int MOD = 1e9+7;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef pair<int, int> pii;

int arr[3], vis[26*26*26];
queue<int> Q;
string start, target;
set<int> mp;

void dfs(int u, int tar, int sum, int cnt)
{
    if (sum > 26*26*26) return;
    if (u == 3)
    {
        if (!vis[sum] && !mp.count(sum))
        {
            vis[sum] = cnt+1;
            Q.push(sum);
        }
        return;
    }
    if (u == tar)
    {
        dfs(u+1, tar, sum*26 + (arr[u]+1)%26, cnt);
        dfs(u+1, tar, sum*26 + (arr[u]-1+26)%26, cnt);
    }
    else dfs(u+1, tar, sum*26 + arr[u], cnt);
}

void Push(int u, int cnt)
{
    arr[2] = u % 26; u /= 26;
    arr[1] = u % 26; u /= 26;
    arr[0] = u;
    for (int i = 0; i <= 2; i++)        //變換第i個字母
        dfs(0, i, 0, cnt);
}

int Hash(char a, char b, char c)
{
    int ret = 0;
    ret = a-'a'; ret *= 26;
    ret += b-'a'; ret *= 26;
    ret += c-'a';
    return ret;
}

void bfs()
{
    int ed = Hash(target[0], target[1], target[2]);
    Q.push(Hash(start[0], start[1], start[2]));
    while (!Q.empty())
    {
        int u = Q.front(); Q.pop();
        if (mp.count(u)) continue;
        if (u == ed) { printf("%d\n", vis[u]); return; }
        Push(u, vis[u]);
    }
    printf("-1\n");
}

int main()
{
    //ROP;
    int T;
    scanf("%d", &T);
    while (T--)
    {
        printf("Case %d: ", ++cases);
        mp.clear();
        while (!Q.empty()) Q.pop();
        MS(vis, 0);
        cin >> start >> target;
        int n;
        scanf("%d", &n);
        while (n--)
        {
            string a, b, c;
            cin >> a >> b >> c;
            FOR(i, 0, SZ(a)) FOR(j, 0, SZ(b)) FOR(k, 0, SZ(c)) mp.insert(Hash(a[i], b[j], c[k]));
        }
        bfs();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章