您的位置:首页 > 运维架构

[BZOJ1646] [Usaco2007 Open]Catch That Cow 抓住那只牛

2015-10-12 22:35 519 查看

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1646

题目大意

给定两个点,从一个点走到另一个点的最小时间

每秒可以从x走到2*x,x-1或x+1

题解

裸的BFS,注意位置可以在0

var
x,t:array[0..100005]of longint;
i,j,k:longint;
n,m,head,tail,v:longint;
begin
for i:=0 to 100000 do x[i]:=1000000000;
readln(n,m);
t[1]:=n; head:=1; tail:=2; x
:=0;
while head<tail do
begin
v:=t[head]; inc(head);
if (v*2<=100000)and(x[v]+1<x[v*2]) then begin x[v*2]:=x[v]+1; t[tail]:=v*2; inc(tail); end;
if (v+1<=100000)and(x[v]+1<x[v+1]) then begin x[v+1]:=x[v]+1; t[tail]:=v+1; inc(tail); end;
if (v-1>=0)and(x[v]+1<x[v-1]) then begin x[v-1]:=x[v]+1; t[tail]:=v-1; inc(tail); end;
end;
writeln(x[m]);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: