# 題目

AP325 Q-7-5. 闖關路線

# 解題思路

目前沒東西...

# 程式碼

#include <bits/stdc++.h>
using namespace std;
#define N 1000005
int a[N];
bool visit[N];
int main(){
    int n,p,l,r;
    cin>>n>>p>>l>>r;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(a[i]<0 || a[i]>=n)
            a[i]=n;
    }
    queue<pair<int,int>> q;
    q.push({0,0});//{now,dis}
    visit[0]=1;
    int buffer;
    while(!q.empty() && q.front().first!=p){
        auto v=q.front();
        q.pop();
        buffer=v.first-l;
        if(buffer>=0 && a[buffer]!=n && visit[a[buffer]]!=1){
            q.push({a[buffer],v.second+1});
            visit[a[buffer]]=1;
        }
        buffer=v.first+r;
        if(buffer<n && a[buffer]!=n && visit[a[buffer]]!=1){
            q.push({a[buffer],v.second+1});
            visit[a[buffer]]=1;
        }
    }
    cout<<q.front().second;
    return 0;
}
更新於 閱讀次數 次