52pts,求调
查看原帖
52pts,求调
615931
Qianmo_su楼主2024/9/30 13:42
#include <bits/stdc++.h>
using namespace std;

const int N = 205;
int g[N];
int n,a,b;
int ans;
bool vis[N];

void bfs()
{
    queue<int> q;
    q.push(a);
    while(!q.empty())
    {
        int t = q.front();
        vis[t] = true;
        q.pop();
        if(t == b) return ;
        if((g[t]+t)>=1 && (g[t]+t)<=n && !vis[g[t]+t]) {q.push(g[t]+t);ans++;}
        if((g[t]-t)>=1 && (g[t]-t)<=n && !vis[g[t]-t]) {q.push(g[t]-t);ans++;}
    }
    ans = -1;
}

int main()
{
    memset(vis,false,sizeof(vis));
    cin >> n >> a >> b;
    //建图
    for(int i=1;i<=n;i++)
    {
        int t;
        cin >> t;
        g[i] = t;
    }
    bfs();
    cout << ans;
    return 0;
}
2024/9/30 13:42
加载中...