36pts悬棺求条
查看原帖
36pts悬棺求条
959512
worldcat楼主2025/1/19 13:52
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n, a, b, k[205], steper = 114514;
ll vis[205];
void dfs(ll x, ll sum) {//x:当前楼层
	if (vis[x]) return ;
	vis[x] = 1;
	if (x == b) {
		steper = (steper > sum ? sum : steper);
	}
	if (x - k[x] >= 1) {
		dfs(x - k[x], sum + 1);
	}
	if (x + k[x] <= n) {
		dfs(x + k[x], sum + 1);
	}
} 
int main() {
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++) {
		cin >> k[i];
	}
	dfs(a, 0);
	if (steper == 114514) {
		cout << -1;
	} else {
		cout << steper;
	}
	return 0;
}

记录

2025/1/19 13:52
加载中...