需要注意的是走过的点进行标注就不再继续走下去。否则会造成错无,同时需要判断这点在不在0-200000的范围内,并且判断条件在该点是否走过了的的前面。
ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<ctype.h>
#include<stack>
#include<math.h>
#include <string>
#include<algorithm>
using namespace std;
struct node
{
int x;
int mint;
}cat;
const int MAXN = 100000;
const int MAXN2 = MAXN * 2;
bool book[MAXN2+2];
int n,k,mini;
void bfs()
{
queue<node> q;
cat.mint=0;
cat.x=n;
book[n]=true;
q.push(cat);
while(!q.empty())
{
node fro;
fro=q.front();
q.pop();
if(fro.x==k)
{
mini=fro.mint;
break;
}
node a,b,c;
a.x=fro.x+1;
a.mint=fro.mint+1;
if(a.x<=200000&&!book[a.x])
{
q.push(a);
book[a.x]=true;
}
b.x=fro.x-1;
b.mint=fro.mint+1;
if(b.x>=0&&!book[b.x])
{
q.push(b);
book[b.x]=true;
}
c.x=fro.x*2;
c.mint=fro.mint+1;
if(c.x<=200000&&!book[c.x])
{
q.push(c);
book[c.x]=true;
}
}
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(book,false,sizeof(book));
bfs();
cout<<mini<<endl;
}
return 0;
}