跳到正文

个人记录

78

Joeger约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月18日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int n,m,vis[10000000];
void bfs(int x)
{
	queue<int>q;
	q.push(x);
	while(q.size())
	{
		int t=q.front();
		q.pop();
		if(t==m)
		{
			cout<<vis[t];
			return;
		}
		int o=t+1;
		if(o<=m && vis[o]==0)
		{
			vis[o]=vis[t]+1;
			q.push(o);
		}
		o=t-1;
		if(o>=0 && vis[o]==0)
		{
			vis[o]=vis[t]+1;
			q.push(o);
		}
		o=t*2;
		if(o<=200000 && vis[o]==0)
		{
			vis[o]=vis[t]+1;
			q.push(o);
		}
	}
}
int main()
{
	cin>>n>>m;
	vis[n]=0;
	bfs(n);
}
原文