64pts求条
查看原帖
64pts求条
1318911
zhengziyan130312楼主2024/9/7 22:24
#include<bits/stdc++.h>
using namespace std;
int n,arr[201],a,b,cnt[201],vis[201];
void Dfs(int o,int c){
	vis[o]=1;
	if(c>=cnt[o]){
		return;
	}
//	if(c<cnt[o]){
	cnt[o]=c;
//	}
	if(arr[o]==0){
		return;
	}
	if(o+arr[o]<=n&&vis[o+arr[o]]==0){
		Dfs(o+arr[o],c+1);
	}
	if(o-arr[o]>=1&&vis[o-arr[o]]==0){
		Dfs(o-arr[o],c+1);
	}
	vis[o]=0;
}
int main(){
	cin>>n>>a>>b;
	for(int i=0;i<=200;i++){
		cnt[i]=201;
	}
	for(int i=1;i<=n;i++){
		cin>>arr[i];
	}
	Dfs(a,0);
	if(cnt[b]==201){
		cout<<-1;
		return 0;
	}
	cout<<cnt[b];
	return 0;
}

本人看不出问题呀

2024/9/7 22:24
加载中...