您的位置:首页 > 其它

Poj 3278 BFS(不多说话) Catch That Cow

2016-11-13 18:39 330 查看
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow
is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute

* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
----------------------------------------------------------分割线-------------------------------------------------------------------------------

var
n,k,head,tail,x:longint;
step,que:array[0..1000000] of longint;
t:array[0..1000000] of boolean;

procedure ins(x:longint);
begin
t[x]:=false;
inc(tail);
que[tail]:=x;
step[tail]:=step[head]+1;
end;
begin
read(n,k);
head:=1;tail:=1;que[1]:=n;step[1]:=0;
fillchar(t,sizeof(t),true);t
:=false;
while head<=tail do begin
x:=que[head];
if x=k then begin
writeln(step[head]);
break;
end;
if (x-1>=0) and t[x-1] then ins(x-1);
if (x+1<=100000) and t[x+1] then ins(x+1);
if (2*x<=100000) and t[2*x] then ins(2*x);
inc(head);
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: