6 条题解

  • 0
    @ 2025-3-30 20:31:38
    int main() {
        int n, a, b;
        cin >> n >> a >> b;
        vector<int> nums(n + 1);
        for (int i = 1; i <= n; i++) {
            cin >> nums[i];
        }
    
        vector<bool> visited(n + 1, false);
        queue<State> q;
        q.push(State(a, 0));
        visited[a] = true;
    
        while (!q.empty()) {
            State current = q.front();
            q.pop();
    
            if (current.floor == b) {
                cout << current.presses << endl;
                return 0;
            }
    
            int up = current.floor + nums[current.floor];
            int down = current.floor - nums[current.floor];
    
            if (up <= n && !visited[up]) {
                q.push(State(up, current.presses + 1));
                visited[up] = true;
            }
            if (down >= 1 && !visited[down]) {
                q.push(State(down, current.presses + 1));
                visited[down] = true;
            }
        }
        cout << -1 << endl;
        return 0;
    }
    

    信息

    ID
    85
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    776
    已通过
    174
    上传者