P1135 求教 58分
查看原帖
P1135 求教 58分
1379724
liuchenjia12345楼主2025/7/20 08:48

求教:测试点

#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;

int n;
int a[2005];
int v[2005];
queue<pair<int,int>>q;

int bfs(int st,int en){
	q.push({st,0});
	while(!q.empty()){
		auto x=q.front();
		if(x.f==en)return x.s;
		q.pop();
		if(x.f+a[x.f]<=n&&!v[x.f]){
			q.push({x.f+a[x.f],x.s+1});
            v[x.f]=1;
		}
		if(x.f-a[x.f]>=1&&!v[x.f]){
			q.push({x.f-a[x.f],x.s+1});
            v[x.f]=1;
		}
	}
    return -1;
}

signed main(){
	int x,y;
	cin>>n>>x>>y;
	for(int i=1;i<=n;i++)cin>>a[i];
	cout<<bfs(x,y);
}
2025/7/20 08:48
加载中...