#9,#10寄了,求大佬帮忙
查看原帖
#9,#10寄了,求大佬帮忙
383781
UT_MC_wuming楼主2023/1/20 11:36
#include <bits/stdc++.h>
using namespace std;
int n, a, b, k[205], ans = INT_MAX;
bool vis[205];
void dfs(int depth, int cnt) {
	if (depth == b) {
		ans = min(cnt, ans);
		vis[depth] = false;
		return;
	}
	if (cnt > ans)return;
	vis[depth] = true;
	if (depth + k[depth] <= n && !vis[depth + k[depth]])dfs(depth + k[depth], cnt + 1);
	else if (depth - k[depth] >= 1 && !vis[depth - k[depth]])dfs(depth - k[depth], cnt + 1);
	vis[depth] = false;
}
int main() {
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++) {
		cin >> k[i];
	}vis[a] = true;
	dfs(a, 0);
	if (ans != INT_MAX)cout << ans;
	else cout << -1;
	return 0;
}
2023/1/20 11:36
加载中...