1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| x1,y1,x2,y2=RR() n,m,l=RR()
a=[] b=[]
for _ in range(m): tmp=RLS() a.append([tmp[0],int(tmp[1])]) for _ in range(l): tmp=RLS() b.append([tmp[0],int(tmp[1])])
i,j=0,0 total=ans=0
vec={'L','R'} hor={'U','D'}
dirs={'D':(1,0),'U':(-1,0),'L':(0,-1),'R':(0,1)}
while total<n:
d1,d2=a[i][0],b[j][0] if a[i][1]==b[j][1]: step=a[i][1] a[i][1]=b[j][1]=0 i+=1 j+=1 elif a[i][1]<b[j][1]: step=a[i][1] b[j][1]-=a[i][1] i+=1 else: step=b[j][1] a[i][1]-=b[j][1] j+=1 if x1==x2 and y1==y2 and d1==d2:ans+=step elif x1==x2 and abs(y1-y2)&1==0 and abs(y1-y2)<=2*step: if y1<y2 and d1=='R' and d2=='L': ans+=1 elif y1>y2 and d1=='L' and d2=='R': ans+=1 elif y1==y2 and abs(x1-x2)&1==0 and abs(x1-x2)<=2*step: if x1>x2 and d1=='U' and d2=='D': ans+=1 elif x1<x2 and d1=='D' and d2=='U': ans+=1 elif ((d1 in hor and d2 in vec) or (d1 in vec and d2 in hor)) and abs(x1-x2)==abs(y1-y2) and abs(x1-x2)<=step: f=False if y1>y2: x1,x2=x2,x1 y1,y2=y2,y1 d1,d2=d2,d1 f=True if x1>x2: if (d1=='R' and d2=='D') or (d1=='U' and d2=='L'): ans+=1 else: if (d1=='R' and d2=='U') or (d1=='D' and d2=='L') : ans+=1 if f: x1,x2=x2,x1 y1,y2=y2,y1 d1,d2=d2,d1 total+=step dx,dy=dirs[d1] x1+=dx*step y1+=dy*step
dx,dy=dirs[d2] x2+=dx*step y2+=dy*step
print(ans)
|