差不多的代码,数组同样开的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;
}