大佬求救!!!
查看原帖
大佬求救!!!
1371632
KJun楼主2025/1/28 14:29

这题我想用dfs做 但是最后一个点WA了

这个是输入 输出为8 我程序输出NO ANSWER!

abaaaba abcdaba
a b
b d
d e
e f
f g
g c
#include <bits/stdc++.h>
using namespace std;
string s[6][2];
string a,b;
int i=0,min_time=114514;
void dfs(string k,int times)
{
	if(k==b)
	{
		if(min_time>times) min_time=times;
		return;
	}
	if(times>10) return;
	if(times>=min_time) return;
	for(int j=0;j<i;j++){
		int place=k.find(s[j][0]);
		if(place!=-1){
			string new_s=k;
			new_s=new_s.replace(place,s[j][0].length(),s[j][1]);
			dfs(new_s,times+1);
			place = k.find(s[j][0],place+1);
		}
	}
}
int main()
{
	cin>>a>>b;
	while(cin>>s[i][0]>>s[i][1]) i++;

	dfs(a,0);
	if(min_time!=114514) cout<<min_time;
	else cout<<"NO ANSWER!";
}

好像已经多次find了(我也不确定) 大佬求调!!!

2025/1/28 14:29
加载中...