求助大佬,调了半天没看出来错在哪,只有6分,测试样例能过。
查看原帖
求助大佬,调了半天没看出来错在哪,只有6分,测试样例能过。
1133932
crush_j楼主2024/1/6 18:31

#include <bits/stdc++.h>//c++
using namespace std;
int arr[210];
bool tpt[210];
queue<int> q;
queue<int> q1;
int main()
{
    int n,a,b,gs=0;
    bool tp;
    cin>>n>>a>>b;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    q.push(a);
    tpt[a-1]=false;
    while(1)
    { 
        int d1,d2;
        d1=q.front()+arr[q.front()-1];
        d2=q.front()-arr[q.front()-1];
        if(d1>=1&&d1<=n)
        {
            q.push(d1);
            if(tpt[d1-1]==true)
            {
                cout<<-1;
                break;
            }
            if(d1==b)
            {
                cout<<gs+1;
                break;
            }
            tpt[d1-1]=true;
        }
        if(d2>=1&&d2<=n&&d2!=d1)
        {
            q.push(d2);
            if(tpt[d2-1]==true)
            {
                cout<<-1;
                break;
            }
            if(d2==b)
            {
                cout<<gs+1;
                break;
            }
            tpt[d2-1]=true;
        }
        q.pop();
        gs++;
        if(q.empty()==true)
        {
            cout<<-1;
            break;
        }
    }
    system("pause");
    return 0;
}
2024/1/6 18:31
加载中...