#include<iostream>
#include<queue>
#include<memory.h>
#include<stdio.h>
using namespace std;
#define MAX 200100
bool flag[MAX];
struct Node
{
int x,step;
Node()
{
x=step=0;
}
};
int BFS(Node start,int goal)
{
if(start.x==goal)
return 0;
queue<Node> Q;
Q.push(start);
while(!Q.empty())
{
Node now=Q.front();
Q.pop();
if(now.x==goal)
{
return now.step;
}
Node temp;
temp.step=now.step+1;
temp.x=now.x+1;
if(temp.x<MAX&&!flag[temp.x])
{
Q.push(temp);
flag[temp.x]=true;
}
temp.x=now.x-1;
if(temp.x>=0&&temp.x<MAX&&!flag[temp.x])
{
Q.push(temp);
flag[temp.x]=true;
}
temp.x=now.x*2;
if(temp.x<MAX&&!flag[temp.x])
{
Q.push(temp);
flag[temp.x]=true;
}
}
}
int main(int argc,char *argv[])
{
//freopen("input.txt","r",stdin);
int N,K;
while(scanf("%d%d",&N,&K)!=EOF)
{
Node start;
start.x=N;
start.step=0;
memset(flag,false,sizeof(flag));
printf("%d\n",BFS(start,K));
}
return 0;
}
POJ 3278 Catch That Cow[BFS]
最新推荐文章于 2024-07-24 11:36:30 发布