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())
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 = ['U','D','L','R']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

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 = ['U','R','D','L']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

Submission status: Accepted

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

left, right = 0, W+1
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for i in reversed(range(N-1)):
    si, ti = S[i], T[i]
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(0, left)
    right = min(W+1, right)

    if si == "R":
        right -= 1
    elif si == "L":
        left += 1

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

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


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

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

    if si == "D":
        right -= 1
    elif si == "U":
        left += 1

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

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

print("YES")

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 = ['U','D','R','L']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

Submission status: Accepted

Solution code:
H, W, N = [int(_) for _ in input().split()]
R, C = [int(_) for _ in input().split()]
S = input()
T = input()

imin = 1
imax = H
jmin = 1
jmax = W

for n in range(N - 1, -1, -1):
    s = S[n]
    t = T[n]
    if t == 'U':
        imax = min(imax + 1, H)
    elif t == 'D':
        imin = max(imin - 1, 1)
    elif t == 'L':
        jmax = min(jmax + 1, W)
    elif t == 'R':
        jmin = max(jmin - 1, 1)
    if s == 'U':
        imin += 1
    elif s == 'D':
        imax -= 1
    elif s == 'L':
        jmin += 1
    elif s == 'R':
        jmax -= 1
    if imax < imin or jmax < jmin:
        print('NO')
        exit()
if imin <= R <= imax and jmin <= C <= jmax:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
from itertools import accumulate
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**10
from functools import reduce

h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
l = 1
r = w
u = 1
d = h
for s,t in zip(s[::-1],t[::-1]):
    if t == "R":
        l = max(l-1,1)
    elif t == "L":
        r = min(r+1,w)
    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 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())
sr -=1
sc -=1
s = input()
t = input()
p1,p2 = sr,sr
p3,p4 = sc,sc
for i in range(n):
    if s[i] == "U":
        p1 -=1
        if p1 < 0:
            print("NO")
            exit()
    if t[i] == "D" and p1 < h-1:
        p1 += 1
for i in range(n):
    if s[i] == "D":
        p2 +=1
        if p2 >= h:
            print("NO")
            exit()
    if t[i] == "U" and p2 >0:
        p2 -= 1
for i in range(n):
    if s[i] == "R":
        p3 +=1
        if p3 >= w:
            print("NO")
            exit()
    if t[i] == "L" and p3 >0:
        p3 -= 1
for i in range(n):
    if s[i] == "L":
        p4 -=1
        if p4 < 0:
            print("NO")
            exit()
    if t[i] == "R" and p4 < h-1:
        p4 += 1
print("YES")

Submission status: Accepted

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

flag = True
hmax = h
wmax = w
hmin = 1
wmin = 1

for i in range(n, 0, -1):
    if t[i - 1] == "U":
        hmax = min(hmax + 1, h)
    elif t[i - 1] == "D":
        hmin = max(hmin - 1, 1)
    elif t[i - 1] == "L":
        wmax = min(wmax + 1, w)
    elif t[i - 1] == "R":
        wmin = max(wmin - 1, 1)

    if s[i - 1] == "U":
        hmin += 1
    elif s[i - 1] == "D":
        hmax -= 1
    elif s[i - 1] == "L":
        wmin += 1
    elif s[i - 1] == "R":
        wmax -= 1

    if hmax < hmin or wmax < wmin:
        flag = False
        break

if not hmin <= sr <= hmax or not wmin <= sc <= wmax:
    flag = False

print("YES" if flag else "NO")

Submission status: Accepted

Solution code:
H,W,N = map(int,input().split())
sr,sc = [int(e)-1 for e in input().split()]
S = input()
T = input()

r = 0
l = 0
u = 0
d = 0
ans = "YES"

for j in range(N):
    for i in range(2):

        if i == 0:
            mark = S[j]
            if mark == "R":
                r += 1
            elif mark == "L":
                l += 1
            elif mark == "U":
                u += 1
            elif mark == "D":
                d += 1
            
        if i == 1:
            mark = T[j]
            if mark == "R":
                l = max(l-1,-(W-1-sc))
            elif mark == "L":
                r = max(r-1,-sc)
            elif mark == "U":
                d = max(d-1,-sr)
            elif mark == "D":
                u = max(u-1,-(H-1-sr))
    
        if r > W-1-sc or l > sc or u > sr or d > H-1-sr:
            ans = "NO"

print(ans)

Submission status: Accepted

Solution code:
def RL(a):
    if a=='R':
        return 1
    elif a=='L':
        return -1
    else:
        return 0
    
def UD(a):
    if a=='D':
        return 1
    elif a=='U':
        return -1
    else:
        return 0

def printLim(minus_lim, plus_lim, MAX):
    if minus_lim <0:
        print('X|', end='')
    else:
        print('|'+'X'*(minus_lim+1), end='')
    
    print('O'*(plus_lim - minus_lim -1), end='')
    if plus_lim ==MAX:
        print('|X')
    else:
        print('X'*(MAX-plus_lim) + '|')

def isFall(s_data,t_data,MAX,target):
    a = -1
    b = MAX
    for i in range(N-1, -1,-1):
        save = t_data[i]
        if save == 1:
            a-=1
        elif save == -1:
            b+=1
            
        # limit check
        if a < -1:
            a=-1
        if b>MAX:
            b=MAX
        if a==b:
            break
#         printLim(a,b,MAX)

        kill = s_data[i]
#         print(kill, save)
        # A lim
        if kill == -1:
            a +=1
        # B lim
        elif kill == 1:
            b -=1

        # limit check
        if a < -1:
            a=-1
        if b>MAX:
            b=MAX
        if (b-a) ==1:
            break


#         printLim(a,b,MAX)
#     print(a,b)
    
    if a < target < b:
        return False
    else:
        return True




################
H, W, N = map(int,input().split())
orig_y,orig_x = map(int,input().split())
orig_y -=1
orig_x -=1
s = input()
t = input()


flag = isFall([RL(it) for it in s],[RL(it) for it in t],W,orig_x)
flag |= isFall([UD(it) for it in s],[UD(it) for it in t],H,orig_y)
if flag:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
H, W, N = map(int, input().split())
sy, sx = map(int, input().split())
S = input()
T = input()
S = S[::-1]
T = T[::-1]

safe_y = [1, H]
safe_x = [1, W]
flg = 1
for i in range(N):
    if T[i] == 'L':
        if safe_x[1] != W:
            safe_x[1] += 1
    elif T[i] == 'R':
        if safe_x[0] != 1:
            safe_x[0] -= 1
    elif T[i] == 'U':
        if safe_y[1] != H:
            safe_y[1] += 1
    elif T[i] == 'D':
        if safe_y[0] != 1:
            safe_y[0] -= 1
    if S[i] == 'L':
        safe_x[0] += 1
    elif S[i] == 'R':
        safe_x[1] -= 1
    elif S[i] == 'U':
        safe_y[0] += 1
    elif S[i] == 'D':
        safe_y[1] -= 1
    if safe_y[1] - safe_y[0] < 0 or safe_x[1] - safe_x[0] < 0:
        flg = 0
        break

if flg:
    if safe_y[0] <= sy <= safe_y[1] and safe_x[0] <= sx <= safe_x[1]:
        pass
    else:
        flg = 0

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

Submission status: Accepted

Solution code:
def solve():
    lim_u = 0
    lim_b = h
    lim_l = 0
    lim_r = w
    for s, t in zip(ss[::-1], ts[::-1]):
        if t == 'U':
            if lim_b < h:
                lim_b += 1
        elif t == 'D':
            if lim_u > 0:
                lim_u -= 1
        elif t == 'L':
            if lim_r < w:
                lim_r += 1
        else:
            if lim_l > 0:
                lim_l -= 1
        if s == 'U':
            lim_u += 1
        elif s == 'D':
            lim_b -= 1
        elif s == 'L':
            lim_l += 1
        else:
            lim_r -= 1

        if lim_u >= lim_b or lim_l >= lim_r:
            return False

    return lim_u < sr <= lim_b and lim_l < sc <= lim_r


h, w, n = list(map(int, input().split()))
sr, sc = map(int, input().split())
ss = input()
ts = input()
print('YES' if solve() else 'NO')

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
win = 0

SR = sr
SC = sc
sr = SR
sc = SC
for i in range(n):
    if S[i] == "L":
        sc -= 1
    if sc <= 0:
        break
    if T[i] == "R" and sc < w:
        sc += 1
if sc <= 0:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "R":
        sc += 1
    if sc > w:
        break
    if T[i] == "L" and sc > 1:
        sc -= 1
if sc > w:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "D":
        sr += 1
    if sr > h:
        break
    if T[i] == "U" and sr > 1:
        sr -= 1
if sr > h:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        break
    if T[i] == "D" and sr < h:
        sr += 1
if sr <= 0:
    win = 1

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

Submission status: Accepted

Solution code:
import numpy as np

H, W, N = map(int, input().split())
si, sj = map(int, input().split())
si, sj = si - 1, sj - 1

S = input()
T = input()

piece_remaining = True

low_min = 0
high_max = H - 1

for t in range(N)[::-1]:
	if T[t] == 'U':
		high_max = min(high_max + 1, H - 1)
	elif T[t] == 'D':
		low_min = max(0, low_min - 1)
	
	if S[t] == 'U':
		low_min += 1
	elif S[t] == 'D':
		high_max -= 1
	
	if high_max < low_min:
		piece_remaining = False
		break

if not low_min <= si <= high_max:
	piece_remaining = False


low_min = 0
high_max = W - 1

for t in range(N)[::-1]:
	if T[t] == 'L':
		high_max = min(high_max + 1, W - 1)
	elif T[t] == 'R':
		low_min = max(0, low_min - 1)
	
	if S[t] == 'L':
		low_min += 1
	elif S[t] == 'R':
		high_max -= 1
	
	if high_max < low_min:
		piece_remaining = False
		break

if not low_min <= sj <= high_max:
	piece_remaining = False

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

Submission status: Accepted

Solution code:
h,w,n=map(int,input().split())
r,c=map(int,input().split())
s=input()
t=input()
xr=xl=xd=xu=yr=yl=yd=yu=0
for i in range(n):
    xr+=s[i]=='R'
    xl+=s[i]=='L'
    xd+=s[i]=='D'
    xu+=s[i]=='U'
    if w-c-(xr-yl)<0 or c-1-(xl-yr)<0 or h-r-(xd-yu)<0 or r-1-(xu-yd)<0:
        print('NO')
        break
    yr+=t[i]=='R'
    yr-=w-c-(yr-xl)<0
    yl+=t[i]=='L'
    yl-=c-1-(yl-xr)<0
    yd+=t[i]=='D'
    yd-=h-r-(yd-xu)<0
    yu+=t[i]=='U'
    yu-=r-1-(yu-xd)<0
else:
    print('YES')

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


dpx = [None for _ in range(n*2+1)]
dpy = [None for _ in range(n*2+1)]

dpx[0] = (1, w+1)
dpy[0] = (1, h+1)

# search safety region from tail
for i in range(1,n*2+1):
    is_t = i % 2 == 1
    strategy = t[-1-(i-1)//2] if is_t else s[-1-(i-1)//2]

    # T's turn: want to expand region
    if is_t:
        if strategy == 'L':
            dpx[i] = (dpx[i-1][0], min(dpx[i-1][1] + 1, w+1))
            dpy[i] = dpy[i-1]
        if strategy == 'R':
            dpx[i] = (max(1, dpx[i-1][0] - 1), dpx[i-1][1])
            dpy[i] = dpy[i-1]
        if strategy == 'U':
            dpy[i] = (dpy[i-1][0], min(dpy[i-1][1] + 1, h+1))
            dpx[i] = dpx[i-1]
        if strategy == 'D':
            dpy[i] = (max(1, dpy[i-1][0] - 1), dpy[i-1][1])
            dpx[i] = dpx[i-1]
    # S's turn: want to contract region
    else:
        if strategy == 'L':
            dpx[i] = (dpx[i-1][0] + 1, dpx[i-1][1])
            dpy[i] = dpy[i-1]
        if strategy == 'R':
            dpx[i] = (dpx[i-1][0], dpx[i-1][1] - 1)
            dpy[i] = dpy[i-1]
        if strategy == 'U':
            dpy[i] = (dpy[i-1][0] + 1, dpy[i-1][1])
            dpx[i] = dpx[i-1]
        if strategy == 'D':
            dpy[i] = (dpy[i-1][0], dpy[i-1][1] - 1)
            dpx[i] = dpx[i-1]
    # safety region has been cotrancted to 0, then break
    if len(range(*dpx[i])) == 0 or len(range(*dpy[i])) == 0:
        break

print('NO' if dpx[-1] is None else 'YES' if sr in range(*dpy[-1]) and sc in range(*dpx[-1]) else 'NO')

Submission status: Accepted

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

border_U = 0
border_D = H + 1
border_L = 0
border_R = W + 1

res = "YES"

for i in reversed(range(1, N)):

    # Takahashi's turn
    if S[i] == "U":
        border_U += 1
    elif S[i] == "D":
        border_D -= 1
    elif S[i] == "L":
        border_L += 1
    elif S[i] == "R":
        border_R -= 1

    # mate in the middle
    if border_U >= border_D - 1 or border_L >= border_R - 1:
        res = "NO"

    # Aoki's turn
    if T[i - 1] == "U":
        border_D += 1
    elif T[i - 1] == "D":
        border_U -= 1
    elif T[i - 1] == "L":
        border_R += 1
    elif T[i - 1] == "R":
        border_L -= 1

    # sanitize
    border_U = max(border_U, 0)
    border_D = min(border_D, H + 1)
    border_L = max(border_L, 0)
    border_R = min(border_R, W + 1)

# Takahashi's final turn
if S[0] == "U":
    border_U += 1
elif S[0] == "D":
    border_D -= 1
elif S[0] == "L":
    border_L += 1
elif S[0] == "R":
    border_R -= 1

if sr <= border_U:
    res = "NO"
elif sr >= border_D:
    res = "NO"
elif sc <= border_L:
    res = "NO"
elif sc >= border_R:
    res = "NO"

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(H, W, N, s, S, T):
    s_ = s
    lx = 0
    rx = W-1
    ly = 0
    ry = H-1
    for s, t in reversed(list(zip(S, T))):
        if t == 'R':
            lx = max(0, lx-1)
        elif t == 'L':
            rx = min(W-1, rx+1)
        elif t == 'D':
            ly = max(0, ly-1)
        elif t == 'U':
            ry = min(H-1, ry+1)

        if s == 'L':
            lx += 1
        elif s == 'R':
            rx -= 1
        elif s == 'U':
            ly += 1
        elif s == 'D':
            ry -= 1

        if lx > rx:
            return 'NO'
        elif ly > ry:
            return 'NO'

    s = s_
    # print(s, lx, rx, ly, ry)
    if not (lx <= s[1]-1 <= rx):
        return 'NO'
    elif not (ly <= s[0]-1 <= ry):
        return 'NO'
    return 'YES'


def main():
    H, W, N = read_int_n()
    s = read_int_n()
    S = read_str()
    T = read_str()
    print(slv(H, W, N, s, S, T))


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()
is_drop = False

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n)[::-1]:
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)
    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1
    if R < L: is_drop = True

if not L <= sc <= R: is_drop = True

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)
    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1

    if R < L: is_drop = True

if not L <= sr <= R: is_drop = True

print("NO" if is_drop else "YES")

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()
sr -= 1; sc -= 1
 
L=0; R=w-1; U=0; D=h-1;
is_drop = False
for i in range(n)[::-1]:
    if t[i] == 'U': D = min(D+1, h-1)
    if t[i] == 'D': U = max(U-1, 0)
    if t[i] == 'L': R = min(R+1, w-1)
    if t[i] == 'R': L = max(L-1, 0)
    if s[i] == 'U': U += 1
    if s[i] == 'D': D -= 1
    if s[i] == 'L': L += 1
    if s[i] == 'R': R -= 1
    if R < L or D < U: is_drop = True
if (not L <= sc <= R) or (not U <= sr <= D):  is_drop = True

print( "NO" if is_drop else "YES" )

Submission status: Accepted

Solution code:
#!/usr/bin/env python
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
p = [sc, sc, sr, sr]

def takahashi(p, M):
    if M == 'L':
        p[0] -= 1
    if M == 'R':
        p[1] += 1
    if M == 'U':
        p[2] -= 1
    if M == 'D':
        p[3] += 1
    return p

def aoki(p, M):
    if M == 'L':
        p[1] = max(1, p[1]-1)
    if M == 'R':
        p[0] = min(W, p[0]+1)
    if M == 'U':
        p[3] = max(1, p[3]-1)
    if M == 'D':
        p[2] = min(H, p[2]+1)
    return p

for n in range(N):
    p = takahashi(p, S[n])
    if p[0] == 0 or p[1] == W+1 or p[2] == 0 or p[3] == H+1:
        print('NO')
        exit()
    p = aoki(p, T[n])
print('YES')

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    ini_r, ini_c = map(int, input().split())
    S = str(input().strip())[::-1]
    T = str(input().strip())[::-1]

    dp = [0] * (N+1)
    dp[0] = (1, H, 1, W)
    for i in range(N):
        s, t, = S[i], T[i]
        d, u, l, r = dp[i]

        if t == 'L':
            r = min(W, r+1)
        elif t == 'R':
            l = max(1, l-1)
        elif t == 'U':
            u = min(H, u+1)
        else:
            d = max(1, d-1)

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

        if u < d or r < l:
            return 'NO'

        dp[i+1] = (d, u, l, r)

    d, u, l, r = dp[N]
    if not(d <= ini_r <= u and l <= ini_c <= r):
        return 'NO'
    else:
        return 'YES'


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l, r = 0, W
u, d = 0, H
for i in range(N)[::-1]:
    if T[i] == 'L': r = min(W, r + 1)
    if T[i] == 'R': l = max(0, l - 1)
    if T[i] == 'U': d = min(H, d + 1)
    if T[i] == 'D': u = max(0, u - 1)
    if S[i] == 'L': l += 1
    if S[i] == 'R': r -= 1
    if S[i] == 'U': u += 1
    if S[i] == 'D': d -= 1
    if l == r or u == d: 
        print("NO")
        exit()

print(("NO", "YES")[l <= sc < r and u <= sr < d])

Submission status: Accepted

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
taka_array = list(
    map(int,
        input().replace("L", "0").replace("R", "1").replace("U", "2").replace(
            "D", "3")))
aoki_array = list(
    map(int,
        input().replace("L", "0").replace("R", "1").replace("U", "2").replace(
            "D", "3")))
# print(list(taka_array))

limit_array = [0, 0, 0, 0]
size_array = [W, H]

for i, (taka, aoki) in enumerate(zip(taka_array[::-1], aoki_array[::-1])):
    if i != 0 and limit_array[(aoki // 2 * 4 + 1 - aoki)] != 0:
        limit_array[(aoki // 2 * 4 + 1 - aoki)] -= 1  # 0,1,2,3 -> 1,0,3,2

    limit_array[taka] += 1
    if limit_array[taka] == size_array[taka // 2]:
        print("NO")
        exit()
    # print(limit_array)
if 1 + limit_array[0] <= sc and W - limit_array[1] >= sc and 1 + limit_array[2] <= sr and H - limit_array[3] >= sr:
    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()

BB = [1, 1, 1, 1]
DD = [-1, 1, -1, 1]
B = [sc, sc, sr, sr]
UB = [W, W, H, H]
C = "LRUD"
ok = 0

def check(l, r, sz, ps):
    lb = 0; rb = sz+1
    for i in range(N-1, -1, -1):
        s = S[i]; t = T[i]
        if t == l:
            rb = min(rb+1, sz+1)
        elif t == r:
            lb = max(lb-1, 0)
        if s == l:
            lb += 1
        elif s == r:
            rb -= 1
        if rb <= lb+1:
            return 0
    if ps <= lb or rb <= ps:
        return 0
    return 1

if not check("L", "R", W, sc) or not check("U", "D", H, sr):
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
import sys
H, W, N = map(int, input().split())
Sr, Sc = map(int, input().split())
S = input()
T = input()

True_arr = [0, 0, 0, 0]

LRUD_S = {"L":0, "R":1, "U":2, "D":3}
LRUD_T = {"R":0, "L":1, "D":2, "U":3}

for i in range(N):
    if True_arr[LRUD_T[T[-i-1]]] > 0:
        True_arr[LRUD_T[T[-i-1]]] -= 1
    
    True_arr[LRUD_S[S[-i-1]]] += 1
    if True_arr[0] == W or True_arr[1] == W or True_arr[2] == H or True_arr[3] == H:
        break

if Sr > True_arr[2] and Sr <= H - True_arr[3] and Sc > True_arr[0] and Sc <= W - True_arr[1]:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
h,w,n=map(int,input().split())
sx,sy=map(int,input().split())
S=input(); T=input()
u,d=1,h; l,r=1,w
ans='YES'
for i in reversed(range(n)):
    if T[i]=='U':
        d=min(h,d+1)
    if T[i]=='D':
        u=max(1,u-1)
    if T[i]=='L':
        r=min(w,r+1)
    if T[i]=='R':
        l=max(1,l-1)
    if S[i]=='U':
        u+=1
        if u==h+1:
            print('NO')
            break
    if S[i]=='D':
        d-=1
        if d==0:
            print('NO')
            break
    if S[i]=='L':
        l+=1
        if l==w+1:
            print('NO')
            break
    if S[i]=='R':
        r-=1
        if r==0:
            print('NO')
            break
    if r<l or d<u:
        print('NO')
        break
else:
    if sx<u or sx>d or sy<l or sy>r:
        print('NO')
    else:
        print('YES')

Submission status: Accepted

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

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

x = sc
# Check Right
for i in range(n):
    if s[i] == "R":
        x += 1
    if x > w-1:
        print("NO")
        exit()
    if t[i] == "L" and x > 0:
        x -= 1

y = sr
# Check Down
for i in range(n):
    if s[i] == "D":
        y += 1
    if y > h-1:
        print("NO")
        exit()
    if t[i] == "U" and y > 0:
        y -= 1

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

print("YES")

Submission status: Accepted

Solution code:
 
def solve():
    H,W,N= [int(x) for x in input().split()]
     
    Sr,Sc = [int(x) for x in input().split()]
     
    S = input()
    T = input()
     
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if S[i] == 'D':
            maxd += 1
            if maxd == H+1:
                return "NO"
        if S[i] == 'R':
            maxr += 1
            if maxr == W+1:
                return "NO"
        if S[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"
 
        if T[i] == 'D' and minu < H:
            minu += 1
        if T[i] == 'U' and maxd > 1:
            maxd -= 1
        if T[i] == 'L' and maxr > 1:
            maxr -= 1
        if T[i] == 'R' and minl < W:
            minl += 1
 
    return "YES"
 
print(solve())

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
h,w,n=map(int,input().split())
sx,sy=map(int,input().split())
S=input(); T=input()
u,d=sx,sx; l,r=sy,sy
for i in range(n):
    if S[i]=='U':
        u-=1
    if S[i]=='D':
        d+=1
    if S[i]=='L':
        l-=1    
    if S[i]=='R':
        r+=1
    if u<1 or d>h or l<1 or r>w:
        print('NO')
        break
    if T[i]=='U':
        d=max(1,d-1)
    if T[i]=='D':
        u=min(h,u+1)
    if T[i]=='L':
        r=max(1,r-1)
    if T[i]=='R':
        l=min(w,l+1)
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()
wr = sc
wl = sc
hu = sr
hd = sr
def taka(k,wr,wl,hu,hd):
    if k=='U':
        hu -= 1
    if k=='D':
        hd += 1
    if k=='R':
        wr += 1
    if k=='L':
        wl -= 1
    return wr,wl,hu,hd
def ao(k,wr,wl,hu,hd):
    if k=='U':
        hd = max(1,hd-1)
    if k=='D':
        hu = min(h,hu+1)
    if k=='R':
        wl = min(w,wl+1)
    if k=='L':
        wr = max(1,wr-1)
    return wr,wl,hu,hd
for i in range(n):
    wr,wl,hu,hd = taka(s[i],wr,wl,hu,hd)
    if wl==0 or hu==0 or hd==h+1 or wr==w+1:
        print('NO')
        exit()
    wr,wl,hu,hd = ao(t[i],wr,wl,hu,hd)
print('YES')