社区讨论

第五个点出错了,求助!

P1032[NOIP 2002 提高组] 字串变换(疑似错题)参与者 3已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
6 条
当前快照
1 份
快照标识符
@lodntsrm
此快照首次捕获于
2023/10/31 09:38
2 年前
此快照最后确认于
2023/11/07 00:18
2 年前
查看原帖
CPP
#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;
} 

回复

6 条回复,欢迎继续交流。

正在加载回复...