大佬求调!!
查看原帖
大佬求调!!
929151
xxr___楼主2023/10/4 19:45
#include<iostream>
#include<cstring>
#include<queue> 
#include<map>
using namespace std;
map<string,string> mp;
map<string,bool> vis;
map<string,int> step;
void bfs(string s1,string s2){
	queue<string> q;
	vis[s1]=1;
	step[s1]=0;
	q.push(s1);
	while(q.size()){
		auto v=q.front();
		q.pop();
		string m="";
		string x="";
		int len=v.size();
		for(int i=0;i<len;i++){
			for(int j=i;j<len;j++){
				m+=v[j];
				if(mp[m]!=""){
					for(int l=0;l<i;l++)x+=v[l];
					x+=mp[m];
					for(int k=j+1;k<len;k++){
						x+=v[k];
					}
					cout<<x<<endl;
					if(!vis[x]){
						vis[x]=1;
						step[x]=step[v]+1;
						q.push(x);
						goto fl;
						if(x==s2){
							if(step[x]>10){
								puts("NO ANSWER!");
								exit(0);
							}else{
								cout<<step[x];
								exit(0);
							}
						}
					}
				}
			}
		}
		fl:;
	}
}
signed main(){
	string A,B;
	cin>>A>>B;
	string a,b;
	while(cin>>a>>b){
		mp[a]=b;
	}
	bfs(A,B);
	return 0;
}
2023/10/4 19:45
加载中...