神奇的事
  • 板块灌水区
  • 楼主monologues
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/7/13 21:59
  • 上次更新2024/7/14 08:48:49
查看原帖
神奇的事
1362554
monologues楼主2024/7/13 21:59

差不多的代码,数组同样开的10的6次方,为啥我同学对了,我错了!!QAQ 蒟蒻求助

https://www.luogu.com.cn/problem/B3626

下面是我的代码

#include<bits/stdc++.h>
using namespace std;
int n,v[1000001];
struct S{
	int x;
	int step;
}s1,s2;
queue<S>q;
void bfs()
{
	while(!q.empty())q.pop();
	v[1]=1;
	q.push({1,0});
	while(!q.empty())
	{
		 s1=q.front();
		 q.pop();
		 if(s1.x==n)
		 {
		   cout<<s1.step;
		   return;
		 }
		 if(v[s1.x+1]==0&&s1.x+1>0&&s1.x+1<=n)
		 {
		   v[s1.x+1]=1;
		   q.push({s1.x+1,s1.step+1});
		 }
		 if(v[s1.x-1]==0&&s1.x-1>0&&s1.x-1<=n)
		 {
		   v[s1.x-1]=1;
		   q.push({s1.x-1,s1.step+1});
		 }
		 if(v[s1.x*2]==0&&s1.x*2>0&&s1.x*2<=n)
		 {
		   v[s1.x*2]=1;
		   q.push({s1.x*2,s1.step+1});
		 }
	}
}
int main()
{
	cin>>n;
	bfs();
	return 0;
}
2024/7/13 21:59
加载中...