电梯问题pascal 在线等!急

来源:百度知道 编辑:UC知道 时间:2024/06/30 17:26:51
有一幢大楼,共有N层。为了便利乘坐电梯,规定每架电梯除停顶层和底层外,还可停K层,试求至少需要多少架电梯才能使每两层之间有直达电梯

var n,i,j,l,a,b,p:longint;
lift,step:array[0..200] of longint;
begin
read(n,a,b);
for i:=1 to n do read(lift[i]);
for i:=1 to n do step[i]:=-1;
step[a]:=0;
p:=0; l:=1;
while (l>0) and (step[b]=-1) do
begin
l:=0;
for i:=1 to n do
if step[i]=p then
begin
j:=i-lift[i];
if (j>0) and (step[j]=-1) then
begin
step[j]:=p+1;
l:=l+1;
end;
j:=i+lift[i];
if (j<=n) and (step[j]=-1) then
begin
step[j]:=p+1;
l:=l+1;
end;
end;
p:=p+1;
end;
write(step[b]);
end.

换个

1架就够了,可以找电梯厂家设定停层数