天天看點

P1135 奇怪的電梯

P1135 奇怪的電梯

a == b這種情況要記得判斷

#include <bits/stdc++.h>
using namespace std;

int n,a,b;
int d[210][210];
int main(){
    freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    cin >> n >> a >> b;
    if(a == b){
        cout << 0;
        return 0;
    }
    memset(d,0x3f3f3f3f, sizeof(d));
    for(int i = 1; i <= n; i++){
        int x;cin >> x;
        if(i > x) d[i][i - x] = 1;
        if(i + x <= n) d[i][i + x] = 1;
    }
    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(d[i][j] > d[i][k] + d[k][j])
                    d[i][j] = d[i][k] + d[k][j];
            }
        }
    }
    if(d[a][b] == 0x3f3f3f3f) cout << "-1";
    else cout << d[a][b];
    return 0;
}
           

floyed