全部评论 1

  • 看,神搜可以过

    #include<bits/stdc++.h>
    using namespace std;
    int n,A,B;
    int ans=-1;
    int a[205];
    int vis[205];
    clock_t st,ed;
    bool check(int x){
    	return x>=1 && x <= n;
    }
    void dfs(int x,int step){
    	if(x == B){
            if(ans == -1)ans = step;
            else ans = min(ans,step);
            return;
    	}
    	if((double)(clock() - st)/CLOCKS_PER_SEC > 0.97)return;
       //if(ans!=-1 && step >= ans)return;
        int tmp = x+a[x];
        if(check(tmp) && !vis[tmp]){
            vis[tmp] = 1;
            dfs(tmp,step + 1);
            vis[tmp] = 0;
        }
        
        tmp = x-a[x];
        if(check(tmp) && !vis[tmp]){
            vis[tmp] = 1;
            dfs(tmp,step + 1);
            vis[tmp] = 0;
        }
        
        
    }
    int main(){
    	st = clock();
        cin >> n >> A >> B;
        for(int i = 1 ; i <= n ;i++)cin >> a[i];
        vis[A] = 1;
        dfs(A,0);
        cout << ans << endl;
    	return 0;
    }
    

    2024-11-30 来自 广东

    0
首页