Problem:
 Score : 600 points    Problem Statement We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i -th row from the top and the j -th column from the left.
On this grid, there is a piece, which is initially placed at square (s_r,s_c) .  Takahashi and Aoki will play a game, where each player has a string of length N .
Takahashi's string is S , and Aoki's string is T . S and T both consist of four kinds of letters: L , R , U and D .  The game consists of N steps. The i -th step proceeds as follows:   First, Takahashi performs a move. He either moves the piece in the direction of S_i , or does not move the piece.  Second, Aoki performs a move. He either moves the piece in the direction of T_i , or does not move the piece.   Here, to move the piece in the direction of L , R , U and D , is to move the piece from square (r,c) to square (r,c-1) , (r,c+1) , (r-1,c) and (r+1,c) , respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.  Takahashi wants to remove the piece from the grid in one of the N steps.
Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid.
Determine if the piece will remain on the grid at the end of the game when both players play optimally.      Constraints  2 \leq H,W \leq 2 \times 10^5  2 \leq N \leq 2 \times 10^5  1 \leq s_r \leq H  1 \leq s_c \leq W  |S|=|T|=N  S and T consists of the four kinds of letters L , R , U and D .         Input Input is given from Standard Input in the following format:  H  W  N  s_r  s_c  S  T       Output If the piece will remain on the grid at the end of the game, print YES ; otherwise, print NO .        Sample Input 1 2 3 3
2 2
RRL
LUD      Sample Output 1 YES  Here is one possible progress of the game:   Takahashi moves the piece right. The piece is now at (2,3) .  Aoki moves the piece left. The piece is now at (2,2) .  Takahashi does not move the piece. The piece remains at (2,2) .  Aoki moves the piece up. The piece is now at (1,2) .  Takahashi moves the piece left. The piece is now at (1,1) .  Aoki does not move the piece. The piece remains at (1,1) .        Sample Input 2 4 3 5
2 2
UDRRR
LLDUD      Sample Output 2 NO       Sample Input 3 5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD      Sample Output 3 NO

Solution (Python):
def solve(W, sc, chL, chR):
    limLs, limRs = [0]*N, [0]*N
    limL, limR = 0, W+1
    for i in reversed(range(N)):
        if Ts[i] == chL:
            limR += 1
            if limR > W+1: limR = W+1
        elif Ts[i] == chR:
            limL -= 1
            if limL < 0: limL = 0
        if Ss[i] == chL:
            limL += 1
            if limL > W+1: limL = W+1
        elif Ss[i] == chR:
            limR -= 1
            if limR < 0: limR = 0
        limLs[i] = limL
        limRs[i] = limR

    posL, posR = sc, sc
    for i in range(N):
        if posR <= limLs[i] or limRs[i] <= posL:
            return False
        if Ss[i] == chL:
            posR -= 1
            if posR < 1: posR = 1
        elif Ss[i] == chR:
            posL += 1
            if posL > W: posL = W
        if Ts[i] == chL:
            posL -= 1
            if posL < 1: posL = 1
        elif Ts[i] == chR:
            posR += 1
            if posR > W: posR = W

    return True


H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
Ss = input()
Ts = input()

ansLR = solve(W, sc, 'L', 'R')
ansUD = solve(H, sr, 'U', 'D')

print('YES' if ansLR and ansUD else 'NO')

Solution (Python):
def main():
    h, w, n = map(int, input().split())
    y, x = map(int, input().split())
    s, t = input(), input()

    u, d, l, r = 1, h, 1, w

    for ss, tt in zip(s[::-1], t[::-1]):
        if tt == 'U':
            d = min(d + 1, h)
        elif tt == 'D':
            u = max(u - 1, 1)
        elif tt == 'L':
            r = min(r + 1, w)
        else:
            l = max(l - 1, 1)  

        if ss == 'U':
            u += 1
        elif ss == 'D':
            d -= 1
        elif ss == 'L':
            l += 1
        else:
            r -= 1  

        if l > r or u > d:
            print('NO')
            return

    if l <= x <= r and u <= y <= d:
        print('YES')
        return
    else:
        print('NO')
        return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    h, w, n = map(int, input().split())
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    s, t = input().rstrip(), input().rstrip()

    u, d, l, r = 0, h - 1, 0, w - 1

    for ss, tt in zip(s[::-1], t[::-1]):
        if tt == 'U':
            d = min(d + 1, h - 1)
        elif tt == 'D':
            u = max(u - 1, 0)
        elif tt == 'L':
            r = min(r + 1, w - 1)
        else:
            l = max(l - 1, 0)  

        if ss == 'U':
            u += 1
        elif ss == 'D':
            d -= 1
        elif ss == 'L':
            l += 1
        else:
            r -= 1  

        if l > r or u > d:
            print('NO')
            return

    if l <= x <= r and u <= y <= d:
        print('YES')
        return
    else:
        print('NO')
        return


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    h, w, n = map(int, input().split())
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    s, t = input(), input()

    u, d, l, r = 0, h - 1, 0, w - 1

    for ss, tt in zip(s[::-1], t[::-1]):
        if tt == 'U':
            d = min(d + 1, h - 1)
        elif tt == 'D':
            u = max(u - 1, 0)
        elif tt == 'L':
            r = min(r + 1, w - 1)
        else:
            l = max(l - 1, 0)  

        if ss == 'U':
            u += 1
        elif ss == 'D':
            d -= 1
        elif ss == 'L':
            l += 1
        else:
            r -= 1  

        if l > r or u > d:
            print('NO')
            return

    if l <= x <= r and u <= y <= d:
        print('YES')
        return
    else:
        print('NO')
        return


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    h, w, n = map(int, input().split())
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    s, t = input(), input()

    u, d, l, r = 0, h - 1, 0, w - 1

    for ss, tt in zip(s[::-1], t[::-1]):
        if tt == 'U': d = min(d + 1, h - 1)
        if tt == 'D': u = max(u - 1, 0)
        if tt == 'L': r = min(r + 1, w - 1)
        if tt == 'R': l = max(l - 1, 0)

        if ss == 'U': u += 1
        if ss == 'D': d -= 1
        if ss == 'L': l += 1
        if ss == 'R': r -= 1

        if l > r or u > d:
            print('NO')
            return

    if l <= x <= r and u <= y <= d:
        print('YES')
        return
    else:
        print('NO')
        return


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    h, w, n = map(int, input().split())
    y, x = map(int, input().split())
    y -= 1
    x -= 1
    s, t = input(), input()

    u, d, l, r = 0, h - 1, 0, w - 1

    for ss, tt in reversed(tuple(zip(s, t))):
        if tt == 'U': d = min(d + 1, h - 1)
        if tt == 'D': u = max(u - 1, 0)
        if tt == 'L': r = min(r + 1, w - 1)
        if tt == 'R': l = max(l - 1, 0)

        if ss == 'U': u += 1
        if ss == 'D': d -= 1
        if ss == 'L': l += 1
        if ss == 'R': r -= 1

        if l > r or u > d:
            print('NO')
            return

    if l <= x <= r and u <= y <= d:
        print('YES')
        return
    else:
        print('NO')
        return


if __name__ == '__main__':
    main()

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = reversed(input())
t = reversed(input())

feasible_bound = [1, w, 1, h]

for si, ti in zip(s, t):
    if ti == "L":
        feasible_bound[1] = min(w, feasible_bound[1] + 1)
    elif ti == "R":
        feasible_bound[0] = max(1, feasible_bound[0] - 1)
    elif ti == "U":
        feasible_bound[3] = min(h, feasible_bound[3] + 1)
    elif ti == "D":
        feasible_bound[2] = max(1, feasible_bound[2] - 1)

    if si == "L":
        feasible_bound[0] += 1
    elif si == "R":
        feasible_bound[1] -= 1
    elif si == "U":
        feasible_bound[2] += 1
    elif si == "D":
        feasible_bound[3] -= 1

    

    if feasible_bound[0] > feasible_bound[1] or feasible_bound[2] > feasible_bound[3]:
        print("NO")
        break
else:
    if (
        feasible_bound[0] <= sc <= feasible_bound[1]
        and feasible_bound[2] <= sr <= feasible_bound[3]
    ):
        print("YES")
    else:
        print("NO")

Solution (Python):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

left = 1
right = W
up = 1
down = H

for i in reversed(range(N)):
    if T[i] == "U":
        down = min(H, down+1)
    elif T[i] == "D":
        up = max(1, up-1)
    elif T[i] == "L":
        right = min(W, right+1)
    else:
        left = max(1, left-1)

    if S[i] == "U":
        up += 1
    elif S[i] == "D":
        down -= 1
    elif S[i] == "L":
        left += 1
    else:
        right -= 1

    

    if left > right or up > down:
        print("NO")
        exit()



if left <= sc <= right and up <= sr <= down:
    print("YES")
else:
    print("NO")

Solution (Python):
H, W, N = map(int,input().split())
sr, sc = map(int,input().split())
S = input()
T = input()

cant = False
leftsafe, rightsafe, upsafe, downsafe = 1, W, 1, H

for i in reversed(range(N)):
    if i != N-1: 
        if T[i] == "R" and leftsafe > 1:
            leftsafe -= 1
        elif T[i] == "L" and rightsafe < W:
            rightsafe += 1
        elif T[i] == "D" and upsafe > 1:
            upsafe -= 1
        elif T[i] == "U" and downsafe < H:
            downsafe += 1
    
    if S[i] == "L":
        leftsafe += 1
    elif S[i] == "R":
        rightsafe -= 1
    elif S[i] == "U":
        upsafe += 1
    elif S[i] == "D":
        downsafe -= 1
    
    if leftsafe > rightsafe or upsafe > downsafe:
        cant = True
        break
    
if leftsafe > sc or rightsafe < sc or upsafe > sr or downsafe < sr:
    cant = True
    
if cant:
    print("NO")
else:
    print("YES")

Solution (Python):
H, W, N = tuple(map(int, input().split()))
r, c = tuple(map(int, input().split()))
r-=1
c-=1
Ta = input()
Ao = input()
L, R, U, D = -1, W, -1, H
for idx, (t, a) in enumerate(zip(reversed(Ta), reversed(Ao))):
    if idx != 0:
        if(a == "R"):
            L = max(L-1,-1)
        elif(a == "L"):
            R = min(R+1,W)
        elif(a == "U"):
            D = min(D+1,H)
        else:
            U = max(U-1,-1)
            
    
    if(t == "R"):
        R = max(R-1,-1)
    elif(t == "L"):
        L = min(L+1,W)
    elif(t == "U"):
        U = min(U+1,H)
    else:
        D = max(D-1,-1)
    
    if (R-L-1)*(D-U-1) <= 0:
        print("NO")
        quit()
print("YES" if L < c < R and U < r < D else "NO")

Solution (Python):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()

 
safe_ud = [1, H]
safe_lr = [1, W]
flag = True
for i in reversed(range(N)):
    if T[i] == 'L': 
        if safe_lr[1] != W:
            safe_lr[1] += 1
    elif T[i] == 'R': 
        if safe_lr[0] != 1:
            safe_lr[0] -= 1
    elif T[i] == 'U': 
        if safe_ud[1] != H:
            safe_ud[1] += 1
    elif T[i] == 'D': 
        if safe_ud[0] != 1:
            safe_ud[0] -= 1
    
    if S[i] == 'L': 
        safe_lr[0] += 1
    elif S[i] == 'R': 
        safe_lr[1] -= 1
    elif S[i] == 'U': 
        safe_ud[0] += 1
    elif S[i] == 'D': 
        safe_ud[1] -= 1

    if safe_lr[1] - safe_lr[0] < 0 or safe_ud[1] - safe_ud[0] < 0: 
        flag = False
        break
 
if flag:
    if safe_ud[0] <= st[0] <= safe_ud[1] and safe_lr[0] <= st[1] <= safe_lr[1]:
        pass
    else:
        flag = False
 
if flag:
    print('YES')
else:
    print('NO')

Solution (Python):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()

 
safe_ud = [1, H]
safe_lr = [1, W]
flag = True
for i in reversed(range(N)):
    if T[i] == 'L': 
        if safe_lr[1] != W:
            safe_lr[1] += 1
    elif T[i] == 'R': 
        if safe_lr[0] != 1:
            safe_lr[0] -= 1
    elif T[i] == 'U': 
        if safe_ud[1] != H:
            safe_ud[1] += 1
    elif T[i] == 'D': 
        if safe_ud[0] != 1:
            safe_ud[0] -= 1
    
    if S[i] == 'L': 
        safe_lr[0] += 1
    elif S[i] == 'R': 
        safe_lr[1] -= 1
    elif S[i] == 'U': 
        safe_ud[0] += 1
    elif S[i] == 'D': 
        safe_ud[1] -= 1

    if safe_lr[1] - safe_lr[0] < 0 or safe_ud[1] - safe_ud[0] < 0: 
        flag = False
        break
 
if flag:
    if safe_ud[0] <= st[0] <= safe_ud[1] and safe_lr[0] <= st[1] <= safe_lr[1]:
        pass
    else:
        flag = False
 
if flag:
    print('YES')
else:
    print('NO')

Solution (Python):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 1:
                    now[dim] += step
    return False
        
Dim = ['D','U','R','L']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

Solution (Python):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 1:
                    now[dim] += step
    return False
        
Dim = ['U','D','L','R']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

Solution (Python):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 1:
                    now[dim] += step
    return False
        
Dim = ['U','R','D','L']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

Solution (Python):
H,W,N = map(int, input().split())
sr,sc = map(int, input().split())
S = input()
T = input()

left, right = 0, W+1
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for i in reversed(range(N-1)):
    si, ti = S[i], T[i]
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(0, left)
    right = min(W+1, right)

    if si == "R":
        right -= 1
    elif si == "L":
        left += 1

    if right - left == 1:
        print("NO")
        exit()

if sc <= left or right <= sc:
    print("NO")
    exit()


left, right = 0, H+1
if S[-1] == "U":
    left += 1
elif S[-1] == "D":
    right -= 1

for i in reversed(range(N-1)):
    si, ti = S[i], T[i]
    if ti == "D":
        left -= 1
    elif ti == "U":
        right += 1
    left = max(0, left)
    right = min(H+1, right)

    if si == "D":
        right -= 1
    elif si == "U":
        left += 1

    if right - left == 1:
        print("NO")
        exit()

if sr <= left or right <= sr:
    print("NO")
    exit()

print("YES")

Solution (Python):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 1:
                    now[dim] += step
    return False
        
Dim = ['U','D','R','L']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

Solution (Python):
H, W, N = [int(_) for _ in input().split()]
R, C = [int(_) for _ in input().split()]
S = input()
T = input()

imin = 1
imax = H
jmin = 1
jmax = W

for n in range(N - 1, -1, -1):
    s = S[n]
    t = T[n]
    if t == 'U':
        imax = min(imax + 1, H)
    elif t == 'D':
        imin = max(imin - 1, 1)
    elif t == 'L':
        jmax = min(jmax + 1, W)
    elif t == 'R':
        jmin = max(jmin - 1, 1)
    if s == 'U':
        imin += 1
    elif s == 'D':
        imax -= 1
    elif s == 'L':
        jmin += 1
    elif s == 'R':
        jmax -= 1
    if imax < imin or jmax < jmin:
        print('NO')
        exit()
if imin <= R <= imax and jmin <= C <= jmax:
    print('YES')
else:
    print('NO')

Solution (Python):
from itertools import accumulate
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**10
from functools import reduce

h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
l = 1
r = w
u = 1
d = h
for s,t in zip(s[::-1],t[::-1]):
    if t == "R":
        l = max(l-1,1)
    elif t == "L":
        r = min(r+1,w)
    elif t =="U":
        d = min(d+1,h)
    else:
        u = max(u-1,1)

    if s == "L":
        l += 1
    elif s =="R":
        r -=1
    elif s =="U":
        u+=1
    else:
        d-=1
    if l > r or u > d:
        print('NO')
        exit()
if u <= sr <= d and l <= sc <= r:
    print("YES")
else:
    print("NO")

Solution (Python):
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
sr -=1
sc -=1
s = input()
t = input()
p1,p2 = sr,sr
p3,p4 = sc,sc
for i in range(n):
    if s[i] == "U":
        p1 -=1
        if p1 < 0:
            print("NO")
            exit()
    if t[i] == "D" and p1 < h-1:
        p1 += 1
for i in range(n):
    if s[i] == "D":
        p2 +=1
        if p2 >= h:
            print("NO")
            exit()
    if t[i] == "U" and p2 >0:
        p2 -= 1
for i in range(n):
    if s[i] == "R":
        p3 +=1
        if p3 >= w:
            print("NO")
            exit()
    if t[i] == "L" and p3 >0:
        p3 -= 1
for i in range(n):
    if s[i] == "L":
        p4 -=1
        if p4 < 0:
            print("NO")
            exit()
    if t[i] == "R" and p4 < h-1:
        p4 += 1
print("YES")

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()

flag = True
hmax = h
wmax = w
hmin = 1
wmin = 1

for i in range(n, 0, -1):
    if t[i - 1] == "U":
        hmax = min(hmax + 1, h)
    elif t[i - 1] == "D":
        hmin = max(hmin - 1, 1)
    elif t[i - 1] == "L":
        wmax = min(wmax + 1, w)
    elif t[i - 1] == "R":
        wmin = max(wmin - 1, 1)

    if s[i - 1] == "U":
        hmin += 1
    elif s[i - 1] == "D":
        hmax -= 1
    elif s[i - 1] == "L":
        wmin += 1
    elif s[i - 1] == "R":
        wmax -= 1

    if hmax < hmin or wmax < wmin:
        flag = False
        break

if not hmin <= sr <= hmax or not wmin <= sc <= wmax:
    flag = False

print("YES" if flag else "NO")

Solution (Python):
H,W,N = map(int,input().split())
sr,sc = [int(e)-1 for e in input().split()]
S = input()
T = input()

r = 0
l = 0
u = 0
d = 0
ans = "YES"

for j in range(N):
    for i in range(2):

        if i == 0:
            mark = S[j]
            if mark == "R":
                r += 1
            elif mark == "L":
                l += 1
            elif mark == "U":
                u += 1
            elif mark == "D":
                d += 1
            
        if i == 1:
            mark = T[j]
            if mark == "R":
                l = max(l-1,-(W-1-sc))
            elif mark == "L":
                r = max(r-1,-sc)
            elif mark == "U":
                d = max(d-1,-sr)
            elif mark == "D":
                u = max(u-1,-(H-1-sr))
    
        if r > W-1-sc or l > sc or u > sr or d > H-1-sr:
            ans = "NO"

print(ans)

Solution (Python):
def RL(a):
    if a=='R':
        return 1
    elif a=='L':
        return -1
    else:
        return 0
    
def UD(a):
    if a=='D':
        return 1
    elif a=='U':
        return -1
    else:
        return 0

def printLim(minus_lim, plus_lim, MAX):
    if minus_lim <0:
        print('X|', end='')
    else:
        print('|'+'X'*(minus_lim+1), end='')
    
    print('O'*(plus_lim - minus_lim -1), end='')
    if plus_lim ==MAX:
        print('|X')
    else:
        print('X'*(MAX-plus_lim) + '|')

def isFall(s_data,t_data,MAX,target):
    a = -1
    b = MAX
    for i in range(N-1, -1,-1):
        save = t_data[i]
        if save == 1:
            a-=1
        elif save == -1:
            b+=1
            
        
        if a < -1:
            a=-1
        if b>MAX:
            b=MAX
        if a==b:
            break


        kill = s_data[i]

        
        if kill == -1:
            a +=1
        
        elif kill == 1:
            b -=1

        
        if a < -1:
            a=-1
        if b>MAX:
            b=MAX
        if (b-a) ==1:
            break




    
    if a < target < b:
        return False
    else:
        return True





H, W, N = map(int,input().split())
orig_y,orig_x = map(int,input().split())
orig_y -=1
orig_x -=1
s = input()
t = input()


flag = isFall([RL(it) for it in s],[RL(it) for it in t],W,orig_x)
flag |= isFall([UD(it) for it in s],[UD(it) for it in t],H,orig_y)
if flag:
    print('NO')
else:
    print('YES')

Solution (Python):
H, W, N = map(int, input().split())
sy, sx = map(int, input().split())
S = input()
T = input()
S = S[::-1]
T = T[::-1]

safe_y = [1, H]
safe_x = [1, W]
flg = 1
for i in range(N):
    if T[i] == 'L':
        if safe_x[1] != W:
            safe_x[1] += 1
    elif T[i] == 'R':
        if safe_x[0] != 1:
            safe_x[0] -= 1
    elif T[i] == 'U':
        if safe_y[1] != H:
            safe_y[1] += 1
    elif T[i] == 'D':
        if safe_y[0] != 1:
            safe_y[0] -= 1
    if S[i] == 'L':
        safe_x[0] += 1
    elif S[i] == 'R':
        safe_x[1] -= 1
    elif S[i] == 'U':
        safe_y[0] += 1
    elif S[i] == 'D':
        safe_y[1] -= 1
    if safe_y[1] - safe_y[0] < 0 or safe_x[1] - safe_x[0] < 0:
        flg = 0
        break

if flg:
    if safe_y[0] <= sy <= safe_y[1] and safe_x[0] <= sx <= safe_x[1]:
        pass
    else:
        flg = 0

print('YES' if flg else 'NO')

Solution (Python):
H, W, N = map(int, input().split())
sy, sx = map(int, input().split())
S = input()
T = input()
S = S[::-1]
T = T[::-1]

safe_y = [1, H]
safe_x = [1, W]
flg = 1
for i in range(N):
    if T[i] == 'L':
        if safe_x[1] != W:
            safe_x[1] += 1
    elif T[i] == 'R':
        if safe_x[0] != 1:
            safe_x[0] -= 1
    elif T[i] == 'U':
        if safe_y[1] != H:
            safe_y[1] += 1
    elif T[i] == 'D':
        if safe_y[0] != 1:
            safe_y[0] -= 1
    if S[i] == 'L':
        safe_x[0] += 1
    elif S[i] == 'R':
        safe_x[1] -= 1
    elif S[i] == 'U':
        safe_y[0] += 1
    elif S[i] == 'D':
        safe_y[1] -= 1
    if safe_y[1] - safe_y[0] < 0 or safe_x[1] - safe_x[0] < 0:
        flg = 0
        break

if flg:
    if safe_y[0] <= sy <= safe_y[1] and safe_x[0] <= sx <= safe_x[1]:
        pass
    else:
        flg = 0

print('YES' if flg else 'NO')

Solution (Python):
def solve():
    lim_u = 0
    lim_b = h
    lim_l = 0
    lim_r = w
    for s, t in zip(ss[::-1], ts[::-1]):
        if t == 'U':
            if lim_b < h:
                lim_b += 1
        elif t == 'D':
            if lim_u > 0:
                lim_u -= 1
        elif t == 'L':
            if lim_r < w:
                lim_r += 1
        else:
            if lim_l > 0:
                lim_l -= 1
        if s == 'U':
            lim_u += 1
        elif s == 'D':
            lim_b -= 1
        elif s == 'L':
            lim_l += 1
        else:
            lim_r -= 1

        if lim_u >= lim_b or lim_l >= lim_r:
            return False

    return lim_u < sr <= lim_b and lim_l < sc <= lim_r


h, w, n = list(map(int, input().split()))
sr, sc = map(int, input().split())
ss = input()
ts = input()
print('YES' if solve() else 'NO')

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
win = 0

SR = sr
SC = sc
sr = SR
sc = SC
for i in range(n):
    if S[i] == "L":
        sc -= 1
    if sc <= 0:
        break
    if T[i] == "R" and sc < w:
        sc += 1
if sc <= 0:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "R":
        sc += 1
    if sc > w:
        break
    if T[i] == "L" and sc > 1:
        sc -= 1
if sc > w:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "D":
        sr += 1
    if sr > h:
        break
    if T[i] == "U" and sr > 1:
        sr -= 1
if sr > h:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        break
    if T[i] == "D" and sr < h:
        sr += 1
if sr <= 0:
    win = 1

if win:
    print("NO")
else:
    print("YES")

Solution (Python):
import numpy as np

H, W, N = map(int, input().split())
si, sj = map(int, input().split())
si, sj = si - 1, sj - 1

S = input()
T = input()

piece_remaining = True

low_min = 0
high_max = H - 1

for t in range(N)[::-1]:
	if T[t] == 'U':
		high_max = min(high_max + 1, H - 1)
	elif T[t] == 'D':
		low_min = max(0, low_min - 1)
	
	if S[t] == 'U':
		low_min += 1
	elif S[t] == 'D':
		high_max -= 1
	
	if high_max < low_min:
		piece_remaining = False
		break

if not low_min <= si <= high_max:
	piece_remaining = False


low_min = 0
high_max = W - 1

for t in range(N)[::-1]:
	if T[t] == 'L':
		high_max = min(high_max + 1, W - 1)
	elif T[t] == 'R':
		low_min = max(0, low_min - 1)
	
	if S[t] == 'L':
		low_min += 1
	elif S[t] == 'R':
		high_max -= 1
	
	if high_max < low_min:
		piece_remaining = False
		break

if not low_min <= sj <= high_max:
	piece_remaining = False

print('YES' if piece_remaining else 'NO')

Solution (Python):
h,w,n=map(int,input().split())
r,c=map(int,input().split())
s=input()
t=input()
xr=xl=xd=xu=yr=yl=yd=yu=0
for i in range(n):
    xr+=s[i]=='R'
    xl+=s[i]=='L'
    xd+=s[i]=='D'
    xu+=s[i]=='U'
    if w-c-(xr-yl)<0 or c-1-(xl-yr)<0 or h-r-(xd-yu)<0 or r-1-(xu-yd)<0:
        print('NO')
        break
    yr+=t[i]=='R'
    yr-=w-c-(yr-xl)<0
    yl+=t[i]=='L'
    yl-=c-1-(yl-xr)<0
    yd+=t[i]=='D'
    yd-=h-r-(yd-xu)<0
    yu+=t[i]=='U'
    yu-=r-1-(yu-xd)<0
else:
    print('YES')

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
win = 0

SR = sr
SC = sc
sr = SR
sc = SC
for i in range(n):
    if S[i] == "L":
        sc -= 1
    if sc <= 0:
        break
    if T[i] == "R" and sc < w:
        sc += 1
if sc <= 0:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "R":
        sc += 1
    if sc > w:
        break
    if T[i] == "L" and sc > 1:
        sc -= 1
if sc > w:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "D":
        sr += 1
    if sr > h:
        break
    if T[i] == "U" and sr > 1:
        sr -= 1
if sr > h:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        break
    if T[i] == "D" and sr < h:
        sr += 1
if sr <= 0:
    win = 1

if win:
    print("NO")
else:
    print("YES")

Solution (Python):
h, w, n = [int(i) for i in input().split()]
sr, sc = [int(i) for i in input().split()]
s = input()
t = input()


dpx = [None for _ in range(n*2+1)]
dpy = [None for _ in range(n*2+1)]

dpx[0] = (1, w+1)
dpy[0] = (1, h+1)


for i in range(1,n*2+1):
    is_t = i % 2 == 1
    strategy = t[-1-(i-1)//2] if is_t else s[-1-(i-1)//2]

    
    if is_t:
        if strategy == 'L':
            dpx[i] = (dpx[i-1][0], min(dpx[i-1][1] + 1, w+1))
            dpy[i] = dpy[i-1]
        if strategy == 'R':
            dpx[i] = (max(1, dpx[i-1][0] - 1), dpx[i-1][1])
            dpy[i] = dpy[i-1]
        if strategy == 'U':
            dpy[i] = (dpy[i-1][0], min(dpy[i-1][1] + 1, h+1))
            dpx[i] = dpx[i-1]
        if strategy == 'D':
            dpy[i] = (max(1, dpy[i-1][0] - 1), dpy[i-1][1])
            dpx[i] = dpx[i-1]
    
    else:
        if strategy == 'L':
            dpx[i] = (dpx[i-1][0] + 1, dpx[i-1][1])
            dpy[i] = dpy[i-1]
        if strategy == 'R':
            dpx[i] = (dpx[i-1][0], dpx[i-1][1] - 1)
            dpy[i] = dpy[i-1]
        if strategy == 'U':
            dpy[i] = (dpy[i-1][0] + 1, dpy[i-1][1])
            dpx[i] = dpx[i-1]
        if strategy == 'D':
            dpy[i] = (dpy[i-1][0], dpy[i-1][1] - 1)
            dpx[i] = dpx[i-1]
    
    if len(range(*dpx[i])) == 0 or len(range(*dpy[i])) == 0:
        break

print('NO' if dpx[-1] is None else 'YES' if sr in range(*dpy[-1]) and sc in range(*dpx[-1]) else 'NO')

Solution (Python):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = list(input())
T = list(input())

border_U = 0
border_D = H + 1
border_L = 0
border_R = W + 1

res = "YES"

for i in reversed(range(1, N)):

    
    if S[i] == "U":
        border_U += 1
    elif S[i] == "D":
        border_D -= 1
    elif S[i] == "L":
        border_L += 1
    elif S[i] == "R":
        border_R -= 1

    
    if border_U >= border_D - 1 or border_L >= border_R - 1:
        res = "NO"

    
    if T[i - 1] == "U":
        border_D += 1
    elif T[i - 1] == "D":
        border_U -= 1
    elif T[i - 1] == "L":
        border_R += 1
    elif T[i - 1] == "R":
        border_L -= 1

    
    border_U = max(border_U, 0)
    border_D = min(border_D, H + 1)
    border_L = max(border_L, 0)
    border_R = min(border_R, W + 1)


if S[0] == "U":
    border_U += 1
elif S[0] == "D":
    border_D -= 1
elif S[0] == "L":
    border_L += 1
elif S[0] == "R":
    border_R -= 1

if sr <= border_U:
    res = "NO"
elif sr >= border_D:
    res = "NO"
elif sc <= border_L:
    res = "NO"
elif sc >= border_R:
    res = "NO"

print(res)