MLE#5求调
查看原帖
MLE#5求调
1068513
ZHUSITAOcccccc楼主2025/8/3 20:35
#include <bits/stdc++.h>
using namespace std;
struct node {
    string s;
    int step;
};
string A, B, a[10], b[10], now, chnow, str;
int n, steps, k;
queue<node> q;
map<string, bool> vis;
void bfs() {
    q.push({A, 0});
    while (!q.empty()) {
        now = q.front().s;
        steps = q.front().step;
        q.pop();
        if (vis[now] || steps > 10)
            continue;
        if (now == B) {
            cout << steps;
            return ;
        }
        for (int i(1); i <= n; ++i) {
            chnow = now;
            while (true) {
                k = chnow.find(a[i]);
                if (k == -1)
                    break;
                str = now;
                str.replace(k, a[i].size(), b[i]);
                q.push({str, steps + 1});
                chnow[k] = ' ';
            }
        }
    }
    puts("NO ANSWER!");
}
int main() {
	cin >> A >> B;
    while (cin >> a[++n] >> b[n]);
    n--;
    bfs();
	return 0;
}

请大佬们帮帮忙。

2025/8/3 20:35
加载中...