第五个点出错了,求助!
查看原帖
第五个点出错了,求助!
220362
chenxuanting楼主2020/6/20 19:09
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
string st,ed;
string a[10],b[10];
map<string,int> apr;//appear v.出现 
int num,ans=1000;
struct node
{
	int step;
	string cur;
};
void bfs()
{
	queue<node> q;
	q.push((node){0,st});
	apr[st]=5;
	while(!q.empty()){
		node x=q.front();
		q.pop();
		if(x.step>10){
			break;
		}
		if(x.cur==ed){
			ans=x.step;
			break;
		}
		for(int i=1;i<=num;i++){
			if(x.cur.find(a[i])!=string::npos){
				string shadow=x.cur;
				shadow.replace(x.cur.find(a[i]),a[i].size(),b[i]);
				if(apr.count(shadow)==1){
					continue;
				}else{
					apr[shadow]=5;
				}
				q.push((node){x.step+1,shadow});
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>st>>ed;
	for(int i=1;i<=6;i++){
		cin>>a[i]>>b[i];
		if(a[i]==""){
			break;
		}
		num++;
	}
	bfs();
	if(ans==1000){
		cout<<"NO ANSWER!";
	}else{
		cout<<ans;
	}
	return 0;
} 
2020/6/20 19:09
加载中...