啊啊啊!0pts求调
查看原帖
啊啊啊!0pts求调
1058091
Kingsley_qu楼主2024/7/23 14:59
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

int vis[2000005];
int n;

void bfs()
{
	queue<int> q;
	q.push(n);
	vis[n] = 0;
	while (!q.empty())
	{
		int now = q.front();
		q.pop();
		if (now == n) return;
		int nxt = now + 1;
		if (1 <= nxt && nxt <= n && vis[nxt] == -1)
		{
			q.push(nxt);
			vis[nxt] = vis[now] + 1;
		}
		nxt = now - 1;
		if (1 <= nxt && nxt <= n && vis[nxt] == -1)
		{
			q.push(nxt);
			vis[nxt] = vis[now] + 1;
		}
		nxt = now * 2;
		if (1 <= nxt && nxt <= n && vis[nxt] == -1)
		{
			q.push(nxt);
			vis[nxt] = vis[now] + 1;
		}
	}
}
int main()
{
	cin >> n;
	memset(vis, -1, sizeof vis);
	bfs();
	cout << vis[n];
	return 0;
}

必关必关!!!

2024/7/23 14:59
加载中...