为什么会RE啊!QAQ
查看原帖
为什么会RE啊!QAQ
657188
tdks楼主2023/3/29 16:51

救命救命,求大佬相助

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,a,b,ans=1e9,num[10000010];
bool use[10000010],flag;
void Dfs(ll cc,ll step){
	if(step>=ans)return;
	if(cc==b){
		flag=true;
		ans=min(ans,step);
		return;       
	}
	if(cc+num[cc]<=n && !use[cc+num[cc]]){
		use[cc+num[cc]]=true;
		Dfs(cc+num[cc],step+1);
		use[cc+num[cc]]=false;
	}
	if(cc+num[cc]>=1 && !use[cc-num[cc]]){
		use[cc-num[cc]]=true;
		Dfs(cc-num[cc],step+1);
		use[cc-num[cc]]=false;
	}
}
int main(){
	cin >> n >> a >> b;
	for(ll i=1;i<=n;i++){
		cin >> num[i];
	}
	Dfs(a,0);
	if(flag){
		cout << ans;
	}else{
		cout << -1;
	}
	return 0;
}
2023/3/29 16:51
加载中...