88分求调
查看原帖
88分求调
1443372
angziqwq楼主2024/10/22 20:56
#include <iostream>
#include <queue>
using namespace std;
int n,a,b;
int k[210];
int lou[210],ans[210];
int t,t1,t2,m;//t为当前楼层,t1为向上走的楼层,t2为向下走的楼层 
queue<int>q;
int main()
{
	cin>>n>>a>>b;
	if (a==b)
	{
		cout<<0<<endl;
		return 0;
	}
	q.push(a);
	lou[a]=1;
	for (int i=1;i<=n;i++)
	{
		cin>>k[i];
		ans[i]=114514;
	}
	for (int i=1;i<=n;i++)
	{
		t1=i+k[i];
		t2=i-k[i];
		cout<<i<<' '<<t1<<' '<<t2<<endl;
	}
	ans[a]=0;
	while(!q.empty())
    {
        t=q.front();
        q.pop();
        //if (k[t]==0)continue;
        t1=t+k[t];
        t2=t-k[t];
        cout<<t<<' '<<t1<<' '<<t2<<endl;
        if (t1<=n)
        {
            if(ans[t1]!=114514)
            {
                if(ans[t1] < ans[t] + 1)
                    continue;
                else
                {
                    ans[t1] = ans[t] + 1;
                    q.push(t1);
                }  
            }
            else
            {
                ans[t1] = ans[t] + 1;
                q.push(t1);
            }  
        } 
        if (t2>=1)
        {
            if(ans[t2]!=114514)
            {
                if(ans[t2] < ans[t] + 1)
                    continue;
                else
                {
                    ans[t2] = ans[t] + 1;
                    q.push(t2);
                }  
            }
            else
            {
                ans[t2] = ans[t] + 1;
                q.push(t2);
            }    
        } 
    }
    if(ans[b]!=114514)
        {
            cout<<ans[b]<<endl;
            return 0;
        }
    cout<<-1<<endl;
    return 0;
}
/*5 1 5
3 3 1 2 5*/
2024/10/22 20:56
加载中...