3 条题解
-
1
#include<bits/stdc++.h> using namespace std; const int N=200+10; const int INF=0x3f3f3f3f; struct node { int pos,step; }; int n,l,r,a[N]; bool v[N]; void bfs(int x) { queue q; q.push((node){x,0}); v[x]=1; while(!q.empty()){ node top=q.front(); q.pop(); if(top.pos==r){ cout<<top.step; exit(0); } if(top.pos+a[top.pos]<=n&&v[top.pos+a[top.pos]]==0){ v[top.pos+a[top.pos]]=1; q.push((node){top.pos+a[top.pos],top.step+1}); } if(top.pos-a[top.pos]>0&&v[top.pos-a[top.pos]]==0){ v[top.pos-a[top.pos]]=1; q.push((node){top.pos-a[top.pos],top.step+1}); } }
} int main(){ cin>>n>>l>>r; for(int i=1;i<=n;i++){ cin>>a[i]; }
bfs(l); cout<<-1; return 0;
}
信息
- ID
- 3064
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 129
- 已通过
- 48
- 上传者