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:
#loud game
h,w,n=map(int,input().split())
sr,sc=map(int,input().split())
s=input()
t=input()

sr_deque=[]
sc_deque=[]

for i in range(n):
    p=s[i]
    if p=="U":
        #(X,people)=いくつ進めるか、どちらの人間か
        sr_deque.append((-1,1))
    elif p=="D":
        sr_deque.append((1,1))
    q=t[i]
    if q=="U":
        #(X,people)=いくつ進めるか、どちらの人間か
        sr_deque.append((-1,2))
    elif q=="D":
        sr_deque.append((1,2))      

for i in range(n):
    p=s[i]
    if p=="R":
        #(X,people)=いくつ進めるか、どちらの人間か
        sc_deque.append((1,1))
    elif p=="L":
        sc_deque.append((-1,1))
    q=t[i]
    if q=="R":
        #(X,people)=いくつ進めるか、どちらの人間か
        sc_deque.append((1,2))
    elif q=="L":
        sc_deque.append((-1,2)) 
        
sr_deque=sr_deque[::-1]
sc_deque=sc_deque[::-1]

def judge(start_node,width,deque):
    safe_left=1
    safe_right=width
    flags=True
    for que in deque:
        if que[1]==1:
            #消してしまいたい
            if que[0]==1:
                if safe_right>0:
                    safe_right-=1
           
            elif que[0]==-1:
                if safe_left<width+1:
                    safe_left+=1 
            
        if que[1]==2:
            #残しておきたい
            if que[0]==1:
                if safe_left>1:
                    safe_left-=1
                
                
            elif que[0]==-1:
                if safe_right<width:
                    safe_right+=1
 
        #境界条件
        if safe_left>safe_right:
            return False
    return safe_left<=start_node<=safe_right
  
def safe():
    return judge(sr,h,sr_deque) and judge(sc,w,sc_deque)
if safe():
    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()
p1 = sc - 1
p2 = sc - 1
q1 = sr - 1
q2 = sr - 1
for i in range(n):
    if s[i] == 'R':
        p1 += 1
    elif s[i] == 'L':
        p2 -= 1
    elif s[i] == 'U':
        q1 -= 1
    else:
        q2 += 1
    if p1 >= w or p2 < 0 or q1 < 0 or q2 >= h:
        print('NO')
        exit(0)
    if t[i] == 'R':
        p2 += 1
        if p2 >= w:
            p2 -= 1
    elif t[i] == 'L':
        p1 -= 1
        if p1 < 0:
            p1 += 1
    elif t[i] == 'U':
        q2 -= 1
        if q2 < 0:
            q2 += 1
    else:
        q1 += 1
        if q1 >= h:
            q1 -= 1
print('YES')

Submission status: Accepted

Solution code:
h,w,n = map(int,input().split())
s = list(map(int,input().split()))
S = input()
T = input()

def YES(b):
    if not b:
        print('YES')
    else:
        print('NO')
    exit(0)
    return

R = [s[0],s[0]]
C = [s[1],s[1]]

for i in range(n):
    if S[i] == 'L':
        C[0] -= 1
        if not (1 <= C[0] <= w):YES(True)
    elif S[i] == 'R':
        C[1] += 1
        if not (1 <= C[1] <= w):YES(True)
    elif S[i] == 'U':
        R[0] -= 1
        if not (1 <= R[0] <= h):YES(True)
    elif S[i] == 'D':
        R[1] += 1
        if not (1 <= R[1] <= h):YES(True)
    if T[i] == 'R' and C[0] != w:
        C[0] += 1
    elif T[i] == 'L' and C[1] != 1:
        C[1] -= 1
    elif T[i] == 'D' and R[0] != h:
        R[0] += 1
    elif T[i] == 'U' and R[1] != 1:
        R[1] -= 1
YES(False)

Submission status: Accepted

Solution code:
H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
S=input()
T=input()
mtl=S.count("L")
mtr=S.count("R")
mtu=S.count("U")
mtd=S.count("D")
mal=T.count("L")
mar=T.count("R")
mau=T.count("U")
mad=T.count("D")
tl,tr,tu,td=0,0,0,0
al,ar,au,ad=0,0,0,0
a=1
for i in range(N):
    if S[i]=="L":
        tl+=1
        if sc-tl+ar==0:
            a=0
            break
    elif S[i]=="R":
        tr+=1
        if sc+tr-al>W:
            a=0
            break
    elif S[i]=="U":
        tu+=1
        if sr-tu+ad==0:
            a=0
            break
    else:
        td+=1
        if sr+td-au>H:
            a=0
            break
    if T[i]=="L":
        al+=1
        if mtr-tr-mal+al>=W:
            a=0
            break
    elif T[i]=="R":
        ar+=1
        if mtl-tl-mar+ar>=W:
            a=0
            break
    elif T[i]=="U":
        au+=1
        if mtd-td-mau+au>=H:
            a=0
            break
    else:
        ad+=1
        if mtu-tu-mad+ad>=H:
            a=0
            break
print("YES" if a else "NO")

Submission status: Accepted

Solution code:
# たとえばLを目指す場合、
# 高橋くんは出てきたLをすべて使い、Rは使わない
# 青木君は出てきたRをすべて使い、Lを使わない
# 最初の位置が(r,c)とすると、Sに現れるLの個数がTに現れるRの個数を上回るタイミングがあれば
# 外に出すことができる。（同時に同じ個数になったときは先手の高橋君が外に出す）
# 4回ループ回してOK

H,W,N = map(int,input().split())
r,c = map(int,input().split())
S = input()
T = input()

# Lを試す
xl = c
for i in range(N):
  if S[i] == "L":
    xl -= 1
    if xl == 0:
      print("NO")
      exit(0)
  if T[i] == "R":
    if xl + 1 <= W:
      xl += 1
    

# Rを試す
xr = c
for i in range(N):
  if S[i] == "R":
    xr += 1
    if xr > W:
      print("NO")
      exit(0)
  if T[i] == "L":
    if xr - 1 > 0:
      xr -= 1

# Uを試す
xu = r
for i in range(N):
  if S[i] == "U":
    xu -= 1
    if xu == 0:
      print("NO")
      exit(0)
  if T[i] == "D":
    if xu + 1 <= H:
      xu += 1

# Dを試す
xd = r
for i in range(N):
  if S[i] == "D":
    xd += 1
    if xd > H:
      print("NO")
      exit(0)
  if T[i] == "U":
    if xd - 1 > 0:
      xd -= 1

print("YES")

Submission status: Accepted

Solution code:
h,w,n=map(int,input().split())
si,sj=map(int,input().split())
ss=input()
tt=input()
l=1
u=1
r=w
d=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')

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    H, W, N = map(int, rl().split())
    sr, sc = map(lambda x: int(x) - 1, rl().split())
    S = input()
    T = input()
    
    cy = sr
    for i in range(N):
        if S[i] == 'U':
            cy -= 1
            if cy < 0:
                print('NO')
                exit()
        if T[i] == 'D' and cy < H - 1:
            cy += 1
    
    cy = sr
    for i in range(N):
        if S[i] == 'D':
            cy += 1
            if H <= cy:
                print('NO')
                exit()
        if T[i] == 'U' and 0 < cy:
            cy -= 1
    
    cx = sc
    for i in range(N):
        if S[i] == 'L':
            cx -= 1
            if cx < 0:
                print('NO')
                exit()
        if T[i] == 'R' and cx < W - 1:
            cx += 1
    
    cx = sc
    for i in range(N):
        if S[i] == 'R':
            cx += 1
            if W <= cx:
                print('NO')
                exit()
        if T[i] == 'L' and 0 < cx:
            cx -= 1
    
    print('YES')


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

h, w,n = map(int, input().split())
r,c = map(int, input().split())
s = input() #sys.stdin.readlineは最後が改行
t = input() #sys.stdin.readlineは最後が改行

# toR
x=c
for i in range(n):
    if s[i] == 'R' : x+=1
    if x == w+1:
        print('NO')
        exit()
    if t[i] == 'L' and x>1 : x-=1

# toL
x=c
for i in range(n):
    if s[i] == 'L' : x-=1
    if x == 0:
        print('NO')
        exit()
    if t[i] == 'R' and x<w: x+=1

# toU
y=r
for i in range(n):
    if s[i] == 'U' : y-=1
    if y == 0:
        print('NO')
        exit()
    if t[i] == 'D' and y<h: y+=1

# toD
y=r
for i in range(n):
    if s[i] == 'D' : y+=1
    if y == h+1:
        print('NO')
        exit()
    if t[i] == 'U' and y>1: y-=1

print('YES')

Submission status: Accepted

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

command = []
for s, t in zip(S, T):
    command.append(s)
    command.append(t)

L, U, R, D = sw, sh, sw, sh
taka = 1
for c in command:
    if c == "R":
        if taka:
            R += 1
        else:
            L += 1
            L = min(L, W)
    elif c == "L":
        if taka:
            L -= 1
        else:
            R -= 1
            R = max(R, 1)
    elif c == "U":
        if taka:
            U -= 1
        else:
            D -= 1
            D = max(1, D)
    elif c == "D":
        if taka:
            D += 1
        else:
            U += 1
            U = min(U, H)
    taka = 1 - taka
    if R == W + 1 or L == 0 or U == 0 or D == H + 1:
        print("NO")
        exit()

print("YES")

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
s, t =  input(), input()
l, r, u, d = sx, sx, sy, sy

for i in range(n):
    if s[i] == "L": l -= 1
    elif s[i] == "R": r += 1
    elif s[i] == "U": u -= 1
    else: d += 1
    if l < 0 or r >= w or u < 0 or d >= h:
        print("NO")
        break
    if t[i] == "L" and r > 0: r -= 1
    elif t[i] == "R" and l < w-1: l += 1
    elif t[i] == "U" and d > 0: d -= 1
    elif t[i] == "D" and u < h-1: u += 1
else: print("YES")

Submission status: Accepted

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

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

for s, t in zip(S[::-1], T[::-1]):
    if left + 1 >= right or up + 1 >= down:
        break

    if t == 'L':
        right = min(W + 1, right + 1)
    if t == 'R':
        left = max(0, left - 1)
    if t == 'U':
        down = min(H + 1, down + 1)
    if t == 'D':
        up = max(0, up - 1)

    if s == 'R':
        right -= 1
    if s == 'L':
        left += 1
    if s == 'D':
        down -= 1
    if s == 'U':
        up += 1

print('YES' if left < sw < right and up < sh < down else 'NO')

Submission status: Accepted

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

ok = True
x = sr
for i, j in zip(s, t):
    if i == "U":
        x -= 1
    if not 1 <= x <= h:
        ok = False
    if j == "D":
        x += 1
    if not 1 <= x <= h:
        x -= 1

x = sr
for i, j in zip(s, t):
    if i == "D":
        x += 1
    if not 1 <= x <= h:
        ok = False
    if j == "U":
        x -= 1
    if not 1 <= x <= h:
        x += 1
x = sc
for i, j in zip(s, t):
    if i == "L":
        x -= 1
    if not 1 <= x <= w:
        ok = False
    if j == "R":
        x += 1
    if not 1 <= x <= w:
        x -= 1

x = sc
for i, j in zip(s, t):
    if i == "R":
        x += 1
    if not 1 <= x <= w:
        ok = False
    if j == "L":
        x -= 1
    if not 1 <= x <= w:
        x += 1

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 = list(str(input()))
T = list(str(input()))

y = sr
for s,t in zip(S, T):
    if s == 'U':
        y -= 1
        if y < 1:
            print('NO')
            exit()
    if t == 'D':
        if y < h:
            y += 1

y = sr
for s, t in zip(S, T):
    if s == 'D':
        y += 1
        if y > h:
            print('NO')
            exit()
    if t == 'U':
        if y > 1:
            y -= 1

x = sc
for s, t in zip(S, T):
    if s == 'R':
        x += 1
        if x > w:
            print('NO')
            exit()
    if t == 'L':
        if x > 1:
            x -= 1

x = sc
for s, t in zip(S, T):
    if s == 'L':
        x -= 1
        if x < 1:
            print('NO')
            exit()
    if t == 'R':
        if x < w:
            x += 1

print('YES')

Submission status: Accepted

Solution code:
H, W, N = map(int, input().split())
sh, sw = map(int, input().split())

S = input()
T = input()

def canTakaWin(toS, isInc, toT, R, c):
    now = c

    for s, t in zip(S, T):
        if s == toS:
            now = (now + 1) if isInc else (now - 1)

        if not 0 < now <= R:
            return True

        if t == toT:
            now = (now + 1) if not isInc else (now - 1)
            now = max(1, now)
            now = min(R, now)

    return False

ans = False
ans = ans or canTakaWin('U', False, 'D', H, sh)
ans = ans or canTakaWin('D', True, 'U', H, sh)
ans = ans or canTakaWin('L', False, 'R', W, sw)
ans = ans or canTakaWin('R', True, 'L', W, sw)

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

Submission status: Accepted

Solution code:
def main():
    # 嘘解法
    # 2 3 4
    # 2 2
    # ULRR
    # LUUU
    # はNo(Yesと出る)
    H, W, N = (int(i) for i in input().split())
    sr, sc = (int(i) for i in input().split())
    S = input()
    T = input()
    from collections import Counter
    S_C = Counter(S)
    if sc <= S_C['L']:
        move = 0
        for s, t in zip(S, T):
            if s == "L":
                move += 1
            if sc <= move:
                return print("NO")
            if t == "R":
                move -= 1
                if -move == W - sc + 1:
                    move += 1
    if W - sc + 1 <= S_C['R']:
        move = 0
        for s, t in zip(S, T):
            if s == "R":
                move += 1
            if W - sc + 1 <= move:
                return print("NO")
            if t == "L":
                move -= 1
                if -move == sc:
                    move += 1
    if sr <= S_C['U']:
        move = 0
        for s, t in zip(S, T):
            if s == "U":
                move += 1
            if sr <= move:
                return print("NO")
            if t == "D":
                move -= 1
                if -move == H - sr + 1:
                    move += 1
    if H - sr + 1 <= S_C['D']:
        move = 0
        for s, t in zip(S, T):
            if s == "D":
                move += 1
            if H - sr + 1 <= move:
                return print("NO")
            if t == "U":
                move -= 1
                if -move == sr:
                    move += 1
    print("YES")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    H, W, N = (int(i) for i in input().split())
    sr, sc = (int(i) for i in input().split())
    S = input()
    T = input()
    from collections import Counter
    S_C = Counter(S)
    if sc <= S_C['L']:
        move = 0
        for s, t in zip(S, T):
            if s == "L":
                move += 1
            if sc <= move:
                return print("NO")
            if t == "R":
                move -= 1
                if -move == W - sc + 1:
                    move += 1
    if W - sc + 1 <= S_C['R']:
        move = 0
        for s, t in zip(S, T):
            if s == "R":
                move += 1
            if W - sc + 1 <= move:
                return print("NO")
            if t == "L":
                move -= 1
                if -move == sc:
                    move += 1
    if sr <= S_C['U']:
        move = 0
        for s, t in zip(S, T):
            if s == "U":
                move += 1
            if sr <= move:
                return print("NO")
            if t == "D":
                move -= 1
                if -move == H - sr + 1:
                    move += 1
    if H - sr + 1 <= S_C['D']:
        move = 0
        for s, t in zip(S, T):
            if s == "D":
                move += 1
            if H - sr + 1 <= move:
                return print("NO")
            if t == "U":
                move -= 1
                if -move == sr:
                    move += 1
    print("YES")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    # LR
    left_limit = 0
    right_limit = w
    for i in range(n-1, -1, -1):
        if t[i] == 'L' and right_limit < w:
            right_limit += 1
        elif t[i] == 'R' and left_limit > 0:
            left_limit -= 1
        if s[i] == 'L':
            left_limit += 1
        elif s[i] == 'R':
            right_limit -= 1
        if left_limit == right_limit:
            print('NO')
            return

    if sc <= left_limit or sc > right_limit:
        print('NO')
        return

    # UD
    up_limit = 0
    down_limit = h
    for i in range(n-1, -1, -1):
        if t[i] == 'U' and down_limit < h:
            down_limit += 1
        elif t[i] == 'D' and up_limit > 0:
            up_limit -= 1
        if s[i] == 'U':
            up_limit += 1
        elif s[i] == 'D':
            down_limit -= 1
        if up_limit == down_limit:
            print('NO')
            return

    if sr <= up_limit or sr > down_limit:
        print('NO')
        return

    print('YES')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W, N = map(int, input().split())
s_y, s_x = map(int, input().split())
S = str(input())
T = str(input())

# 高橋: 落とそうとする
# 青木: 残そうとする

lr = []
ud = []

for tup in zip(S, T):
    for n, i in enumerate(tup):
        if i == "L" or i == "R":
            lr.append((n, i))
        else:
            ud.append((n, i))

def f():
    crd = s_x
    # 貪欲に左へ
    # i == 0 -> 高橋
    # i == 1 -> 青木
    for i, j in lr:
        if i == 0 and j == "L":
            crd -= 1
        if i == 1 and j == "R" and crd < W:
            crd += 1
        if crd == 0:
            return 0
    # 貪欲に右へ
    crd = s_x
    for i, j in lr:
        if i == 0 and j == "R":
            crd += 1
        if i == 1 and j == "L" and crd > 1:
            crd -= 1
        if crd == W + 1:
            return 0
    # 貪欲に上へ
    crd = s_y
    for i, j in ud:
        if i == 0 and j == "U":
            crd -= 1
        if i == 1 and j == "D" and crd < H:
            crd += 1
        if crd == 0:
            return 0
    # 貪欲に下へ
    crd = s_y
    for i, j in ud:
        if i == 0 and j == "D":
            crd += 1
        if i == 1 and j == "U" and crd > 1:
            crd -= 1
        if crd == H + 1:
            return 0
    return 1

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    s, r = map(int, input().split())
    S = input().strip("\n")
    T = input().strip("\n")
    Lb, Rb, Ub, Db = -1, W, -1, H
    for i in reversed(range(N)):
        if i != N - 1:
            if T[i] == "L": Rb = min(Rb + 1, W)
            elif T[i] == "R": Lb = max(Lb - 1, -1)
            elif T[i] == "U": Db = min(Db + 1, H)
            elif T[i] == "D": Ub = max(Ub - 1, 0)

        if S[i] == "L": Lb = Lb + 1
        elif S[i] == "R": Rb = Rb - 1
        elif S[i] == "U": Ub = Ub + 1
        elif S[i] == "D": Db = Db - 1

        if Lb >= W - 1 or Rb <= 0 or Ub >= H - 1 or Db <= 0:
            print("NO")
            break
    else:
        if Lb < r - 1 < Rb and Ub < s - 1 < Db: print("YES")
        else: print("NO")
    
    #print(Lb, Rb, Ub, Db)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ans = 'YES'

x = sc - 1
for i in range(N):
    if S[i] == 'L': x -= 1
    # print("# L", i, x)
    if x < 0: ans = 'NO'
    if T[i] == 'R': x = min(x + 1, W - 1)

x = sc - 1
for i in range(N):
    if S[i] == 'R': x += 1
    # print("# R", i, x)
    if x >= W: ans = 'NO'
    if T[i] == 'L': x = max(x - 1, 0) 
    
y = sr - 1
for i in range(N):
    if S[i] == 'U': y -= 1
    # print("# U", i, y)
    if y < 0: ans = 'NO'
    if T[i] == 'D': y = min(y + 1, H - 1)
    
y = sr - 1
for i in range(N):
    if S[i] == 'D': y += 1
    # print("# D", i, y)
    if y >= H: ans = 'NO'
    if T[i] == 'U': y = max(y - 1, 0)

print(ans)

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
src = list(map(int,input().split()))

s = input()
t = input()
s_c, t_c = [], []
for i in ["U", "D", "L", "R"]:
    s_c.append(s.count(i))
    t_c.append(t.count(i))
if (src[0] <= s_c[0]-t_c[1]) or (h-src[0]+1<=s_c[1]-t_c[0] )or (src[1]<=s_c[2]-t_c[3] )or (w-src[1]+1<=s_c[3]-s_c[2]):
    result = "NO"
else:
    result = "YES"
    for i in range(n):
        if s[i]=="U" and src[0]<=h-src[0]+1:
            src[0] -= 1
        elif s[i]=="D" and h-src[0]+1<=src[0]:
            src[0] += 1
        elif s[i]=="L" and src[1]<=w-src[1]+1:
            src[1] -= 1
        elif s[i]=="R" and w-src[1]+1<=src[1]:
            src[1] += 1
        if src[0]<1 or src[0]>h or src[1]<1 or src[1]>w:
            result = "NO"
            break

        if t[i]=="U" and src[0]>h-src[0]+1:
            src[0] -= 1
        elif t[i]=="D" and h-src[0]+1>src[0]:
            src[0] += 1
        elif t[i]=="L" and src[1]>w-src[1]+1:
            src[1] -= 1
        elif t[i]=="R" and w-src[1]+1>src[1]:
            src[1] += 1

print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    h, w, n = LI()
    sr, sc = LI()
    s = S()[::-1]
    t = S()[::-1]
    
    plus = 0
    minus = 1

    def f(l, start, end):
        tmp = [1, end]
        for i in range(n):
            if t[i] == l[plus]:
                tmp[0] -= 1
                tmp[0] = max(tmp[0], 1)
            elif t[i] == l[minus]:
                tmp[1] += 1
                tmp[1] = min(end, tmp[1])
            if s[i] == l[plus]:
                tmp[1] -= 1
            elif s[i] == l[minus]:
                tmp[0] += 1
            if tmp[1] < tmp[0]:
                return False
        return tmp[0] <= start <= tmp[1]
    
    print("YES" if f(["R", "L"], sc, w) and f(["D", "U"], sr, h) else "NO")
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
S = input()
T = input()
hor = [1,w]
ver = [1,h]
flag1 = True
flag2 = True
for i in range(n):
    k = T[n-i-1]
    if (hor[0] == 1 and hor[1] == w):
        None
    else:
        if k == 'L' and hor[1] != w:
            hor[1] += 1
        elif k == 'R' and hor[0] != 1:
            hor[0] -= 1
    k = S[n-i-1]
    if (hor[0] == w and k == 'R') or (hor[1] == 1 and k == 'L'):
        flag1 = False
        break
    else:
        if k == 'L':
            hor[0] += 1
        elif k == 'R':
            hor[1] -= 1
    if hor[0] > hor[1]:
        flag1 = False
        break
for i in range(n):
    k = T[n-i-1]
    if (ver[0] == 1 and ver[1] == h):
        None
    else:
        if k == 'U' and ver[1] != h:
            ver[1] += 1
        elif k == 'D' and ver[0] != 1:
            ver[0] -= 1
    k = S[n-i-1]
    if (ver[0] == h and k == 'D') or (ver[1] == 1 and k == 'U'):
        flag2 = False
        break
    else:
        if k == 'U':
            ver[0] += 1
        elif k == 'D':
            ver[1] -= 1
    if ver[0] > ver[1]:
        flag2 = False
        break
if flag1 == True and flag2 == True:
    if hor[0] <= sc <= hor[1]:
        if ver[0] <= sr <= ver[1]:
            print('YES')
        else:
            print('NO')
    else:
        print('NO')
else:
    print('NO')

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  sr, sc = sr-1, sc-1
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d):
    if d=='L':
      r[L] = (min(max(-1, r[L]+1), w))
    if d=='R':
      r[R] = (min(max(-1, r[R]-1), w))
    if d=='U':
      r[U] = (min(max(-1, r[U]+1), h))
    if d=='D':
      r[D] = (min(max(-1, r[D]-1), h))
      
  def g(d):
    if d=='L':
      r[R] = (min(max(0, r[R]+1), w-1))
    if d=='R':
      r[L] = (min(max(0, r[L]-1), w-1))
    if d=='U':
      r[D] = (min(max(0, r[D]+1), h-1))
    if d=='D':
      r[U] = (min(max(0, r[U]-1), h-1))
      
  def check():
    if r[L] > r[R] or r[L] < 0 or r[R] > w-1:
      return True
    if r[U] > r[D] or r[U] < 0 or r[D] > h-1:
      return True
    return False
  
  for s, t in zip(ls, lt):
    g(t)
    f(s)
    if check():
      print('NO')
      return
  if r[L] <= sc <= r[R] and r[U] <= sr <= r[D]:
    print('YES')
  else:
    print('NO')
    
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    # right
    t = sc
    for i in range(N):
        if S[i] == 'R':
            t += 1
        if t > W:
            return True
        if T[i] == 'L' and t > 1:
            t -= 1
    # left
    t = sc
    for i in range(N):
        if S[i] == 'L':
            t -= 1
        if t < 1:
            return True
        if T[i] == 'R' and t < W:
            t += 1
    # up
    t = sr
    for i in range(N):
        if S[i] == 'U':
            t -= 1
        if t < 1:
            return True
        if T[i] == 'D' and t < H:
            t += 1
    # down
    t = sr
    for i in range(N):
        if S[i] == 'D':
            t += 1
        if t > H:
            return True
        if T[i] == 'U' and t > 1:
            t -= 1
    return False
if main():
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
def main():
    H, W, N = list(map(int, input().split()))
    sr, sc = list(map(int, input().split()))
    sr, sc = sr - 1, sc - 1
    S = input()
    T = input()
    K = [sc, W - sc - 1, sr, H - sr - 1]
    M = [W - 1, W - 1, H - 1, H - 1]
    Is = {'L': 0, 'R': 1, 'U': 2, 'D': 3}
    It = {'L': 1, 'R': 0, 'U': 3, 'D': 2}
    for s, t in zip(S, T):
        si, ti = Is[s], It[t]
        sti = It[s]
        K[si] -= 1
        if K[si] < 0:
            return False
        if K[sti] == M[sti]:
            K[sti] -= 1
            if K[sti] < 0:
                return False
        K[ti] += 1
        if K[ti] > M[ti]:
            K[ti] = M[ti]
    return True

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

Submission status: Accepted

Solution code:

import numpy as np
from functools import *

import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def acinput():
    return list(map(int, input().split(" ")))


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, NN = acinput()

initp = np.array(acinput())

S = list(input())[:-1]
T = list(input())[:-1]
#S = list(input().split(""))
#T = list(input().split(""))


def position_relative(direction, direction_op, cs, flg, seq1, seq2, N):
    global H, W
    # N=len(seq1)
    for i in range(len(seq1)):
        s1 = seq1[i]
        s2 = seq2[i]

        #print(cs,flg)
        if s1 == direction:
            cs += flg

        if flg > 0:
            if cs > N:
                return False

        else:
            if cs <= 0:
                return False
            # elif cs>N:
            #    cs=N
        #print(cs)
        if s2 == direction_op:
            #print("op",N)
            cs -= flg
    
    
        if cs > N:
           cs = N
        
        if cs <= 0:
            cs = 1

    return cs


sr = position_relative("R", "L", initp[1], 1, S, T, W)
sl = position_relative("L", "R", initp[1], -1, S, T, W)
su = position_relative("U", "D", initp[0], -1, S, T, H)
sd = position_relative("D", "U", initp[0], 1, S, T, H)

#print(sr, sl, su, sd)

# su=0
# if sr or sl or su or sd:
if sr*sl*su*sd == 0:
    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()
r=sr-1
for i in range(len(s)):
    if s[i] =="U":
        r-=1
    if r<0:
        print("NO")
        exit()
    if t[i] == "D" and r < h-1:
        r+=1
r=sr-1
for i in range(len(s)):
    if s[i] =="D":
        r+=1
    if h<=r:
        print("NO")
        exit()
    if t[i] == "U" and 0<r:
        r-=1
c=sc-1
for i in range(len(s)):
    if s[i] =="L":
        c-=1
    if c<0:
        print("NO")
        exit()
    if t[i] == "R" and c<w-1:
        c+=1
c=sc-1
for i in range(len(s)):
    if s[i] =="R":
        c+=1
    if c>=w:
        print("NO")
        exit()
    if t[i] == "L" and 0 < c:
        c-=1
print("YES")