测试点我用编译器是对的,但不知道为什么没过,求大佬指导
查看原帖
测试点我用编译器是对的,但不知道为什么没过,求大佬指导
784813
SakurajiamaMai楼主2023/4/9 21:13
#include<bits/stdc++.h>
using namespace std;
const int N=205;
int n,m,a,b,minn=0;
int mp[N];
bool vis[N];
struct node
{
    int x,step;
}now;
void dfs()
{
    queue<node>que;
    node str;
    str.x=a;
    str.step=0;
    que.push(str);
    vis[a]=true;
    while(!que.empty())
    {
        now=que.front();
        que.pop();
        node next;
        if(now.x==b)
            break;
        next.x=now.x+mp[now.x];
        if(next.x<=n&&!vis[next.x])
        {
            next.step+=1;
            vis[next.x]=true;
            que.push(next);
        }
        next.x=now.x-mp[now.x];
        if(next.x>=1&&!vis[next.x])
        {
            next.step+=1;
            vis[next.x]=true;
            que.push(next);
        }
    }
    if(now.x==b)
        cout<<now.step;
    else
        cout<<"-1";
}
int main()
{
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++)
        cin>>mp[i];
        dfs();
    return 0;
}
2023/4/9 21:13
加载中...