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:
import sys
input = sys.stdin.readline

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

l, r = 1, W
if S[-1] == 'L':
    l += 1
if S[-1] == 'R':
    r -= 1
for i in range(N-1)[::-1]:
    if T[i] == 'L':
        r = min(r+1, W)
    if T[i] == 'R':
        l = max(l-1, 1)

    if S[i] == 'L':
        l += 1
    if S[i] == 'R':
        r -= 1
        
    if l > r:
        print('NO')
        exit()

u, d = 1, H
if S[-1] == 'U':
    u += 1
if S[-1] == 'D':
    d -= 1
for i in range(N-1)[::-1]:
    if T[i] == 'U':
        d = min(d+1, H)
    if T[i] == 'D':
        u = max(u-1, 1)

    if S[i] == 'U':
        u += 1
    if S[i] == 'D':
        d -= 1

    if u > d:
        print('NO')
        exit()

if l <= sc <= r and u <= sr <= d:
    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()[::-1]
T = input()[::-1]

l = 1
r = W
u = 1
d = H

for i in range(N):
    if T[i] == 'L':
        r = min(W,r+1)
    elif T[i] == 'R':
        l = max(1,l-1)
    elif T[i] == 'U':
        d = min(H,d+1)
    else:
        u = max(1,u-1)
    if S[i] == 'L':
        l = l+1
    elif S[i] == 'R':
        r = r-1
    elif S[i] == 'U':
        u = u+1
    else:
        d = d-1
    if r < l or d < u:
        print('NO')
        break
else:
    if l <= sc <= r and u <= sr <= d:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W,N = map(int, input().split())
    start = tuple(map(int, input().split()))
    S = input().rstrip()
    T = input().rstrip()
    D = {
        'R': (0,1),
        'L': (0,-1),
        'U': (-1,0),
        'D': (1,0)
    }
    Dc = ['R', 'L', 'U', 'D']

    def move(current, direction):
        d = D[direction]
        return (current[0]+d[0], current[1]+d[1])

    def is_dropped(a):
        return not (1<=a[0]<=H and 1<=a[1]<=W)

    ans = True
    for i in range(4):
        cur = start
        for j in range(N):
            if S[j] == Dc[i]:
                cur = move(cur, Dc[i])
                if is_dropped(cur): ans = False
            if T[j] == Dc[i^1]:
                cur = move(cur, Dc[i^1])
                if is_dropped(cur): cur = move(cur, Dc[i])
    print('YES' if ans else 'NO')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

# 青木くんが左に動かせるということは、駒の生存可能範囲の右端を1つのばせる
# 高橋くんが左に動かせるということは、駒の生存可能範囲の左端を1つ縮める
l = 1
r = W
u = 1
d = H
for s, t in zip(S[::-1], T[::-1]):
    if t == 'L':
        r = min(r + 1, W)
    elif t == 'R':
        l = max(1, l - 1)
    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 l <= sc <= r and u <= sr <= d:
    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())

# 青木くんが左に動かせるということは、駒の生存可能範囲の右端を1つのばせる
# 高橋くんが左に動かせるということは、駒の生存可能範囲の左端を1つ縮める
l = 1
r = W
u = 1
d = H
for s, t in zip(S[::-1], T[::-1]):
    if t in ('L', 'R'):
        if t == 'L':
            r = min(r + 1, W)
        else:
            l = max(1, l - 1)
    else:
        if t == 'U':
            d = min(d + 1, H)
        else:
            u = max(u - 1, 1)

    if s in ('L', 'R'):
        if s == 'L':
            l += 1
        else:
            r -= 1
    else:
        if s == 'U':
            u += 1
        else:
            d -= 1
    if l > r or u > d:
        print('NO')
        exit()

if l <= sc <= r and u <= sr <= d:
    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())
u, d, l, r = 1, h, 1, w
while S:
  aoki = T.pop()
  if aoki == "U":
    if d<h: d += 1
  elif aoki == "D":
    if u>1: u -= 1
  elif aoki == "L":
    if r<w: r += 1
  else:
    if l>1: l -= 1
  takahashi = S.pop()
  if takahashi == "U":
    if u==d:
      print("NO")
      exit()
    u += 1
  elif takahashi == "D":
    if u==d:
      print("NO")
      exit()
    d -= 1
  elif takahashi == "L":
    if l==r:
      print("NO")
      exit()
    l += 1
  else:
    if l==r:
      print("NO")
      exit()
    r -= 1
if u<=sr<=d and l<=sc<=r:
  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()
for (x,y),(s,t) in zip(["LR","RL","UD","DU"],[(W-sc,W),(sc-1,W),(H-sr,H),(sr-1,H)]):
    C=[s]*(N+1)
    for i in range(N):
        C[i]+=S[i]==x
        C[i+1]=max(0,C[i]-(T[i]==y))
    if max(C)>=t:
        print("NO")
        break
else:
    print("YES")

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.readline

def main():
    H,W,N = map(int,input().split())
    sr,sc = map(int,input().split())
    S = input()
    T = input()
    xp,xd = sr,sr
    yp,yd = sc,sc
    
    for i in range(N):
        if S[i] == "D":
            xp += 1
            if xp == H+1:
                print("NO")
                exit()
        elif S[i] == "U":
            xd -= 1
            if xd == 0:
                print("NO")
                exit()
        elif S[i] == "R":
            yp += 1
            if yp == W+1:
                print("NO")
                exit()
        else:
            yd -= 1
            if yd == 0:
                print("NO")
                exit()
        if T[i] == "D":
            if xd != H:
                xd += 1
        elif T[i] == "U":
            if xp != 1:
                xp -= 1
        elif T[i] == "R":
            if yd != W:
                yd += 1
        else:
            if yp != 1:
                yp -= 1
            
    print("YES")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H, W, N, sr, sc, S, T = open(0).read().split()
H, W, N, sr, sc = map(int, (H,W,N,sr,sc))
dpr = [0]*(N+1)
dpl = [0]*(N+1)
dpu = [0]*(N+1)
dpd = [0]*(N+1)
dpr[0] = W-sc
dpl[0] = sc-1
dpu[0] = sr-1
dpd[0] = H-sr
for i in range(N):
  if S[i]=='R':
    dpr[i] -= 1
    if 0>dpr[i]:
      print('NO')
      break
  elif S[i]=='D':
    dpd[i] -= 1
    if 0>dpd[i]:
      print('NO')
      break
  elif S[i]=='L':
    dpl[i] -= 1
    if 0>dpl[i]:
      print('NO')
      break
  else:
    dpu[i] -= 1
    if 0>dpu[i]:
      print('NO')
      break
  if T[i]=='R':
    dpl[i+1] = min(W-1,dpl[i]+1)
    dpr[i+1] = dpr[i]
    dpd[i+1] = dpd[i]
    dpu[i+1] = dpu[i]
  elif T[i]=='L':
    dpr[i+1] = min(W-1,dpr[i]+1)
    dpl[i+1] = dpl[i]
    dpu[i+1] = dpu[i]
    dpd[i+1] = dpd[i]
  elif T[i]=='U':
    dpd[i+1] = min(H-1,dpd[i]+1)
    dpl[i+1]= dpl[i]
    dpu[i+1] = dpu[i]
    dpr[i+1] = dpr[i]
  else:
    dpu[i+1] = min(H-1,dpu[i]+1)
    dpl[i+1] = dpl[i]
    dpr[i+1] = dpr[i]
    dpd[i+1] = dpd[i]
else:
  print('YES')

Submission status: Accepted

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

L = [-1]*N
R = [W]*N
U = [-1]*N
D = [H]*N

if S[-1]=="L":
  L[-1] = 0
if S[-1]=="R":
  R[-1] = W-1
if S[-1]=="U":
  U[-1] = 0
if S[-1]=="D":
  D[-1] = H-1

ans = "YES"

for i in range(N-1)[::-1]:
  L[i] = L[i+1]
  R[i] = R[i+1]
  U[i] = U[i+1]
  D[i] = D[i+1]
  if S[i]=="L" and (T[i]!="R" or L[i]<0):
    L[i] += 1
  if S[i]!="L" and T[i]=="R":
    L[i] = max(L[i]-1, -1)
  if S[i]=="R" and (T[i]!="L" or R[i]>=W):
    R[i] -= 1
  if S[i]!="R" and T[i]=="L":
    R[i] = min(R[i]+1, W)
  if S[i]=="U" and (T[i]!="D" or U[i]<0):
    U[i] += 1
  if S[i]!="U" and T[i]=="D":
    U[i] = max(U[i]-1, -1)
  if S[i]=="D" and (T[i]!="U" or D[i]>=H):
    D[i] -= 1
  if S[i]!="D" and T[i]=="U":
    D[i] = min(D[i]+1, H)
  if L[i]+1>=R[i] or U[i]+1>=D[i]:
    ans = "NO"

if sr<=U[0] or D[0]<=sr or sc<=L[0] or R[0]<=sc:
  ans = "NO"
print ans

Submission status: Accepted

Solution code:

"""
後ろから考えていく

サンプル1のLRで考える

後手が勝つためには2ターン目終了以前に [2,3]にいる必要
→1ターン終了時に[2,2]にいること
→1ターン目初手終了時に[2,3]にいること
→最初に[2,2]にいること

→後手Lがくると右の範囲が1増える
→先手Lがくると左の範囲が1減る

最後に最初の地点が入ってれば後手が勝ち

"""
import sys

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

s,c = map(int,input().split())

S = input()
T = input()

u = 1
d = H
l = 1
r = W

for i in range(N):

    i = N - i - 1

    if T[i] == "U":
        d = min(d+1 , H)

    elif T[i] == "D":
        u = max(1 , u-1)

    elif T[i] == "L":
        r = min(r+1 , W)

    else:
        l = max(l-1 , 1)




    if S[i] == "U":
        u += 1

    elif S[i] == "D":
        d -= 1

    elif S[i] == "L":
        l += 1

    else:
        r -= 1


    if l > r or u > d or l > W or r < 1 or u > H or d < 1: 
        print ("NO")
        sys.exit()

#print (u,d,l,r)

if u <= s <= d and l <= c <= r:
    print ("YES")
else:
    print ("NO")

Submission status: Accepted

Solution code:
h,w,n = map(int,input().split())
sx,sy = map(int,input().split())
S = input()
T = input()
S = list(S[::-1])
T = list(T[::-1])
area = [1,h,1,w]
#上下左右
for s,t in zip(S,T):
  if t == "R":
    area[2] = max(1,area[2]-1)
  if t == "L":
    area[3] = min(w,area[3]+1)
  if t == "D":
    area[0] = max(1,area[0]-1)
  if t == "U":
    area[1] = min(h,area[1]+1)
  if s == "L":
    area[2] = min(w+1,area[2]+1)
  if s == "R":
    area[3] = max(0,area[3]-1)
  if s == "U":
    area[0] = min(h+1,area[0]+1)
  if s == "D":
    area[1] = max(0,area[1]-1)
  if area[0] > area[1] or area[2] > area[3]:
    print("NO")
    exit()
if area[0] <= sx <= area[1] and area[2] <= sy <= area[3]:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
from collections import defaultdict

H,W,N = map(int, input().split())
sy, sx = map(int, input().split())
T = input()
A = input()

td = defaultdict(int)
ad = defaultdict(int)

for i in range(N):
    td[T[i]] += 1

    # 落とせるか判定
    if sx + max(0, td["R"] - ad["L"]) > W \
    or sx - max(0, td["L"] - ad["R"]) < 1 \
    or sy + max(0, td["D"] - ad["U"]) > H \
    or sy - max(0, td["U"] - ad["D"]) < 1:
        print("NO")
        exit()

    if A[i] == "L":
        ad[A[i]] = min(ad[A[i]] + 1, sx - 1 + td["R"])
    elif A[i] == "R":
        ad[A[i]] = min(ad[A[i]] + 1, W - sx  + td["L"])
    elif A[i] == "U":
        ad[A[i]] = min(ad[A[i]] + 1, sy - 1 + td["D"])
    if A[i] == "D":
        ad[A[i]] = min(ad[A[i]] + 1, H - sy + td["U"])

print("YES")

Submission status: Accepted

Solution code:
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *

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


st=0
end=W-1
sttate=0
endtate=H-1
for i in range(N-1,-1,-1):
    if T[i]=="L":
        if end<W-1:
            end+=1
    if T[i]=="R":
        if st>0:
            st-=1
    if T[i]=="U":
        if endtate<H-1:
            endtate+=1
    if T[i]=="D":
        if sttate>0:
            sttate-=1

    if S[i]=="L":
        st+=1
    if S[i]=="R":
        end-=1
    if S[i]=="U":
        sttate+=1
    if S[i]=="D":
        endtate-=1
    if st>end or sttate>endtate:
        print("NO")
        exit()

if st<=sc-1<=end and sttate<=sr-1<=endtate:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

l, r = 1, W
d, u = 1, H
for s, t in zip(reversed(S), reversed(T)):

    l = max(l - (t == "R"), 1) + (s == "L")
    r = min(r + (t == "L"), W) - (s == "R")

    d = max(d - (t == "U"), 1) + (s == "D")
    u = min(u + (t == "D"), H) - (s == "U")

    if l > r or d > u:
        break

if l <= sc <= r and d <= H - sr + 1 <= u:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

h,w,n = inm()
srow,scol = inm()
s = input().strip()
t = input().strip()
t = t[:-1] + 'M'
l = r = u = d = 0
for i in range(n-1,-1,-1):
    if s[i]=='D' and (t[i-1]!='U' or d==h-1):
        d += 1
    elif s[i]!='D' and t[i-1]=='U' and d>0:
        d -= 1

    if s[i]=='U' and (t[i-1]!='D' or u==h-1):
        u += 1
    elif s[i]!='U' and t[i-1]=='D' and u>0:
        u -= 1

    if s[i]=='L' and (t[i-1]!='R' or l==w-1):
        l += 1
    elif s[i]!='L' and t[i-1]=='R' and l>0:
        l -= 1

    if s[i]=='R' and (t[i-1]!='L' or r==w-1):
        r += 1
    elif s[i]!='R' and t[i-1]=='L' and r>0:
        r -= 1

    if l+r>=w or u+d>=h:
        print('NO')
        exit()

if 1+l<=scol<=w-r and 1+u<=srow<=h-d:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations
from itertools import accumulate
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

h,w,n=map(int,input().split())
src=list(map(int,input().split()))
s=input()
t=input()
lrud=["L","U","D","R"]
dic={"L":[0,-1],"R":[0,1],"U":[-1,0],"D":[1,0]}
for i in range(4):
    tmp=src[:]
    # print(lrud[i],lrud[-1-i])
    for j in range(n):
        if s[j]==lrud[i]:
            tmp[0]+=dic[s[j]][0]
            tmp[1]+=dic[s[j]][1]
        if lrud[i]=="L" and tmp[1]==0:
            print("NO")
            quit()
        if lrud[i]=="R" and tmp[1]==w+1:
            print("NO")
            quit()
        if lrud[i]=="U" and tmp[0]==0:
            print("NO")
            quit()
        if lrud[i]=="D" and tmp[0]==h+1:
            print("NO")
            quit()
        # print(tmp)
        if t[j]==lrud[-1-i]:
            if 0<tmp[0]+dic[t[j]][0]<=h:
                tmp[0]+=dic[t[j]][0]
            if 0<tmp[1]+dic[t[j]][1]<=w:
                tmp[1]+=dic[t[j]][1]
else:
    print("YES")

Submission status: Accepted

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/agc033/tasks/agc033_b
# B - LRUD Game
"""
"""
H = gi()
W = gi()
N = gi()
sr = gi()
sc = gi()
S = gw()
T = gw()
d_to_i = {"L": 0, "R": 1, "U": 2, "D": 3}
cnt = [[0 for x in range(4)] for i in range(2)]


def o(pos, si, ti, d, sb, tb):
    for i in range(N):
        if (d_to_i[S[i]] == si):
            pos += d
            if pos == sb:
                return 1
        if (d_to_i[T[i]] == ti):
            if (pos - d != tb):
                pos -= d
    return 0


off = 0
#  print(cnt)
off = o(sc, 0, 1, -1, 0, W + 1)
off = off or o(sc, 1, 0, 1, W + 1, 0)
off = off or o(sr, 2, 3, -1, 0, H + 1)
off = off or o(sr, 3, 2, 1, H + 1, 0)

if off:
    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()

Rc = Lc = sc
Uc = Dc = sr

for i in range(N):
  if   S[i] == "L":
    Lc -= 1
  elif S[i] == "R":
    Rc += 1
  elif S[i] == "U":
    Uc -= 1
  elif S[i] == "D":
    Dc += 1
  
  if Lc < 1 or W < Rc or Uc < 1 or H < Dc:
    print("NO")
    break

  if   T[i] == "L" and 1 < Rc:
    Rc -= 1
  elif T[i] == "R" and Lc < W:
    Lc += 1
  elif T[i] == "U" and 1 < Dc:
    Dc -= 1
  elif T[i] == "D" and Uc < H:
    Uc += 1
else:
  print("YES")

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    H, W, N = LI()
    start = LI()
    S = SI(); T = SI()
    for t,a in [["U","D"],["R","L"]]:
        if t=="U":
            n = H
        else:
            n = W
        # 落ちないエリア
        l = 1; r = n
        if S[-1]==t:
            r -=1
        elif S[-1]==a:
            l +=1
        for i in range(N-2,-1,-1):
            if T[i]==t and l>1:
                l -= 1
            elif T[i]==a and r<n:
                r += 1
            if S[i] == t:
                r -= 1
            elif S[i] == a:
                l += 1
#            print(l,r)
            if l>r:
                print("NO")
                return
            if t=="U" and (r==0 or l==n):
                print("NO")
                return
            if t=="R" and (r==0 or l==n):
                print("NO")
                return

        if t=="U" and (not l<=H + 1 -start[0]<=r):
            print("NO")
            return
        elif t=="R" and (not l<=start[1]<=r):
            print("NO")
            return
    print("YES")
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

# maspyさん
def examB():
    H, W, N = LI()
    sr, sc = LI()
    S = SI()
    T = SI()

    sr = H + 1 - sr

    # ここに居たら後手の価値
    L, R = 1, W
    D, U = 1, H
    for s, t in zip(S[::-1], T[::-1]):
        if t == 'L':
            R += 1
        elif t == 'D':
            U += 1
        elif t == 'R':
            L -= 1
        elif t == 'U':
            D -= 1

        if L == 0: L = 1
        if D == 0: D = 1
        if R > W: R = W
        if U > H: U = H

        if s == 'L':
            L += 1
        elif s == 'R':
            R -= 1
        elif s == 'D':
            D += 1
        elif s == 'U':
            U -= 1

        if L > R or D > U:
            # 後手の勝てる場所が存在せず
            break

    bl = (L <= sc <= R) and (D <= sr <= U)
    answer = 'YES' if bl else 'NO'
    print(answer)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Accepted

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

S = S[::-1]
T = T[::-1]

left = 0
right = W - 1
for i in range(N):
  if T[i] == "L":
    right = min(right + 1, W - 1)
  if T[i] == "R":
    left = max(left - 1, 0)
  if S[i] == "L":
    left = left + 1
  if S[i] == "R":
    right = right - 1
  if left > right:
    print("NO")
    exit()

if left <= sc - 1<= right:
  1
else:
  print("NO")
  exit()
  
up = 0
down = H - 1
for i in range(N):
  if T[i] == "U":
    down = min(down + 1, H - 1)
  if T[i] == "D":
    up = max(up - 1, 0)
  if S[i] == "U":
    up = up + 1
  if S[i] == "D":
    down = down - 1
  if up > down:
    print("NO")
    exit()
  
if up <= sr - 1 <= 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()
now = [sc,sc,sr,sr]
ans = "YES"
for i,j in zip(s,t):
  if i == "L":
    now[0] -= 1
    if now[0] <= 0:
      ans = "NO"
      break
  elif i == "R":
    now[1] += 1
    if now[1] > w:
      ans = "NO"
      break
  elif i == "U":
    now[2] -= 1
    if now[2] <= 0:
      ans = "NO"
      break
  else:
    now[3] += 1
    if now[3] > h:
      ans = "NO"
      break
  if j == "L":
    now[1] = max(1,now[1]-1)
  elif j == "R":
    now[0] = min(w,now[0]+1)
  elif j == "U":
    now[3] = max(1,now[3]-1)
  else:
    now[2] = min(h,now[2]+1)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
H, W, N = map(int, input().split())
y, x = map(int, input().split())
S = list(input())[: -1]
T = list(input())[: -1]
def no():
  print("NO")
  exit(0)

v = x + 0
for i in range(N):
  if S[i] == "R": v += 1
  if v > W or v < 1: no()
  if T[i] == "L":
    if v > 1:
      v -= 1
  if v > W or v < 1: no()


v = x + 0
for i in range(N):
  if S[i] == "L": v -= 1
  if v > W or v < 1: no()
  if T[i] == "R":
    if v < W:
      v += 1
  if v > W or v < 1: no()


v = y + 0
for i in range(N):
  if S[i] == "U": v -= 1
  if v > H or v < 1: no()
  if T[i] == "D":
    if v < H:
      v += 1
  if v > H or v < 1: no()

v = y + 0
for i in range(N):
  if S[i] == "D": v += 1
  if v > H or v < 1: no()
  if T[i] == "U":
    if v > 1:
      v -= 1
  if v > H or v < 1: no()

print("YES")

Submission status: Accepted

Solution code:
H,W,N = map(int,input().split())
sr,sc = map(int,input().split())
S = input().strip()
T = input().strip()
cur = sc
flag=0
for i in range(N):
    if S[i]=="R":
        cur += 1
        if cur>W:
            flag = 1
            break
    if cur>1 and T[i]=="L":
        cur -= 1
cur = sc
for i in range(N):
    if S[i]=="L":
        cur -= 1
        if cur<1:
            flag = 1
            break
    if cur<W and T[i]=="R":
        cur += 1
cur = sr
for i in range(N):
    if S[i]=="U":
        cur -= 1
        if cur<1:
            flag = 1
            break
    if cur<H and T[i]=="D":
        cur += 1
cur = sr
for i in range(N):
    if S[i]=="D":
        cur += 1
        if cur>H:
            flag = 1
            break
    if cur>1 and T[i]=="U":
        cur -= 1
if flag==1:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

H,W,N = II()
sr,sc = II()
S = str(input())
T = str(input())

for i in range(N)[::-1]:
    if i==N-1:
        l = 1+(int(S[i]=='L'))
        r = W-(int(S[i]=='R'))
        u = 1+(int(S[i]=='U'))
        d = H-(int(S[i]=='D'))
    else:
        l = max(l+int(S[i]=='L')-int(T[i]=='R'), 1+(int(S[i]=='L')))
        r = min(r-int(S[i]=='R')+int(T[i]=='L'), W-(int(S[i]=='R')))
        u = max(u+int(S[i]=='U')-int(T[i]=='D'), 1+(int(S[i]=='U')))
        d = min(d-int(S[i]=='D')+int(T[i]=='U'), H-(int(S[i]=='D')))
    if l>r or u>d:
        print('NO')
        exit()

if l<=sc<=r and u<=sr<=d:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import sys

H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
sr-=1
sc-=1
S=list(input())
T=list(input())
#4方向全てにおいて阻止できるか
dic1={"U":0,"D":1,"R":2,"L":3}
dic2={"U":1,"D":0,"R":3,"L":2}
dic3={"U":-1,"D":1,"R":1,"L":-1}
data=[[sr],[sr],[sc],[sc]]
for i in range(N):
    data[dic1[S[i]]].append(dic3[S[i]])
    data[dic2[T[i]]].append(dic3[T[i]])

h=data[0]
for i in range(1,len(h)):
    h[i]=min(H-1,h[i]+h[i-1])
    if h[i]==-1:
        print("NO")
        sys.exit()
h=data[1]
for i in range(1,len(h)):
    h[i]=max(0,h[i]+h[i-1])
    if h[i]==H:
        print("NO")
        sys.exit()
h=data[3]
for i in range(1,len(h)):
    h[i]=min(W-1,h[i]+h[i-1])
    if h[i]==-1:
        print("NO")
        sys.exit()
h=data[2]
for i in range(1,len(h)):
    h[i]=max(0,h[i]+h[i-1])
    if h[i]==W:
        print("NO")
        sys.exit()
print("YES")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    h,w,n=map(int,input().split())
    i0,j0=map(int,input().split())
    S=input()
    T=input()

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

        if(s=='U'): u+=1
        elif(s=='D'): d-=1
        elif(s=='L'): l+=1
        elif(s=='R'): r-=1
        if(u>d or l>r):
            print("NO")
            return

    print("YES" if(u<=i0<=d and l<=j0<=r) else "NO")
resolve()

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(int, input().split())
l, r = 1, w
d, u = 1, h
for a, b in zip(input()[::-1], input()[::-1]):
	r = min(r + (b == "L"), w) - (a == "R")
	l = max(l - (b == "R"), 1) + (a == "L")
	u = min(u + (b == "D"), h) - (a == "U")
	d = max(d - (b == "U"), 1) + (a == "D")
	if l > r or d > u:break
print("YES" if l <= sx <= r and d <= h - sy + 1 <= u else "NO")

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

H, W, N = list(map(int, sys.stdin.buffer.readline().split()))
h, w = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
T = sys.stdin.buffer.readline().decode().rstrip()


def solve():
    # 縦横は独立に考えられる
    # これより左右にいたらダメ
    l = 0
    r = W + 1
    if S[-1] == 'L':
        l += 1
    if S[-1] == 'R':
        r -= 1
    for i in reversed(range(N - 1)):
        if T[i] == 'L':
            r += 1
            r = min(r, W + 1)
        if T[i] == 'R':
            l -= 1
            l = max(l, 0)

        if S[i] == 'L':
            l += 1
            l = max(l, 0)
        if S[i] == 'R':
            r -= 1
            r = min(r, W + 1)

        if W <= l:
            return False
        if r <= 1:
            return False
    if not l < w < r:
        return False

    u = 0
    d = H + 1
    if S[-1] == 'U':
        u += 1
    if S[-1] == 'D':
        d -= 1
    for i in reversed(range(N - 1)):
        if T[i] == 'U':
            d += 1
            d = min(d, H + 1)
        if T[i] == 'D':
            u -= 1
            u = max(u, 0)

        if S[i] == 'U':
            u += 1
            u = max(u, 0)
        if S[i] == 'D':
            d -= 1
            d = min(d, H + 1)

        if H <= u:
            return False
        if d <= 1:
            return False
    if not u < h < d:
        return False

    return True


ok = solve()
if ok:
    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()

y=sr
for i,j in zip(s,t):
    if i=="U":
        y-=1
        if y==0:
            print("NO")
            exit()
    if j=="D" and y<h:
        y+=1
y=sr
for i,j in zip(s,t):
    if i=="D":
        y+=1
        if y==h+1:
            print("NO")
            exit()
    if j=="U" and y>1:
        y-=1

x=sc
for i,j in zip(s,t):
    if i=="L":
        x-=1
        if x==0:
            print("NO")
            exit()
    if j=="R" and x<w:
        x+=1

x=sc
for i,j in zip(s,t):
    if i=="R":
        x+=1
        if x==w+1:
            print("NO")
            exit()
    if j=="L" and x>1:
        x-=1

print("YES")

Submission status: Accepted

Solution code:
from collections import defaultdict

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

def solve() :
    l, r = 1, W
    d, u = 1, H
    
    for i in range(N - 1, -1, -1) :
        if T[i] == 'L' :
            r = min(r + 1, W)
        elif T[i] == 'R' :
            l = max(l - 1, 1)
        elif T[i] == 'D' :
            u = min(u + 1, H)
        elif T[i] == 'U' :
            d = max(d - 1, 1)
            
        if S[i] == 'L' :
            l += 1
        elif S[i] == 'R' :
            r -= 1
        elif S[i] == 'D' :
            d += 1
        elif S[i] == 'U' :
            u -= 1
            
        if r < l or u < d :
            return 'NO'

    if l <= sc <= r and d <= H + 1 - sr <= u :
        return 'YES'
    else :
        return 'NO'
    
print(solve())