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: Accepted

Solution code:
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
flag=True
for i in range(4):
  if i==0 or i==1:
    pos=sx
  elif i==2 or i==3:
    pos=sy
  exist=False
  for j in range(1,n+1):
    tmp1=s[j-1]
    tmp2=t[j-1]
    if i==0:
      if tmp1=='L':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='R':
        pos+=1
        if pos>w:
          pos=w
      if tmp1=='R':
        if pos==w and exist==True:
          pos=w-1
      if tmp2=='L':
        exist=True
    elif i==1:
      if tmp1=='R':
        pos+=1
        if pos>w:
          flag=False
      if tmp2=='L':
        pos-=1
        if pos==0:
          pos=1
      if tmp1=='L':
        if pos==1 and exist==True:
          pos=2
      if tmp2=='R':
        exist=True
    elif i==2:
      if tmp1=='U':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='D':
        pos+=1
        if pos>h:
          pos=h
      if tmp1=='D':
        if pos==h and exist==True:
          pos=h-1
      if tmp2=='U':
        exist=True
    elif i==3:
      if tmp1=='D':
        pos+=1
        if pos>h:
          flag=False
      if tmp2=='U':
        pos-=1
        if pos==0:
          pos=1
      if tmp1=='U':
        if pos==1 and exist==True:
          pos=2
      if tmp2=='D':
        exist=True
if flag==True:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
flag=True
for i in range(4):
  if i==0 or i==1:
    pos=sx
  elif i==2 or i==3:
    pos=sy
  for j in range(1,n+1):
    tmp1=s[j-1]
    tmp2=t[j-1]
    if i==0:
      if tmp1=='L':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='R':
        pos+=1
        if pos>w:
          pos=w
    elif i==1:
      if tmp1=='R':
        pos+=1
        if pos>w:
          flag=False
      if tmp2=='L':
        pos-=1
        if pos==0:
          pos=1
    elif i==2:
      if tmp1=='U':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='D':
        pos+=1
        if pos>h:
          pos=h
    elif i==3:
      if tmp1=='D':
        pos+=1
        if pos>h:
          flag=False
      if tmp2=='U':
        pos-=1
        if pos==0:
          pos=1
if flag==True:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    safe = {"L": 1, "R": W, "U": 1, "D": H}
    inv = {
        "L": "R",
        "R": "L",
        "U": "D",
        "D": "U"
    }
    for i in range(N-1, -1, -1):
        t = T[i]
        if t == "L":
            safe["R"] = min(safe["R"] + 1, W)
        elif t == "U":
            safe["D"] = min(safe["D"] + 1, H)
        elif t == "R":
            safe["L"] = max(safe["L"] - 1, 1)
        elif t == "D":
            safe["U"] = max(safe["U"] - 1, 1)
        if S[i] in ("R", "D"):
            safe[S[i]] -= 1
        else:
            safe[S[i]] += 1
#        print(goal)
        if safe["D"] < safe["U"] or safe["R"] < safe["L"]:
            print("NO")
            return
    if safe["U"] <= sr <= safe["D"] and safe["L"] <= sc <= safe["R"]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ContestName
# URL
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    H, W, N = i2nn()
    sr, sc = i2nn()
    S = i2s()
    T = i2s()
    h0 = 1
    h1 = H
    w0 = 1
    w1 = W
    for i, s, t in zip(range(N), S[::-1], T[::-1]):
        if t == 'L':
            w1 += 1 if w1 < W else 0
        elif t == 'R':
            w0 -= 1 if w0 > 1 else 0
        elif t == 'U':
            h1 += 1 if h1 < H else 0
        elif t == 'D':
            h0 -= 1 if h0 > 1 else 0

        if s == 'L':
            w0 += 1
        elif s == 'R':
            w1 -= 1
        elif s == 'U':
            h0 += 1
        elif s == 'D':
            h1 -= 1

        if w0 > w1 or h0 > h1:
            print('NO')
            return
    if w0 <= sc and sc <= w1 and h0 <= sr and sr <= h1:
        print('YES')
    else:
        print('NO')

main()

"""
..#
...
"""

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[::-1]
T = input()[::-1]

r_bound = w
l_bound = 1
d_bound = h
u_bound = 1

remain = True

for s, t in zip(S, T):
    if t =='R' and l_bound > 1:
        l_bound -= 1
    elif t =='L' and r_bound < w:
        r_bound += 1
    elif t =='D' and u_bound > 1:
        u_bound -= 1
    elif t =='U' and d_bound < h:
        d_bound += 1
    if s =='R':
        r_bound -= 1
    elif s =='L':
        l_bound += 1
    elif s =='D':
        d_bound -= 1
    elif s =='U':
        u_bound += 1
    if (r_bound < l_bound) or (d_bound < u_bound):
        remain = False
        break
if (sc < l_bound or sc > r_bound) or (sr < u_bound or sr > d_bound):
    remain = False

if remain:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
# Input
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, amax: int):
    if taki == tolow:
        lower = min(amax, lower + 1)
    elif taki == toup:
        upper = max(amin, 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, U, D, sr, 0, h)

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

Submission status: Accepted

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-1
for i in range(N-1, -1, -1):
    if T[i] == 'U':
        down = min(H-1, 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-1
for i in range(N-1, -1, -1):    
    if T[i] == 'L':
        right = min(W-1, right + 1)
    elif T[i] == 'R':
        left = max(0, left - 1)
    
    if S[i] == 'L':
        left += 1
    elif S[i] == 'R':
        right -= 1
    
    if left > right:
        print('NO')
        exit()
if up <= s_r-1 <=down and left <= s_c-1 <= right:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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 = min(W+1,R+1)
        elif T[i-1] == "R":
            L = max(0,L-1)
        elif T[i-1] == "U":
            D = min(H+1,D+1)
        elif T[i-1] == "D":
            U = max(0,U-1)
else:
    if sr<=U or D<=sr or sc<=L or R<=sc:
        print("NO")
    else:
        print("YES")

Submission status: Accepted

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 = min(H+1, d+1)
    if t == 'D':
        u = max(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: Accepted

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 = w, 1, 1, h
ns = list(range(n))
for i in ns[::-1]:
    if t[i] == 'L' and r < w:
        r += 1
    elif t[i] == 'R' and l > 1:
        l -= 1
    elif t[i] == 'U' and d < h:
        d += 1
    elif t[i] == 'D' and u > 1:
        u -= 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 > r) or (u > d):
        ans = 'NO'
        break
if (l <= sc <= r) and (u <= sr <= d):
    ans = 'YES'
else:
    ans = 'NO'
print(ans)

Submission status: Accepted

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 rd > 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: Accepted

Solution code:
def f(h, w, n):
    si, sj = map(int, input().split())
    ss = input()
    tt = input()
    l = u = 1
    r, d = w, h
    for t, s in zip(tt[::-1], ss[::-1]):
        if t == "U" and d < h:
            d += 1
        elif t == "D" and u > 1:
            u -= 1
        elif t == "L" and r < w:
            r += 1
        elif t == "R" and l > 1:
            l -= 1
        if s == "U":
            u += 1
        elif s == "D":
            d -= 1
        elif s == "L":
            l += 1
        else:
            r -= 1
        if u > d or l > r:
            print("NO")
            exit()
    if u <= si <= d and l <= sj <= r:
        print("YES")
    else:
        print("NO")


h, w, n = map(int, input().split())
f(h, w, n)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]

#gridのindexは入力値のまま扱う(grid=[1,1]~[H,W])
#left<x<rightの間が生存範囲(初めはグリッド全てが生存可能状態)
left=0
right=W+1
up=0
down=H+1

ans="YES"
#手を逆順で見ていく。
for s,t in zip(reversed(S),reversed(T)):
    if t=='R':#生存可能範囲を広げる(0より小さくならないようにする)
        left=max(0,left-1)
    elif t=="L":
        right=min(W+1,right+1)
    elif t=="D":
        up = max(0,up-1)
    else:
        down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    #もし、途中でright-left==1。つまり、生存可能範囲がなくなってしまったら、
    #落とされてしまうため、ans=NOにして終了する
    if right-left==1 or down-up==1:
        ans="NO"
        break

#全ての手を見た後で、スタート地点が生存可能範囲に入っていればOK
if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]

#gridのindexは入力値のまま扱う(grid=[1,1]~[H,W])
#left<x<rightの間が生存範囲(初めはグリッド全てが生存可能状態)
left=0
right=W+1
up=0
down=H+1

ans="YES"
for s,t in zip(reversed(S),reversed(T)):
    if t=='R':
        left=max(0,left-1)
    elif t=="L":
        right=min(W+1,right+1)
    elif t=="D":
        up = max(0,up-1)
    else:
        down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    #right<=left+1
    if right-left==1 or down-up==1:
        ans="NO"
        break

if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]

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

ans="YES"
for i,(s,t) in enumerate(zip(reversed(S),reversed(T))):
    if i!=0 or True:
        if t=='R':
            left=max(0,left-1)
        elif t=="L":
            right=min(W+1,right+1)
        elif t=="D":
            up = max(0,up-1)
        else:
            down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    if right-left==1 or down-up==1:
        ans="NO"
        break

if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]

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

ans="YES"
for i,(s,t) in enumerate(zip(reversed(S),reversed(T))):
    if i!=0:
        if t=='R':
            left=max(0,left-1)
        elif t=="L":
            right=min(W+1,right+1)
        elif t=="D":
            up = max(0,up-1)
        else:
            down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    if right-left==1 or down-up==1:
        ans="NO"
        break

if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

Submission status: Accepted

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

# border
lB, rB, uB, dB = 0, W + 1, 0, H + 1

ans = "YES"
for i, (s, t) in enumerate(zip(reversed(S), reversed(T))):
    if i != 0:
        if t == "R":
            lB = max(0, lB - 1)
        elif t == "L":
            rB = min(W + 1, rB + 1)
        elif t == "D":
            uB = max(0, uB - 1)
        elif t == "U":
            dB = min(H + 1, dB + 1)

    if s == "R":
        rB -= 1
    elif s == "L":
        lB += 1
    elif s == "D":
        dB -= 1
    elif s == "U":
        uB += 1

    if (rB - lB) == 1 or (dB - uB) == 1:
        ans = "NO"
        break

if not lB < sc < rB or not uB < sr < dB:
    ans = "NO"

print(ans)

Submission status: Accepted

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

lr, rr = 1, H
lc, rc = 1, W
# print(lr, rr, lc, rc)

for i in range(N-1, -1, -1):
    if T[i] == 'L':
        rc = min(rc+1, W)
    elif T[i] == 'R':
        lc = max(lc-1, 1)
    elif T[i] == 'U':
        rr = min(rr+1, H)
    else:
        lr = max(lr-1, 1)

    # print(lr, rr, lc, rc)

    if S[i] == 'L':
        lc += 1
    elif S[i] == 'R':
        rc -= 1
    elif S[i] == 'U':
        lr += 1
    else:
        rr -= 1

    # print(lr, rr, lc, rc)

    if lc > rc or lr > rr:
        print('NO')
        exit()

if not lr <= sr <= rr or not lc <= sc <= rc:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

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().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)  # 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: Accepted

Solution code:
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: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    # print(si, ti, feasible_bound)

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

Submission status: Accepted

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

    #print(left, right, " ", up, down)

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

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

Submission status: Accepted

Solution code:
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: #最後だけはT[i]を無視
        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")

Submission status: Accepted

Solution code:
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)
            
    #print("A{} L{} c{} R{} U{} r{} D{}".format(a,L,c,R,U,r,D))
    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)
    #print("T{} L{} c{} R{} U{} r{} D{}".format(t,L,c,R,U,r,D))
    if (R-L-1)*(D-U-1) <= 0:
        print("NO")
        quit()
print("YES" if L < c < R and U < r < D else "NO")

Submission status: Accepted

Solution code:
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': #守りが'L'なら安全地帯の左端を狭める
        if safe_lr[1] != W:
            safe_lr[1] += 1
    elif T[i] == 'R': #守りが'R'なら安全地帯の右端を狭める
        if safe_lr[0] != 1:
            safe_lr[0] -= 1
    elif T[i] == 'U': #守りが'U'なら安全地帯の下端を狭める
        if safe_ud[1] != H:
            safe_ud[1] += 1
    elif T[i] == 'D': #守りが'D'なら安全地帯の上端を狭める
        if safe_ud[0] != 1:
            safe_ud[0] -= 1
    
    if S[i] == 'L': #攻めが'L'なら安全地帯の右端を広げる
        safe_lr[0] += 1
    elif S[i] == 'R': #攻めが'R'なら安全地帯の左端を広げる
        safe_lr[1] -= 1
    elif S[i] == 'U': #攻めが'U'なら安全地帯の上端を広げる
        safe_ud[0] += 1
    elif S[i] == 'D': #攻めが'D'なら安全地帯の下端を広げる
        safe_ud[1] -= 1

    if safe_lr[1] - safe_lr[0] < 0 or safe_ud[1] - safe_ud[0] < 0: #安全地帯が無くなったらOUT
        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')

Submission status: Accepted

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