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



Sample input:
2 3 3
2 2
RRL
LUD


Sample output:
YES

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def aok_turn(aoki: str, tolow: str, toup: str, lower: int, upper: int, amin: int, amax: int):
    if aoki == tolow:
        upper = min(amax, upper + 1)
    elif aoki == toup:
        lower = max(amin, lower + 1)

    return lower, upper

def tak_turn(taki: str, tolow: str, toup: str, lower: int, upper: int, amin:int, amxax: int):
    if taki == tolow:
        lower -= 1
    elif taki == toup:
        upper -= 1

    return lower, upper

def on_board(lower: int, upper: int, amin: int, amax: int):
    if lower < upper and upper > amin and lower < amax:
        return True
    else:
        return False


def judge(tak: str, aok: str, tolow: str, toup: str, start: int, amin: int, amax: int) -> bool:
    lower = amin
    upper = amax
    for aoki, taki in zip(aok, tak):
        lower, upper = aok_turn(aoki, tolow, toup, lower, upper, amin, amax)
        lower, upper = tak_turn(taki, tolow, toup, lower ,upper, amin, amax)
        if not on_board(lower, upper, amin, amax):
            return False

    return True if lower <= start < upper else False


h,w,n = li()
sr, sc = li_()
tak = ns()[::-1]
aok = ns()[::-1]

L = 'L'
R = 'R'
U = 'U'
D = 'D'

lr = judge(tak, aok, L, R, sc, 0, w)
ud = judge(tak, aok, D, U, sr, 0, h)

print("YES") if lr and ud else print("NO")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
s_r, s_c = map(int, input().split())
S = input()
T = input()

# vertical direction
up, down = 0, H
for i in range(N-1, -1, -1):
    if T[i] == 'U':
        down = min(H, down + 1)
    elif T[i] == 'D':
        up = max(0, up - 1)
    
    if S[i] == 'U':
        up += 1
    elif S[i] == 'D':
        down -= 1
    
    if up >= down:
        print('NO')
        exit()
        
# horizontal direction
left, right = 0, W
for j in range(N-1, -1, -1):    
    if T[j] == 'L':
        right = min(W, right + 1)
    elif T[j] == 'R':
        left = max(0, left - 1)
    
    if S[j] == 'L':
        left += 1
    elif S[j] == 'R':
        right -= 1
    
    if left >= right:
        print('NO')
        exit()
if up <= s_r < down and left <= s_c < right:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

L = 0
R = W+1
U = 0
D = H+1

for i in range(N-1,-1,-1):
    if S[i] == "L":
        L += 1
    elif S[i] == "R":
        R -= 1
    elif S[i] == "U":
        U += 1
    elif S[i] == "D":
        D -= 1
    if L+1==R or U+1==D:
        print("NO")
        break

    if i>0:
        if T[i-1] == "L":
            R += 1
        elif T[i-1] == "R":
            L -= 1
        elif T[i-1] == "U":
            D += 1
        elif T[i-1] == "D":
            U -= 1
else:
    if sr<=U or D<=sr or sc<=L or R<=sc:
        print("NO")
    else:
        print("YES")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
l = 0
r = W+1
u = 0
d = H+1
flag = True
for s,t in zip(S[::-1],T[::-1]):
    if l+1==r or u+1==d:
        flag=False
        break
    if t == 'L':
        r = min(W+1, r+1)
    if t == 'R':
        l = max(0, l-1)
    if t == 'U':
        d = max(H+1, d+1)
    if t == 'D':
        u = min(0, u-1)
    if s == 'L':
        l += 1
    if s == 'R':
        r -= 1
    if s == 'U':
        u += 1
    if s == 'D':
        d -= 1
if not(l < sc < r and u < sr < d):
    flag = False
if flag:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
l = 0
r = W+1
u = 0
d = H+1
flag = True
for s,t in zip(S[::-1],T[::-1]):
    if l+1==r or u+1==d:
        flag=False
        break
    if t == 'L':
        r = min(0, r-1)
    if t == 'R':
        l = max(W+1, l+1)
    if t == 'U':
        d = min(0, d-1)
    if t == 'D':
        u = max(H+1, u+1)
    if s == 'L':
        l += 1
    if s == 'R':
        r -= 1
    if s == 'U':
        u += 1
    if s == 'D':
        d -= 1
if not(l < sc < r and u < sr < d):
    flag = False
if flag:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def aok_turn(com: str, tolow: str, toup: str, lower: int, upper: int, amin: int, amax: int):
    if com == tolow:
        upper = min(upper + 1, amax)
    elif com == toup:
        lower = max(lower - 1, amin)

    return lower, upper


def tak_turn(com: str, tolow: str, toup: str, lower: int, upper: int, amin: int, amax: int):
    if com == tolow:
        lower += 1
    elif com == toup:
        upper -= 1

    return lower, upper


def on_board(lower: int, upper: int):
    return True if lower < upper else False


def judge(tak: str, aok: str, start: int, tolow: str, toup: str, lower: int, upper: int, amin: int, amax: int):
    for aoki, taki in zip(aok, tak):
        lower, upper = aok_turn(aoki, tolow, toup, lower, upper, amin, amax)
        lower, upper = tak_turn(taki, tolow, toup, lower, upper, amin, amax)

        if lower >= upper:
            return False

    if lower <= start < upper:
        return True
    else:
        return False


h, w, n = li()
sr, sc = li_()
tak = ns()[::-1]
aok = ns()[::-1]

lr = judge(tak, aok, sc, 'L', 'R', 0, w, 0, w)
ud = judge(tak, aok, sr, 'D', 'U', 0, h, 0, h)

print("YES" if lr and ud else "NO")

Submission status: Wrong Answer

Solution code:
H,W,N = map(int,input().split())
sr,sc = map(int,input().split())
S = list(input())
T = list(input())
sum_r,sum_l,sum_u,sum_d = [0 for i in range(N+1)],[0 for i in range(N+1)],[0 for i in range(N+1)],[0 for i in range(N+1)]

for i in range(N):
    s,t = S[i], T[i]
    if s == "L":
        sum_l[i+1] = sum_l[i] + 1
    if s == "R":
        sum_r[i+1] = sum_r[i] + 1
    if s == "U":
        sum_u[i+1] = sum_u[i] + 1
    if s == "D":
        sum_d[i+1] = sum_d[i] + 1

    if sc - sum_l[i+1] <= 0 or sc + sum_r[i+1] > W or sr - sum_u[i+1] <= 0 or sr + sum_d[i+1] > H:
        print("NO")
        exit()

    if t == "L":
        if sc + sum_r[i] > 1:
            sum_r[i+1] = sum_r[i] - 1
    if t == "R":
        if sc - sum_l[i] < W:
            sum_l[i+1] = sum_l[i] - 1
    if t == "U":
        if sr + sum_d[i] > 1:
            sum_d[i+1] = sum_d[i] - 1
    if t == "D":
        if sr - sum_u[i] < H:
            sum_u[i+1] = sum_u[i] - 1
print("YES")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(input())
t = list(input())
r, l, u, d = sr, 1, sc, 1
ns = list(range(n))
for i in ns[::-1]:
    if t[i] == 'L':
        r += 1
    elif t[i] == 'R':
        l -= 1
    elif t[i] == 'U':
        u += 1
    elif t[i] == 'D':
        d -= 1
    if s[i] == 'L':
        l += 1
    elif s[i] == 'R':
        r -= 1
    elif s[i] == 'U':
        d += 1
    elif s[i] == 'D':
        u -= 1
    if (l > r) or (u > d):
        ans = 'NO'
        break
if (l <= w <= r) and (u <= h <= d):
    ans = 'YES'
else:
    ans = 'NO'
print(ans)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(input())
t = list(input())
sl_move, sr_move, su_move, sd_move = [], [], [], []
tl_move, tr_move, tu_move, td_move = [], [], [], []
for i in s:
    if i == 'L':
        sl_move += [-1]
        sr_move += [0]
        su_move += [0]
        sd_move += [0]
    elif i == 'R':
        sl_move += [0]
        sr_move += [1]
        su_move += [0]
        sd_move += [0]
    elif i == 'U':
        sl_move += [0]
        sr_move += [0]
        su_move += [-1]
        sd_move += [0]
    elif i == 'D':
        sl_move += [0]
        sr_move += [0]
        su_move += [0]
        sd_move += [1]
for i in t:
    if i == 'L':
        tl_move += [-1]
        tr_move += [0]
        tu_move += [0]
        td_move += [0]
    elif i == 'R':
        tl_move += [0]
        tr_move += [1]
        tu_move += [0]
        td_move += [0]
    elif i == 'U':
        tl_move += [0]
        tr_move += [0]
        tu_move += [-1]
        td_move += [0]
    elif i == 'D':
        tl_move += [0]
        tr_move += [0]
        tu_move += [0]
        td_move += [1]
ru, rd, cr, cl = sr, sr, sc, sc
for i in range(n):
    ru += su_move[i]
    if ru < 1:
        ans = 'NO'
        break
    ru += td_move[i]
    if ru > h:
        ru = h
    rd += sd_move[i]
    if ru > h:
        ans = 'NO'
        break
    rd += tu_move[i]
    if rd < 1:
        rd = 1
    cr += sr_move[i]
    if cr > w:
        ans = 'NO'
        break
    cr += tl_move[i]
    if cr < 1:
        cr = 1
    cl += sl_move[i]
    if cl < 1:
        ans = 'NO'
        break
    cl += tr_move[i]
    if cl > w:
        cl = w
    ans = 'YES'
print(ans)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(input())
t = list(input())
sl_move, sr_move, su_move, sd_move = [], [], [], []
tl_move, tr_move, tu_move, td_move = [], [], [], []
for i in s:
    if i == 'L':
        sl_move += [-1]
        sr_move += [0]
        su_move += [0]
        sd_move += [0]
    elif i == 'R':
        sl_move += [0]
        sr_move += [1]
        su_move += [0]
        sd_move += [0]
    elif i == 'U':
        sl_move += [0]
        sr_move += [0]
        su_move += [1]
        sd_move += [0]
    elif i == 'D':
        sl_move += [0]
        sr_move += [0]
        su_move += [0]
        sd_move += [-1]
for i in t:
    if i == 'L':
        tl_move += [-1]
        tr_move += [0]
        tu_move += [0]
        td_move += [0]
    elif i == 'R':
        tl_move += [0]
        tr_move += [1]
        tu_move += [0]
        td_move += [0]
    elif i == 'U':
        tl_move += [0]
        tr_move += [0]
        tu_move += [1]
        td_move += [0]
    elif i == 'D':
        tl_move += [0]
        tr_move += [0]
        tu_move += [0]
        td_move += [-1]
ru, rd, cr, cl = sr, sr, sc, sc
for i in range(n):
    ru += su_move[i]
    if ru > h:
        ans = 'NO'
        break
    ru += td_move[i]
    if ru < 1:
        ru = 1
    rd += sd_move[i]
    if ru < 1:
        ans = 'NO'
        break
    rd += tu_move[i]
    if rd > h:
        rd = h
    cr += sr_move[i]
    if cr > w:
        ans = 'NO'
        break
    cr += tl_move[i]
    if cr < 1:
        cr = 1
    cl += sl_move[i]
    if cl < 1:
        ans = 'NO'
        break
    cl += tr_move[i]
    if cl > w:
        cl = w
    ans = 'YES'
print(ans)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(input())
t = list(input())
sl_move, sr_move, su_move, sd_move = [], [], [], []
tl_move, tr_move, tu_move, td_move = [], [], [], []
for i in s:
    if i == 'L':
        sl_move += [-1]
        sr_move += [0]
        su_move += [0]
        sd_move += [0]
    elif i == 'R':
        sl_move += [0]
        sr_move += [1]
        su_move += [0]
        sd_move += [0]
    elif i == 'U':
        sl_move += [0]
        sr_move += [0]
        su_move += [1]
        sd_move += [0]
    elif i == 'D':
        sl_move += [0]
        sr_move += [0]
        su_move += [0]
        sd_move += [-1]
for i in t:
    if i == 'L':
        tl_move += [-1]
        tr_move += [0]
        tu_move += [0]
        td_move += [0]
    elif i == 'R':
        tl_move += [0]
        tr_move += [1]
        tu_move += [0]
        td_move += [0]
    elif i == 'U':
        tl_move += [0]
        tr_move += [0]
        tu_move += [1]
        td_move += [0]
    elif i == 'D':
        tl_move += [0]
        tr_move += [0]
        tu_move += [0]
        td_move += [-1]
ru, rd, cr, cl = sr, sr, sc, sc
for i in range(n):
    ru += su_move[i]
    if ru > h:
        ans = 'No'
        break
    ru += td_move[i]
    if ru < 1:
        ru = 1
    rd += sd_move[i]
    if ru < 1:
        ans = 'No'
        break
    rd += tu_move[i]
    if rd > h:
        rd = h
    cr += sr_move[i]
    if cr > w:
        ans = 'No'
        break
    cr += tl_move[i]
    if cr < 1:
        cr = 1
    cl += sl_move[i]
    if cl < 1:
        ans = 'No'
        break
    cl += tr_move[i]
    if cl > w:
        cl = w
    ans = 'Yes'
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

H, W, N = map(int, input().split())
y, x = map(int, input().split())
x -= 1
y -= 1
remover = input()
stayer = input()

# borders
top, btm, left, right = 0, H-1, 0, W-1

def solve(top, btm, left, right):
    for s, r in zip(stayer, remover):

        if s == 'U':
            btm = min(btm+1, H-1)
        elif s == 'D':
            top = max(top-1, 0)
        elif s == 'R':
            left = max(left-1, 0)
        else: # 'L'
            right = min(right+1, W-1)

        if r == 'U':
            top += 1
        elif r == 'D':
            btm -= 1
        elif r == 'R':
            right -= 1
        else: # 'L'
            left += 1

        if right <= left:
            return 'NO'
        
        if btm <= top:
            return 'NO'


    if top <= y <= btm and left <= x <= right:
        return 'YES'
    else:
        return 'NO'

print(solve(top, btm, left, right))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

H, W, N = map(int, input().split())
y, x = map(int, input().split())
x -= 1
y -= 1
remover = input()
stayer = input()

# borders
top, btm, left, right = 0, H-1, 0, W-1

def solve(top, btm, left, right):
    for s, r in zip(stayer, remover):

        if s == 'U':
            btm = min(btm+1, H-1)
        elif s == 'D':
            top = max(top-1, 0)
        elif s == 'R':
            left = max(left-1, 0)
        else: # 'L'
            right = min(right+1, W-1)

        if r == 'U':
            top += 1
        elif r == 'D':
            btm -= 1
        elif r == 'R':
            right -= 1
        else: # 'L'
            left += 1

        if right < left:
            return 'NO'
        
        if btm < top:
            return 'NO'


    if top <= y <= btm and left <= x <= right:
        return 'YES'
    else:
        return 'NO'

print(solve(top, btm, left, right))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

H, W, N = map(int, input().split())
y, x = map(int, input().split())
x -= 1
y -= 1
remover = input()
stayer = input()

# borders
top, btm, left, right = 0, H-1, 0, W-1

def solve(top, btm, left, right):
    for s, r in zip(stayer, remover):

        if s == 'U':
            btm = min(btm+1, H-1)
        elif s == 'D':
            top = max(top-1, 0)
        elif s == 'R':
            left = max(left-1, 0)
        else: # 'L'
            right = min(right+1, W-1)

        if r == 'U':
            top += 1
        elif r == 'D':
            btm -= 1
        elif r == 'R':
            right -= 1
        else: # 'L'
            left += 1

        if right < left or right < 0 or W-1 < left:
            return 'NO'
        
        if btm < top or btm < 0 or H-1 < top:
            return 'NO'


    if top <= y <= btm and left <= x <= right:
        return 'YES'
    else:
        return 'NO'

print(solve(top, btm, left, right))

Submission status: Wrong Answer

Solution code:
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(), 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)  # tt == 'R'

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

        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()

Submission status: Wrong Answer

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

left = 0
right = W
up = 0
down = H

for i in reversed(range(N)):
    if i != N - 1:
        if T[i] == "U":
            down = min(H, down+1)
        elif T[i] == "D":
            up = max(0, up-1)
        elif T[i] == "L":
            right = min(W, right+1)
        else:
            left = max(0, 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()
#print(left, right)
#print(up, down)
if left <= sr <= right and up <= sc <= down:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

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

left = 0
right = W
up = 0
down = H

for i in reversed(range(N)):
    if i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 0:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 0:
                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()
#print(left, right)
#print(up, down)
if left <= sr <= right and up <= sc <= down:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

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

left = 0
right = W
up = 0
down = H

for i in reversed(range(N)):
    if i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 0:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 0:
                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()
#print(left, right)
#print(up, down)
if left <= sr <= right and up <= sc <= down:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
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 i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 1:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 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 <= sr <= right and up <= sc <= down:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
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 i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 1:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 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 <= sr <= right and up <= sc <= down:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
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 i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 1:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 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 <= sr <= right and down <= sc <= up:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
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 i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 1:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 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 <= sr <= right and down <= sc <= up:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
Spre = input()
S = [Spre[i] for i in range(N)] #駒を落としたい
Tpre = input()
T = [Tpre[i] for i in range(N)] #駒を残したい

#L, R, U, Dの合計
Ssum = [0, 0, 0, 0]
Tsum = [0, 0, 0, 0]
for i in range(N):
    if S[i] == 'L':
        Ssum[0] += 1
    elif S[i] == 'R':
        Ssum[1] += 1
    elif S[i] == 'U':
        Ssum[2] += 1
    elif S[i] == 'D':
        Ssum[3] += 1

for i in range(N):
    if T[i] == 'L':
        Tsum[0] += 1
    elif T[i] == 'R':
        Tsum[1] += 1
    elif T[i] == 'U':
        Tsum[2] += 1
    elif T[i] == 'D':
        Tsum[3] += 1

#落ちそう度合の数値化
center_i = (H-1)/2
center_j = (W-1)/2

#駒の現在位置
k_i = sr - 1
k_j = sc - 1

#試合ラウンド
round = 0

#高橋君の1ターン目
drop_pos = abs(k_i - center_i) + abs(k_j - center_j)
if Ssum[round] == 'D':
    val = abs(k_i+1 - center_i) + abs(k_j - center_j)
    if k_i + 1 == H:
        k_i = H
    elif val > drop_pos:
        k_i += 1
    S[3] -= 1
    
if S[round] == 'U':
    val = abs(k_i-1 - center_i) + abs(k_j - center_j)
    if k_i - 1 == -1:
        k_i = -1
    elif val > drop_pos:
        k_i -= 1
    Ssum[2] -= 1

if S[round] == 'R':
    val = abs(k_i - center_i) + abs(k_j+1 - center_j)
    if k_j + 1 == W:
        k_j = W
    elif val > drop_pos:
        k_j += 1
    Ssum[1] -= 1
    
if S[round] == 'L':
    val = abs(k_i - center_i) + abs(k_j-1 - center_j)
    if k_j - 1 == -1:
        k_i = -1
    elif val > drop_pos:
        k_i -= 1
    Ssum[0] -= 1

if k_i == -1 or k_i == H or k_j == -1 or k_j == W:
    print('NO')
else:
    while round < N-1 and 0 <= k_i < H and 0 <= k_j < W:
        #青木君のターン
        drop_pos = abs(k_i - center_i) + abs(k_j - center_j)
        if T[round] == 'D':
            val = abs(k_i+1 - center_i) + abs(k_j - center_j)
            if val < drop_pos:
                k_i += 1
            Tsum[3] -= 1
    
        if T[round] == 'U':
            val = abs(k_i-1 - center_i) + abs(k_j - center_j)
            if val < drop_pos:
                k_i -= 1
            Tsum[2] -= 1

        if T[round] == 'R':
            val = abs(k_i - center_i) + abs(k_j+1 - center_j)
            if val < drop_pos:
                k_j += 1
            Tsum[1] -= 1
    
        if T[round] == 'L':
            val = abs(k_i - center_i) + abs(k_j-1 - center_j)
            if val < drop_pos:
                k_j -= 1
            Tsum[0] -= 1
        

        #高橋君のターン
        drop_pos = abs(k_i - center_i) + abs(k_j - center_j)
        if S[round + 1] == 'D':
            val = abs(k_i+1 - center_i) + abs(k_j - center_j)
            if k_i + 1 == H:
                k_i = H
            elif val > drop_pos and H - k_i <= Ssum[3]:
                k_i += 1
            Ssum[3] -= 1
    
        if S[round + 1] == 'U':
            val = abs(k_i-1 - center_i) + abs(k_j - center_j)
            if k_i - 1 == -1:
                k_i = -1
            elif val > drop_pos and k_i + 1 <= Ssum[2]:
                k_i -= 1
            Ssum[2] -= 1

        if S[round + 1] == 'R':
            val = abs(k_i - center_i) + abs(k_j+1 - center_j)
            if k_j + 1 == W:
                k_j = W
            elif val > drop_pos and W - k_j <= Ssum[1]:
                k_j += 1
            Ssum[1] -= 1
    
        if S[round + 1] == 'L':
            val = abs(k_i - center_i) + abs(k_j-1 - center_j)
            if k_j - 1 == -1:
                k_i = -1
            elif val > drop_pos and k_j + 1 <= Ssum[0]:
                k_i -= 1
            Ssum[0] -= 1
        
        
        round += 1

    if round < N-1 or k_i == -1 or k_i == H or k_j == -1 or k_j == W:
        print('NO')
    else:
        print('YES')

Submission status: Wrong Answer

Solution code:
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 == "":
        left += 1

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

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

print("YES")

Submission status: Wrong Answer

Solution code:
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()


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

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

    if si == "D":
        up -= 1
    elif si == "U":
        down += 1

    if up - down == 1:
        print("NO")
        exit()

if sr <= down or up <= sc:
    print("NO")
    exit()

print("YES")