求调悬关
查看原帖
求调悬关
813812
jinzhaoming楼主2024/4/4 09:19

代码可以正确的让子串变化,可是为什么次数没有+1呢

献上我可爱的Code:

#include <bits/stdc++.h>
using namespace std;
string a,b,a1[7],b1[7];
int n=1;
struct node
{
	string x;
	int y;
};
queue<node> q;
void bfs()
{
	q.push({a,0});
	while(!q.empty())
	{
		node h=q.front();
		if(h.y>10)
		{
			cout<<"NO ANSWER!";
			return ;
		}
		for(int i=1;i<=n;i++)
		{
			int p=h.x.find(a1[i],0);
			while(true)
			{
				if(p==-1)
					break;
				string c=h.x.replace(p,a1[i].size(),b1[i]);
				q.push({c,h.y+1});
				cout<<h.x<<" "<<h.y<<endl;
				if(c==b)
				{
					cout<<h.y+1;
					return ;
				}
				p=h.x.find(a1[i],p+1);
			}
		}
		q.pop();
	}
}
int main()
{
	cin>>a>>b;
	while(cin>>a1[n]>>b1[n])
		n++;
	n--;
	bfs();
	return 0;
}

调好给(第一个调好的)关注

2024/4/4 09:19
加载中...