#37267: Python不能過:(


s11104220@school.saihs.edu.tw (施同學)

School : 臺北市立松山高級工農職業學校
ID : 221254
IP address : [118.165.10.104]
Last Login :
2023-10-21 21:29:09
f166. 傳送點 -- 2019年10月APCS | From: [118.165.0.77] | Post Date : 2023-08-29 07:29

def main():
    from sys import stdin
    from collections import deque
    n,P,L,R=map(int,stdin.readline().split())
    tel=tuple(map(int,stdin.readline().split()))
    t=deque([[0,0]])
    vis=set()
    while t:
        x,d=t.popleft()
        if x==P:print(d);break
        for i in (-L,R):
            nx=x+i
            if nx>=0 and nx<n:
                if nx not in vis:
                    vis.add(nx)
                    nnx=tel[nx]
                    if nnx>=0 and nnx<n:
                        t.append([nnx,d+1])
    else:print(-1)
if __name__=="__main__":main()
 
ZeroJudge Forum