【求调】【玄关】20pts,4WA
查看原帖
【求调】【玄关】20pts,4WA
466596
MorningStarCzy楼主2025/1/2 20:50

rt.

#include<bits/stdc++.h>
#include<string>
using namespace std;
int as,n,ans[100005];
bool vis[100005];
struct point{
	int val,step;
};
queue<point>Q;
//tint:string->int;
//ts:int->string
string ts(int n)
{
	string s="";
	while(n)
	{
		s=char(n%10+'0')+s;
		n/=10;
	}
	return s;
}
int tint(string s)
{
	int n=0;
	for(int i=0;i<(int)s.size();i++)
		n=n*10+s[i]-'0';
	return n;
}
void bfs()
{
	vis[as]=1;
	Q.push({as,0});
	while(!Q.empty())
	{
		point now=Q.front();Q.pop();
		string s=ts(now.val);
		ans[now.val]=now.step;
//		cout<<now.val<<" ";
//		if(Q.front().step!=now.step) cout<<endl;
		int l=s.size();
		string str;
		if(s.size()!=1)
		{
			for(int i=0;i<l;i++)
			{
				str=s;str.erase(i,1);
				if(tint(str)>=100000||vis[tint(str)]||str.size()>ts(as).size()) continue;
				vis[tint(str)]=1,Q.push({tint(str),now.step+1});
			}
		}
		for(int i=0;i<l-1;i++)
			for(int j=i+1;j<l;j++)
			{
				str=s;swap(str[i],str[j]);
				if(tint(str)>=100000||vis[tint(str)]||str.size()>ts(as).size()) continue;
				vis[tint(str)]=1,Q.push({tint(str),now.step+1});
			}
		for(int i=0;i<l-1;i++)
		{
			for(char j=s[i]+1;j<s[i+1];j++)
			{
				str=s;str.insert(str.begin()+i,j);
				if(tint(str)>=100000||vis[tint(str)]||str.size()>ts(as).size()) continue;
				vis[tint(str)]=1,Q.push({tint(str),now.step+1});
			}
		}
	}
}
void Solve()
{
	cin>>n;
	cout<<ans[n]<<endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int T;cin>>as>>T;
	memset(ans,-1,sizeof(ans));
	bfs();
//	for(int i=1;i<=10;i++) cout<<ans[i]<<endl;
	while(T--) Solve(); 
	return 0;
}
2025/1/2 20:50
加载中...