您的位置:首页 > 其它

POJ3278 - Catch That Cow - 广度优先搜索

2014-07-16 08:44 423 查看
#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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: