1 条题解

  • 0
    @ 2025-7-26 10:49:03
    #include<bits/stdc++.h>
    using namespace std;
    int n,a,b,k[205],vis[205];
    struct node{
    	int f,s;
    };
    queue<node> q;
    int main(){
    	cin >> n >> a >> b;
    	for(int i=1;i<=n;i++) cin >> k[i];
    	q.push({a,0});
    	vis[a]=1;
    	while(!q.empty()){
    		node now = q.front();
    		q.pop();
    		if(now.f == b){
    			cout << now.s;
    			return 0;
    		}
    		for(int i=-1;i<=1;i+=2){
    			int nx = now.f+i*k[now.f];
    			if(nx<1||nx>n||vis[nx]) continue;
    			vis[nx]=1;
    			q.push({nx,now.s+1}); 
    		}
    	}
    	cout << -1;
    	return 0;
    }
    
    

    信息

    ID
    5193
    时间
    1000ms
    内存
    128MiB
    难度
    5
    标签
    递交数
    87
    已通过
    40
    上传者