广搜做,第三个测评输出八位乱码
#include <bits/stdc++.h>
using namespace std;
string pre[7],res[7],a,b;
int n=1;
struct node{
string s;int step;
}start;
queue<node> q;
int bfs(node x){
q.push(x);
while(!q.empty()){
node xx=q.front();
q.pop();
if(xx.s==b){
return xx.step;
}
if(xx.step>10) return 0;
for(int i=1;i<n;i++){
int pos=xx.s.find(pre[i]);
if(pos==-1) continue;
xx.s.replace(pos,pre[i].size(),res[i]);
xx.step++;
q.push(xx);
}
}
}
int main(){
cin>>a>>b;
while(cin>>pre[n]>>res[n]){
n++;
}
start.s=a;
start.step=0;
if(!bfs(start)){
cout<<"NO ANSWER!";
return 0;
}
cout<<bfs(start);
}