只有50分,求大佬帮助
查看原帖
只有50分,求大佬帮助
464739
冰封侠楼主2022/8/16 20:34

使用的bfs,队列进行维护,但不知道哪里出了问题,求各位大佬帮忙解决下,代码如下:

#include<cstdio>
#include<queue>
#define maxn 210
using namespace std;
int n, a, b, ans, lift[maxn], vis[maxn];
queue <int> q;
int main()
{
    scanf("%d%d%d", &n, &a, &b);
    for(int i = 1; i <= n; i++)
        scanf("%d", &lift[i]);
    q.push(a);
    if (a == b)
    {
        printf("0");
        return 0;
    }
    while(!q.empty())
    {
        int s = q.front();
        printf("%d\n", s);
        q.pop();
        if (s + lift[s] == b || s - lift[s] == b)
        {
            printf("%d", ans + 1);
            return 0;
        }
        if (s + lift[s] <= n && vis[s + lift[s]] == 0){
            vis[s + lift[s]] = 1;
            q.push(s + lift[s]);
        }
        if (s - lift[s] >= 1 && vis[s - lift[s]] == 0){
            vis[s - lift[s]] = 1;
            q.push(s - lift[s]);
        }
        ans++;
    }
    printf("-1");
    return 0;
}

2022/8/16 20:34
加载中...