2 solutions

  • 0
    @ 2023-3-5 14:48:36
    #include <iostream>
    using namespace std;
    int w[20003];
    int n,a,b;
    int minn=0x7ffffff;
    bool ans[2003];
    bool check(int x){
    	return (x<=n&&x>=1);
    }
    void dfs(int now,int sum){
    	if(now==b) minn=min(minn,sum);
        if(sum>minn) return;
    	ans[now]=1;
    	if(check(now+w[now])&&(!ans[now+w[now]]))
    		dfs(now+w[now],sum+1);
    	if(check(now-w[now])&&(!ans[now-w[now]]))
    		dfs(now-w[now],sum+1);
    	ans[now]=0;
        return ;
    }
    int main(){
    	cin>>n>>a>>b;
    	for(int i=1;i<=n;i++){
    		cin>>w[i];
    	}
    	dfs(a,0);
    	if(minn!=0x7ffffff) cout<<minn;
    	else cout<<-1;
    	return 0;
    }

    Information

    ID
    6705
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    (None)
    # Submissions
    83
    Accepted
    9
    Uploaded By