样例通过,64pts,检查不出错来
上代码
#include <bits/stdc++.h>
#include <queue>
using namespace std;
bool vis[202];
int n,nw,to,k[202];
struct pos{
int x,step;
pos(int px=0,int py=0){
x=px;
step=py;
}
};
void bfs(){
queue<pos> q;
q.push(pos(1,0));
while(!q.empty()){
pos now=q.front();
if(now.x==to){
cout<<now.step;
return;
}
q.pop();
int x=now.x,step=now.step;
if(x+k[x]<=n&&vis[x+k[x]]==0){
q.push(pos(x+k[x],step+1));
vis[x+k[x]]=1;
}
if(x-k[x]>=1&&vis[x-k[x]]==0){
q.push(pos(x-k[x],step+1));
vis[x-k[x]]=1;
}
}
cout<<-1;
}
int main(){
cin>>n>>nw>>to;
if(nw==to){
cout<<0;
return 0;
}
for(int i=1;i<=n;i++)
cin>>k[i];
bfs();
return 0;
}