601 BFS练习1 BFS [代码源][namomo spring camp]每日一题div2

在这里插入图片描述
链接

//acmer mxc
#include<bits/stdc++.h>
#define mst(s,x) memset(s,x,sizeof(s));
#define sr(x) scanf("%d",&x); 
#define sr2(a,b) scanf("%d%d",&a,&b); 
#define sr3(a,b,c) scanf("%d%d%d",&a,&b,&c); 
#define f(i,a,n) for(int i=a;i<=n;i++)
#define sc(x) cout<<#x<<" : "<<x<<endl;
#define hh cout<<endl;
//#define int __int128
#define pii pair<int,int>
#define pb(x) push_back(x)
#define mk(a,b) make_pair(a,b)
#define ls(x) x<<1
#define rs(x) x<<1|1
#define fi first
#define se second
#define ll __int128
#define inf 0x3f3f3f3f
using namespace std;
inline ll read()
{
	ll x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') x=(x<<1)+(x<<3)+c-'0',c=getchar();
	return w==1?x:-x;
}
inline void write(ll x)
{
	if(x>=10) write(x/10);
	putchar(x%10+'0');
}
//------------------------------------------------
// #define int long long
// #define int __int128
#define LL long long
#define dd double
const int N=1e6+5;
const int mod=1e9+7;
vector<int>v;
int n,l;
int ans[N];
int vis[N];
void go_dp(int s)
{
	queue<int>q;
	q.push(s);
	ans[s]=0;
	vis[s]=1;
	while(!q.empty())
	{
		int top=q.front();
		q.pop();
		if(top<1||top>1e5)continue;
		if(!vis[top+1])
		{
			vis[top+1]=1;
			ans[top+1]=ans[top]+1;
			q.push(top+1);
		}
		if(!vis[top*2])
		{
			vis[top*2]=1;
			ans[top*2]=ans[top]+1;
			q.push(top*2);
		}
		if(!vis[top*3])
		{
			vis[top*3]=1;
			ans[top*3]=ans[top]+1;
			q.push(top*3);
		}
		if(!vis[top-1])
		{
			vis[top-1]=1;
			ans[top-1]=ans[top]+1;
			q.push(top-1);
		}
	}
	for(int i=0;i<v.size();i++)
	{
		printf("%d%s",ans[v[i]],(i==l)?"":" ");
	}
}
inline void solve()
{
	sr2(n,l);
	for(int i=1;i<=l;i++)
	{
		int t;
		sr(t);
		v.push_back(t);
	}
	go_dp(n);
}

//------------------------------------------------
signed main()
{
	clock_t c1=clock();
#ifdef LOCAL
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
//==================================
    int T=1;
	// cin>>T;
	for(int i=1;i<=T;i++)
	{
		solve();
	}
//==================================
end:
    cerr<<endl<< "Time used:" << clock() - c1 << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

while WA er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值