广搜之坐标移动
程序员文章站
2023-12-23 19:06:03
...
#include<iostream>
#include<queue>
using namespace std;
/*犯了粗心的错,注意审题*/
int n,b;
const int Kmax=5000+20;
bool vis[Kmax];
struct node{
int x,step;
node(int _x,int _step):
x(_x),step(_step){}
};
int bfs(int a)
{
queue<node> loca;
loca.push({a,0});
vis[a]=true;
node t={a,0};
while (!loca.empty()) {
t=loca.front();
loca.pop();
if(t.x==b){
return t.step;
}
if(t.x+1<=n&&(!vis[t.x+1])){
vis[t.x+1]=true;
loca.push({t.x+1,t.step+1});
}
if(t.x-1>=0&&(!vis[t.x-1])){
vis[t.x-1]=true;
loca.push({t.x-1,t.step+1});
}
if(t.x*2<=n&&(!vis[t.x*2])){
vis[t.x*2]=true;
loca.push({t.x*2,t.step+1});
}
}
return 0;
}
int main()
{
int a;
cin>>n>>a>>b;
cout<<bfs(a)<<endl;
return 0;
}