#36866: 只須測量傳送後是否走過即可


samlin961112@gmail.com (林哲甫)

學校 : 新北市私立南山高級中學
編號 : 220506
來源 : [219.70.213.92]
最後登入時間 :
2024-05-06 16:41:02
f166. 傳送點 -- 2019年10月APCS | From: [219.70.213.92] | 發表日期 : 2023-08-13 10:22

#include <bits/stdc++.h>
using namespace std;
int n,p,l,r;

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  cin>>n>>p>>l>>r;
  vector<int> map(n,-1);
  map[0]=0;
  vector<int> s(n);
  for(int i=0;i<n;i++){
    cin>>s[i];
  }
  queue<pair<int,int>>q;
  q.push(make_pair(0,0));
  while(!q.empty()){
    int now=q.front().first;
    int t=q.front().second;
    q.pop();
    if(now==p){
      break;
    }
    if(now+r<n&&map[s[now+r]]==-1){
      q.push(make_pair(s[now+r],t+1));
      map[s[now+r]]=t+1;
    }
    if(now-l>0&&map[s[now-l]]==-1){
      q.push(make_pair(s[now-l],t+1));
      map[s[now-l]]=t+1;
    }
  }
    cout<<map[p];
}

 
ZeroJudge Forum