奇怪的电梯

#include<bits/stdc++.h>
using namespace std;
int k[201];
int h[10001];//队列
int p[10001];//记录按第几次
int c[201];//标记是否走过
int n,a,b;
int bfs()
{
int head=0,tail=1;
h[1]=a;
p[a]=0;
c[a]=1;
do
{
head++;
if(h[head]==b)
{
cout<<p[head];
return 0;
}
int up=h[head]+k[h[head]];
int down=h[head]-k[h[head]];
if(up<=n&&!c[up])
{
tail++;
h[tail]=up;
p[tail]=p[head]+1;
c[up]=1;
}
if(down>=1&&!c[down])
{
tail++;
h[tail]=down;
p[tail]=p[head]+1;
c[down]=1;
}
}while(head<tail);
cout<<-1;
return 0;
}
int main()
{
cin>>n>>a>>b;
for(int i=1;i<=n;i++)
cin>>k[i];
bfs();
}

上一篇:分数相加and化简——PAT (Advanced Level) 1081 Rational Sum (20 分)


下一篇:《算法笔记》知识点总结