#include<stdio.h>
#include<string.h>
int to[3]={-1,1,2};
int mark[200005];
struct
{
int x,id;
}queue[200005],st,ed,now,next;
int bfs()
{
int fro,end;
fro=end=0;
st.id=0;
queue[end++]=st;
mark[st.x]=1;
while(fro<end)
{
now=queue[fro++];
if(now.x==ed.x)
{
return now.id;
}
for(int i=0;i<3;i++)
{
if(now.x>ed.x)
{
next.x=now.x+to[0];
}
else
{
if(i<2)
{
next.x=now.x+to[i];
}
else
{
next.x=now.x*to[i];
}
}
if(next.x<0||next.x>200000||mark[next.x]==1)
{
continue;
}
next.id=now.id+1;
mark[next.x]=1;
queue[end++]=next;
}
}
}
int main()
{
while(scanf("%d%d",&st.x,&ed.x)!=EOF)
{
memset(mark,0,sizeof(mark));
printf("%d\n",bfs());
}
}
POJ3278 - Catch That Cow - 广度优先搜索
最新推荐文章于 2024-07-24 11:36:30 发布