http://tjuacm.chaosheng.top/problem.php?id=1285
https://www.luogu.com.cn/problem/P1135
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 210;
int n, a, b;
int k[N];
bool visit[N];
int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};
struct pos{
int x;
int cnt;
};
int bfs(int x){
memset(visit, false, sizeof(visit));
queue<pos> q;
pos f, v;
f.x = x;
f.cnt = 0;
visit[f.x] = true;
q.push(f);
while(!q.empty()){
f = q.front();
q.pop();
if(f.x == b) return f.cnt;
for(int i = 0; i < 2; i++){
if(i == 0){
v.x = f.x + k[f.x-1];
}else{
v.x = f.x - k[f.x-1];
}
if(v.x > 0 && v.x <= n && !visit[v.x]) {
visit[v.x] = true;
v.cnt = f.cnt + 1;
q.push(v);
}
}
}
return -1;
}
int main(){
cin >> n >> a >> b;
for(int i = 0; i < n; i++){
cin >> k[i];
}
int cnt = bfs(a);
printf("%d\n", cnt);
return 0;
}
京公网安备 11010502036488号