为什么会RE
查看原帖
为什么会RE
581956
lirongsheng楼主2023/8/27 21:28
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, k[210];
int d[] = {-1, 1}, f[210];
queue<int> q;
void bfs(int x, int y){
    q.push(x);
    while(q.size()){
        int xx = q.front();
        // cout << xx << endl;
        if(xx == y){
            cout << f[xx] << endl;
            return ;
        }
        q.pop();
        for(int i = 0; i < 2; i++){
            int xxx = k[xx] * d[i] + xx;
            if(xxx > 0 && xx <= n && f[xxx] == 0){
                f[xxx] = f[xx] + 1;
                q.push(xxx);
            }
        }
    }
    cout << -1 << endl;
}
int main(){
    cin >> n >> a >> b;
    for(int i = 1; i <= n; i++){
        cin >> k[i];
    }
    bfs(a, b);
    // int m = bfs(a, b);
    // if(m == 0){
    //     cout << -1 << endl;
    // }
    system("pause");
    return 0;
}

会有两个点RE,数据有一组是200 68 200底下是67个“68”,63个“67”和剩下的是“66”

把f数组和k数组扩大到300就可以了,但是题目中给的数据范围是200啊

2023/8/27 21:28
加载中...