Unaccepted 100求助
查看原帖
Unaccepted 100求助
753553
Resonate楼主2024/1/25 21:41

第一点TLE https://www.luogu.com.cn/record/134136130

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a, b, k[222], ans = LONG_LONG_MAX;
bool v[222];
void dfs(int x, int s) {
    if (x >= 1 and x <= n and !v[x] and s <= ans) {
        if (x == b) {
            ans = min(ans, s);
            return;
        }
        v[x] = 1;
        dfs(x + k[x], s + 1);
        dfs(x - k[x], s + 1);
        v[x] = 0;
    }
}
signed main() {
    cin >> n >> a >> b;
    for (int i = 1; i <= n; i++) {
        cin >> k[i];
    }
    dfs(a, 0);
    if (ans == LONG_LONG_MAX) {
        cout << -1;
    } else {
        cout << ans;
    }
}
2024/1/25 21:41
加载中...