奇怪的电梯
这个电梯真的好奇怪哦,这种电梯怕不是要急死快要迟到的人qwqwq
#include#include #include #include #include #include #define MAXN 10100using namespace std;int N,A,B;int G[MAXN];int vis[MAXN];bool book;struct item{ int floor; int time;};item r;queue - q;void bfs(item t){ q.push(t); while(!q.empty()) { r=q.front(); vis[r.floor]=1; q.pop(); if(r.floor==B) { book=1; break; } int a=r.floor+G[r.floor]; if(vis[a]==0&&a<=N) { item t2; t2.floor=a; t2.time=r.time+1; q.push(t2); } a=r.floor-G[r.floor]; if(vis[a]==0&&a>=1) { item t2; t2.floor=a; t2.time=r.time+1; q.push(t2); } } }int main(){ scanf("%d%d%d",&N,&A,&B); for(int i=1;i<=N;i++) { scanf("%d",&G[i]); } item t; t.floor=A; t.time=0; bfs(t); if(book) { printf("%d",r.time); } else printf("-1"); return 0;}