70分求救!
查看原帖
70分求救!
1401647
Leaf59楼主2024/9/1 09:16
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int k[205];
bool vis[205];
struct node{
	int x,step;
}q[205];
int bfs(int x){
	
	int head=1,tail=2;
	q[1].x=1,q[1].step=0;
	vis[x]=1;
	while(head!=tail){
	
		node t=q[head++];
		int up=t.x+k[t.x],down=t.x-k[t.x];
		if(up<=n&&!vis[up]){
		
			vis[up]=1;
			q[tail].x=up;
			q[tail].step=t.step+1;
			tail++;
			if(up==b){
				return q[tail-1].step;
			}
			
		}
		if(down>=1&&!vis[down]){
			vis[down]=1;
			q[tail].x=down;
			q[tail].step=t.step+1;
			tail++;
			if(down==b){
				return q[tail-1].step;
			}
			
		}
	}
	return -1;
}
int main(){
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
	cin>>k[i];
} 
if(a==b){
	cout<<0;
	return 0;
}
cout<<bfs(a);
    return 0;
} 

前几个样例WA!

2024/9/1 09:16
加载中...