题解
2023-08-26 08:57:43
发布于:广东
3阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
struct edge{
string s;
int step;
};
int main()
{
int n;
string a, b, x[30], y[30];
queue<edge> q;
set<string> st;
cin >> a >> b;
n = 0;
while (cin >> x[++n] && cin >> y[n]);
q.push({a, 0}); st.insert(a);
while (!q.empty()){
string now = q.front().s;
int step = q.front().step;
if (now == b){
cout << step;
return 0;
}
if (step == 10) continue;
for (int i = 1; i <= n; i++){
string::size_type found = -1;
while (1){
found = now.find(x[i], found+1);
if (found == string::npos) break;
string nxt = now;
nxt.erase(found, x[i].size());
nxt.insert(found, y[i]);
if (st.find(nxt) != st.end()) continue;
st.insert(nxt);
q.push({nxt, step+1});
}
}
q.pop();
}
cout << "NO ANSWER!";
return 0;
}
这里空空如也
有帮助,赞一个