五点RE,紧急求助!!!
查看原帖
五点RE,紧急求助!!!
561043
足球小子楼主2022/12/24 17:04
#include<bits/stdc++.h>
using namespace std;

struct node
{
	int data,cost;
};
queue<node> q;
int n,x,s;
int cnt;
bool v[1000005];

int main()
{
	cin>>n;
	q.push((node){1,0}); 
	while(1)
	{
		x=q.front().data,s=q.front().cost;//取出队首
		
		if(v[x]||x<1||x>n)//判断是否合法
		{
			q.pop();
			continue;
		}
		
		v[x]=1;//合法标记为 1
		
		if(x==n)//如果走到,输出方案数
		{
			cout<<s;
			return 0;
		}
		
		q.push((node){x+1,s+1}),q.push((node){x-1,s+1}),q.push((node){x*2,s+1});//入队 
	}
	return 0;
}


2022/12/24 17:04
加载中...