您的位置:首页 > 其它

jzoj 1365. 【队列练习】奇怪的电梯

2016-07-17 14:19 603 查看


题目描述

大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3 3 1 2 5代表了Ki(K1=3,K2=3,……),从一楼开始。在一楼,按“上”可以到4楼,按“下”是不起作用的,因为没有-2楼。那么,从A楼到B楼至少要按几次按钮呢?


输入

输入文件共有二行,第一行为三个用空格隔开的正整数,表示N,A,B(1≤N≤200, 1≤A,B≤N),第二行为N个用空格隔开的正整数,表示Ki。


输出

输出文件仅一行,即最少按键次数,若无法到达,则输出-1。

var
w,f:array[-10000..10000] of longint;
a,b,i,j,k,n,m,max:longint;
procedure dfs(dep,x:longint);
var
i,j,k:longint;
begin
if f[dep]=1 then exit;
if (dep<=0) or (dep>n) then exit;
if dep=b then  begin if max>x then max:=x; exit; end;
if f[dep-w[dep]]=0 then begin f[dep]:=1; dfs(dep-w[dep],x+1); f
4000
[dep]:=0; end;
if f[dep+w[dep]]=0 then begin f[dep]:=1; dfs(dep+w[dep],x+1); f[dep]:=0; end;
end;
begin
// assign(input,'lift3.in'); reset(input);
max:=maxlongint;
readln(n,a,b);
for i:=1 to n do
read(w[i]);
dfs(a,0);
if max=maxlongint then max:=-1;
writeln(max);
end.
#include <stdio.h>
using namespace std;
int n,a,b;
int state[10000],t[10000],f[10000];
bool fl=false,l[10000];
int bfs()
{
int head=0,tail=1,i,j,k;
t[1]=a; l[a]=true;
while (head<=tail)
{
head++;
if (t[head]-f[t[head]]>=1&&f[t[head]]!=0&&l[t[head]-f[t[head]]]==false)
{
tail++;
l[t[head]-f[t[head]]]=true;
t[tail]=t[head]-f[t[head]];
state[tail]=state[head]+1;
if (t[tail]==b)
{
printf("%d",state[tail]);
fl=true;
return 0;
}
//     printf("%d\n",t[tail]);
}
if (t[head]+f[t[head]]<=n&&f[t[head]]!=0&&l[t[head]+f[t[head]]]==false)
{
tail++;
l[t[head]+f[t[head]]]=true;
t[tail]=t[head]+f[t[head]];
state[tail]=state[head]+1;
if (t[tail]==b)
{
printf("%d",state[tail]);
fl=true;
return 0;
}
//  printf("%d\n",t[tail]);
}

}
}
int main()
{
scanf("%d%d%d",&n,&a,&b);
if (a==b)
{
printf("%d",0);
return 0;
}
for (int i=1;i<=n;i++)
scanf("%d",&f[i]);

bfs();

if (fl==false)
printf("%d",-1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: