58分求助
查看原帖
58分求助
1030865
letusgo楼主2024/1/29 12:01
#include<bits/stdc++.h>
using namespace std;
int n,A,B;
int ans = 1e9;
int a[220];
bool f0 = false;
struct node{
	bool up;
	bool down;
}f[220];
void dfs(int step, int cnt){
//	cout << step << " " << cnt << endl; 
	if(cnt >= ans) return;
	if(step == B){
		ans = min(ans,cnt);
		f0 = true;
	} 
	if(step+a[step] <= n && !f[step].up){
		f[step].up = true;////
//		cout << step+a[step] << " " << cnt << endl; 
		dfs(step+a[step], cnt+1);
	}
	if(step-a[step] >= 1 && !f[step].down){
		f[step].down = true;////
//		cout << step-a[step] << " " << cnt << endl;
		dfs(step-a[step], cnt+1);
	}
}
int main(){
	scanf("%d%d%d",&n,&A,&B);
	for(int i = 1; i <= n; i++){
		scanf("%d",&a[i]);
	}
	dfs(A,0);
	if(f0) cout << ans;
	else cout << -1;
	return 0;
}
2024/1/29 12:01
加载中...