C++题解
2024-05-20 22:15:21
发布于:四川
8阅读
0回复
0点赞
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 209;
int n, A, B, K[N], ans = -1;
bool vis[N];
void dfs (int x, int step) {
if (x == B) {
if(ans == -1) {
ans = step;
} else {
ans = min(ans, step);
}
}
if (ans != -1 && step > ans) {
return;
}
if(x + K[x] <= N && !vis[x+K[x]]) {
vis[x + K[x]] = 1;
dfs(x + K[x], step + 1);
vis[x + K[x]] = 0;
}
if(x - K[x] >= 1 && !vis[x-K[x]]) {
vis[x - K[x]] = 1;
dfs(x - K[x], step + 1);
vis[x - K[x]] = 0;
}
return;
}
int main() {
cin >> n >> A >> B;
for(int i = 1; i <= N; ++i) {
cin >> K[i];
}
dfs(A, 0);
cout << ans;
return 0;
}
这里空空如也
有帮助,赞一个