废话不多说,上代码:
#include<bits/stdc++.h>
using namespace std;
int n, a, b, ans = 0xfffff;
int to[205];
bool vis[205];
void dfs(int now, int sum) {
if (now == b) {
ans = min(ans, sum);
}
if (sum > ans) {
return;
}
vis[now] = 1;
if (now + to[now] <= n && !vis[now + to[now]]) {
dfs(now + to[now], sum + 1);
}
if (now - to[now] >= 1 && !vis[now - to[now]]) {
dfs(now - to[now], sum + 1);
}
vis[now] = 0;
}
int main() {
scanf("%d%d%d",&n,&a,&b);
for(int i=1;i<=n;i++){
cin>>to[i];
}
vis[a] = 1;
dfs(a,0);
if (ans != 0xfffff) {
printf("%d",ans);
} else {
printf("-1");
}
return 0;
}
特判无效
提交记录传送门
各位大佬行行好,帮个忙吧!