help me
查看原帖
help me
1299520
HYR00000楼主2024/11/29 18:45
#include<bits/stdc++.h>
using namespace std;

const int N=1e8;
int n,vis[N],ans[N];
string dl="+-*";
int op(int x,char s){
	int y=x;
	if(s=='+'){
		y+=1;
	}
	if(s=='-'){
		y-=1;
	}
	if(s=='*'){
		y*=2;
	}
	return y;
}
void bfs(int l){
	
	queue<int>q;
	q.push(l);
	vis[1]=1;
	ans[1]=0;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=0;i<3;i++){
			int tl=op(u,dl[i]);
			if(vis[l]==1&&tl>=1&&tl<=n){
				vis[l]=1;
				q.push(l);
				ans[l]=min(ans[u]+1,ans[l]);
			}
		}
	}
}
int main(){
	
	cin>>n;
	for(int i=0;i<=n;i++){
		ans[i]=INT_MAX;
	}
	bfs(1);
	cout<<ans[n];
}
2024/11/29 18:45
加载中...