Problem:
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.

Incorrect submission (verdict: Wrong Answer):
h, w, n = [int(i) for i in input().split()]
sr, sc = [int(i) for i in input().split()]
s = input()
t = input()


def move(strt, sr, sc):
    if strt == 'L':
        return sr, sc - 1
    if strt == 'R':
        return sr, sc + 1
    if strt == 'U':
        return sr - 1, sc
    if strt == 'D':
        return sr + 1, sc



fall_lut = [[[] for i in range(w)] for j in range(h)]
for y in range(h):
    for x in range(w):
        if x <= (w - 1) // 2:
            fall_lut[y][x] += 'L'
        if x >= w // 2:
            fall_lut[y][x] += 'R'
        if y <= (h - 1) // 2:
            fall_lut[y][x] += 'U'
        if y >= h // 2:
            fall_lut[y][x] += 'D'

pass




#     dir_ = []
#     if h % 2 == 0:
#         dir
#     dir_ = ['L' if sc <= w // 2 else 'R', 'U' if sr <= h // 2 else 'D']


for iturn in range(n):
    # S's turn
    s_strt = s[iturn]

    if s_strt in fall_lut[sr-1][sc-1]:
        sr, sc = move(s_strt, sr, sc)
    else:
        # stay
        pass

    
    if not ((sr - 1) in range(h) and (sc - 1) in range(w)):
        break

    # T's turn
    t_strt = t[iturn]

    if t_strt in fall_lut[sr-1][sc-1]:
        # stay
        pass
    else:
        sr, sc = move(t_strt, sr, sc)

    
    if not ((sr - 1) in range(h) and (sc - 1) in range(w)):
        break



print('Yes' if ((sr - 1) in range(h) and (sc - 1) in range(w)) else 'No')


Fixed, accepted submission:
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)

# search safety region from tail
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]

    # T's turn: want to expand region
    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]
    # S's turn: want to contract region
    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]
    # safety region has been cotrancted to 0, then break
    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')