20pts 求调
查看原帖
20pts 求调
681313
cute_overmind楼主2024/2/29 21:18
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int pos , step;
};
const int N = 1e6 + 10;
#define int long long
int n;
int a[N];
bool v[N];
queue <node>q;
void bfs(int x)
{
	q.push((node){x , 0});
	v[x] = 1;
	while(!q.empty())
	{
		node top = q.front();
		q.pop();
		if(top.pos == n)
		{
			cout << top.step;
			exit(0);
		}
		if(top.pos + 1 <= n && v[top.pos + 1] == 0)
		{
			v[top.pos + 1] = 1;
			q.push((node){ top.pos + 1 , top.step + 1});
		}
		if(top.pos - 1 >= 0 && v[top.pos - 1] == 0)
		{
			v[top.pos - 1] = 1;
			q.push((node){ top.pos - 1 , top.step + 1});
		}
		if(top.pos * 2 <= 200000 && v[top.pos * 2] == 0)
		{
			v[top.pos * 2] = 1;
			q.push((node){ top.pos * 2 , top.step + 1});
		}
	}
}
signed main()
{
	
	cin >> n;
	bfs(1);
	return 0;
}
2024/2/29 21:18
加载中...