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:
# 変更後2 後ろから数える　デバッグ用

H,W,N = map(int,input().split())
init_r,init_c = map(int,input().split())
S = input()
T = input()

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

ans = "YES"

for i in range(N):
    t = N-1-i
    #savor's turn
    direction = T[t]
    if direction == "R" and l != 0:
        l -= 1
    elif direction == "L" and r != W+1:
        r += 1
    elif direction == "U" and d != H+1:
        d += 1
    elif direction == "D" and u != 0:
        u -= 1
        
#     killer's turn
    t = N-1-i
    direction = S[t]
    if direction == "R":
        r -=1
        if r == 1:
            ans = "NO"
            break
    elif direction == "L":
        l +=1
        if l == W:
            ans = "NO"
            break
    elif direction == "U":
        u +=1
        if u == H:
            ans = "NO"
            break
    elif direction == "D":
        d -=1
        if d == 1:
            ans = "NO"
            break
    if r <= l:
        ans = "NO"
        break    
    if d <= u:
        ans = "NO"
        break

if init_c <= l or init_c >= r:
    ans = "NO"
if init_r <= u or init_r >= d:
    ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
H, W, N = inpl()
Sr, Sc = inpl()
S = input()
T = input()

Hmin = 0
Hmax = H-1
Wmin = 0
Wmax = W-1
for i in range(N-1):
    s = S[-1-i]
    if s == 'U':
        Hmin += 1
    elif s == 'D':
        Hmax -= 1
    elif s == 'L':
        Wmin += 1
    else: # s == 'R'
        Wmax -= 1

    t = T[-2-i]
    if t == 'D' and Hmin > 0 and Hmin<=Hmax:
        Hmin -= 1
    elif t == 'U' and Hmax < H-1 and Hmin<=Hmax:
        Hmax += 1
    elif t == 'R' and Wmin > 0 and Wmin<=Wmax:
        Wmin -= 1
    elif t == 'L' and Wmax < W-1 and Wmin<=Wmax:
        Wmax += 1

s = S[-N]
if s == 'U':
    Hmin += 1
elif s == 'D':
    Hmax -= 1
elif s == 'L':
    Wmin += 1
else: # s == 'R'
    Wmax -= 1

if Hmin <= Sr-1 <= Hmax and Wmin <= Sc-1 <= Wmax:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
# 変更後2 後ろから数える　デバッグ用

H,W,N = map(int,input().split())
init_r,init_c = map(int,input().split())
S = input()
T = input()

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

# print(r,l,u,d)
ans = "YES"

for i in range(N):
    t = N-1-i
#     print("index:",t)
    #savor's turn
    direction = T[t]
#     print(direction)
    if direction == "R" and l != 0:
        l -= 1
    elif direction == "L" and r != W+1:
        r += 1
    elif direction == "U" and d != H+1:
        d += 1
    elif direction == "D" and u != 0:
        u -= 1
#     print(r,l,u,d)
    
#     killer's turn
    t = N-1-i
    direction = S[t]
#     print(direction)
    if direction == "R":
        r -=1
        if r == 1:
            ans = "NO"
            break
    elif direction == "L":
        l +=1
        if l == W:
            ans = "NO"
            break
    elif direction == "U":
        u +=1
        if u == H:
            ans = "NO"
            break
    elif direction == "D":
        d -=1
        if d == 1:
            ans = "NO"
            break
    if r <= l:
        ans = "NO"
        break    
    if d <= u:
        ans = "NO"
        break
        
#     3 >= 2
# 2 <= 3
#     print(r,l,u,d)
#     print(r,l)
#     print(r,l)

if init_c <= l or init_c >= r:
    ans = "NO"

        
if init_r <= u or init_r >= d:
    ans = "NO"




    
print(ans)

Submission status: Accepted

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

x1 = x

is_no = False
for s, t in zip(S, T):
    if s == 'R':
        x1 += 1
    if x1 == W + 1:
        is_no = True
        break
    if t == 'L':
        if x1 != 1:
            x1 -= 1

x1 = x
for s, t in zip(S, T):
    if s == 'L':
        x1 -= 1
    if x1 == 0:
        is_no = True
        break
    if t == 'R':
        if x1 != W:
            x1 += 1

y1 = y
for s, t in zip(S, T):
    if s == 'U':
        y1 -= 1
    if y1 == 0:
        is_no = True
        break
    if t == 'D':
        if y1 != H:
            y1 += 1

y1 = y
for s, t in zip(S, T):
    if s == 'D':
        y1 += 1
    if y1 == H + 1:
        is_no = True
        break
    if t == 'U':
        if y1 != 1:
            y1 -= 1

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

wr=Sc
wl=Sc
hu=Sr
hd=Sr


ans='YES'
for i in range(N):
    if S[i]=='R':
        wr+=1
    elif S[i]=='L':
        wl-=1
    elif S[i]=='U':
        hu-=1
    elif S[i]=='D':
        hd+=1
    if wr>W or wl<1 or hu<1 or hd>H:
        ans='NO'
        break

    if T[i]=='R':
        wl = min(W,wl+1)
    elif T[i]=='L':
        wr = max(1,wr-1)
    elif T[i]=='U':
        hd= max(1,hd-1)
    elif T[i]=='D':
        hu=min(H,hu+1)

print(ans)

Submission status: Accepted

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

ssr -= 1
ssc -= 1

A = ["L", "R", "U", "D"]

A2 = {"L": -1, "R": 1, "U": -1, "D": 1}

D ={"L": "R", "R": "L", "U": "D", "D": "U"}

ans = "YES"

flg = 0

for a in A :
    
    sr = ssr
    sc = ssc
    
    if flg :
        break
        
    for i in range(N) :
        if (a in ["L", "R"]) and (a == S[i]) :
            sc += A2[a]
            
            if (sc < 0) or (sc >= W) :
                ans = "NO"
                flg = 1
                break

        elif (a in ["U", "D"]) and (a == S[i]) :
            sr += A2[a]
            
            if (sr < 0) or (sr >= H) :
                ans = "NO"
                flg = 1
                break
            
        if (a in ["L", "R"]) and (D[a] == T[i]) :
            if (sc == 0) and (D[a] == "L") :
                continue
            elif (sc == W-1) and (D[a] == "R") :
                continue
            sc += A2[D[a]]
            

            
        elif (a in ["U", "D"]) and (T[i] == D[a]) :
            if (sr == 0) and (D[a] == "U") :
                continue
            elif (sr == H-1) and (D[a] == "D") :
                continue
            sr += A2[D[a]]
        #print((sr,sc))
print(ans)

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
ar, ac = map(int, input().split())
s = input()
t = input()
r = ac
l = ac
u = ar
d = ar
ans = False

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

Submission status: Accepted

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
 
up = [0] * (N+1)
down = [0] * (N+1)
right = [0] * (N+1)
left = [0] * (N+1)
maxU, maxD, maxR, maxL = 0, 0, 0, 0
 
for i in range(N):
    if S[i] == "L":
        left[i+1] = left[i] + 1
        right[i+1] = right[i]
        up[i+1] = up[i]
        down[i+1] = down[i]
        maxL = max(maxL, left[i+1])
    elif S[i] == "R":
        right[i+1] = right[i] + 1
        up[i+1] = up[i]
        down[i+1] = down[i]
        left[i+1]  = left[i]
        maxR = max(maxR, right[i+1])
    elif S[i] == "U":
        up[i+1] = up[i] + 1
        right[i+1] = right[i]
        down[i+1] = down[i]
        left[i+1]  = left[i]
        maxU = max(maxU, up[i+1])
    else:
        down[i+1] = down[i] + 1
        right[i+1] = right[i]
        up[i+1] = up[i]
        left[i+1]  = left[i]
        maxD = max(maxD, down[i+1])
    if T[i] == "L":
        right[i+1] = max(right[i+1] - 1, -sc + 1)
    elif T[i] == "R":
        left[i+1] = max(left[i+1] - 1, sc - W)
    elif T[i] == "U":
        down[i+1] = max(down[i+1] - 1, -sr + 1)
    else:
        up[i+1] = max(up[i+1] - 1, sr - H)
 
if sr + maxD > H or sr - maxU <= 0 or sc + maxR > W or sc - maxL <= 0:
    print("NO")
else:  print("YES")

Submission status: Accepted

Solution code:
from collections import Counter
h, w, n = map(int,input().split())
x, y = map(int,input().split())
S = input()
T = input()
cs = Counter(S)
u = x
d = h-x+1
l = y
r = w-y+1
if not (u <= cs['U'] or d <= cs['D'] or l <= cs['L'] or r <= cs['R']):
    print('YES')
    exit()

a = x
if u <= cs['U']:
    for s, t in zip(S, T):
        if s == 'U':
            a -= 1
        if a < 1:
            print('NO')
            exit()
        if t == 'D' and a < h:
            a += 1
        if a < 1:
            print('NO')
            exit()
    
a = x   
if d <= cs['D']:
    for s, t in zip(S, T):
        if s == 'D':
            a += 1
        if a > h:
            print('NO')
            exit()
        if t == 'U' and a > 1:
            a -= 1
        if a > h:
            print('NO')
            exit()

a = y
if l <= cs['L']:
    for s, t in zip(S, T):
        if s == 'L':
            a -= 1
        if a < 1:
            print('NO')
            exit()
        if t == 'R' and a < w:
            a += 1
        if a < 1:
            print('NO')
            exit()
            
a = y
if r <= cs['R']:
    for s, t in zip(S, T):
        if s == 'R':
            a += 1
        if a > w:
            print('NO')
            exit()
        if t == 'L' and a > 1:
            a -= 1
        if a > w:
            print('NO')
            exit()

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

l = sc
r = sc
u = sr
d = sr

flag = True
for i in range(n):
    if s[i] == "R":
        r += 1
    if s[i] == "L":
        l -= 1
    if s[i] == "U":
        u -= 1
    if s[i] == "D":
        d += 1
    if l==0 or r==w+1 or u==0 or d==h+1:
        print("NO")
        flag = False
        break
    if t[i] == "R" and l != w:
        l += 1
    if t[i] == "L" and r != 1:
        r -= 1
    if t[i] == "U" and d != 1:
        d -= 1
    if t[i] == "D" and u != h:
        u += 1

if flag != False:
    print("YES")

Submission status: Accepted

Solution code:
H,W,N = map(int,input().split())
sr, sc = map(int,input().split())
S = input()
T = input()
L = sc-1
R = W-sc
U = sr-1
D = H-sr
for s, t in zip(S,T):
    if s == 'L':
        L -= 1
    elif s == 'R':
        R -= 1
    elif s == 'U':
        U -= 1
    elif s == 'D':
        D -= 1
    if min(L,R,U,D) < 0:
        print('NO')
        quit()
    if t == 'L' and R < W-1:
        R += 1
    elif t == 'R' and L < W-1:
        L += 1
    elif t == 'U' and D < H-1:
        D += 1
    elif t == 'D' and U < H-1:
        U += 1
print('YES')

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    r, c = map(int, input().split())
    S = str(input().strip())
    T = str(input().strip())

    R = c
    L = c
    U = r
    D = r

    i = 0
    while i < N:
        s, t = S[i], T[i]

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

        if R > W or L <= 0 or U <= 0 or D > H:
            return 'NO'

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

        i += 1

    return 'YES' 


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

Submission status: Accepted

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

S_d = {"L":0,"R":0,"U":0,"D":0}
M_d = {"L":0,"R":0,"U":0,"D":0}

reverse_d = {"L":"R","R":"L","U":"D","D":"U"}

S_d[S[0]] += 1
M_d[S[0]] += 1

if ((sr == 1 and S[0] == "U") or
        (sr == H and S[0] == "D") or
        (sc == 1 and S[0] == "L") or
        (sc == W and S[0] == "R")):
    print("NO")
else:
    for i in range(N-1):
        M_d[reverse_d[T[i]]] = max(0,M_d[reverse_d[T[i]]]-1)
        S_d[reverse_d[T[i]]] = S_d[reverse_d[T[i]]] - 1
        M_d[S[i+1]] += 1
        S_d[S[i+1]] += 1
        if (M_d["U"] >= H or M_d["D"] >= H or M_d["L"] >= W or M_d["R"] >= W):
            print("NO")
            break
        if (S_d["U"]>=sr or S_d["D"]>= H+1-sr or S_d["L"]>=sc or S_d["R"]>=W+1-sc):
            print("NO")
            break
    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()

ld=H-sr
lu=sr-1
lr=W-sc
ll=sc-1

for i in range(N):
  x=S[i]
  y=T[i]

  if x=='U':
    lu-=1
  elif x=='D':
    ld-=1
  elif x=='L':
    ll-=1
  elif x=='R':
    lr-=1

  # print(lu,ld,ll,lr)

  if lu<0 or ld<0 or ll<0 or lr<0:
    print('NO')
    exit()

  if y=='U' and ld<H-1:
    ld+=1
  elif y=='D' and lu<H-1:
    lu+=1
  elif y=='L' and lr<W-1:
    lr+=1
  elif y=='R' and ll<W-1:
    ll+=1

  # print(lu,ld,ll,lr)

print('YES')

Submission status: Accepted

Solution code:
import sys
h,w,n = map(int,input().split())
y,x = map(int,input().split())
s = list(input())
t = list(input())

up = h
down = 1 
right = w
left = 1
for i in range(n-1,-1,-1):
    if i != n-1:
        if t[i] == 'D':
            down = max(down-1,1)
        elif t[i] == 'U':
            up = min(up+1,h)
        elif t[i] == 'R':
            left = max(left-1,1)
        else:
            right = min(right+1,w)
    if s[i] == 'D':
        up -= 1
    elif s[i] == 'U':
        down += 1
    elif s[i] == 'R':
        right -= 1
    else:
        left += 1
    
    #print(left,right)
    #print(down,up)

    if right == 0 or left == w or up == 0 or down == h:
        print('NO')
        exit()
    if right<left or up<down:
        print('NO')
        exit()

if up>=y and down<=y and right >= x and left<=x:
    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()
R = [0] * N
if S[-1] == 'R':
    R[-1] = W - 1
else:
    R[-1] = W
for i in range(N - 2, -1, -1):
    if S[i] == 'R':
        if T[i] == 'L':
            R[i] = min(R[i + 1], W - 1)
        else:
            R[i] = max(0, R[i + 1] - 1)
    else:
        if T[i] == 'L':
            R[i] = min(W, R[i + 1] + 1)
        else:
            R[i] = R[i + 1]
L = [0] * N
if S[-1] == 'L':
    L[-1] = 2
else:
    L[-1] = 1
for i in range(N - 2, -1, -1):
    if S[i] == 'L':
        if T[i] == 'R':
            L[i] = max(L[i + 1], 2)
        else:
            L[i] = min(W + 1, L[i + 1] + 1)
    else:
        if T[i] == 'R':
            L[i] = max(1, L[i + 1] - 1)
        else:
            L[i] = L[i + 1]
D = [0] * N
if S[-1] == 'D':
    D[-1] = H - 1
else:
    D[-1] = H
for i in range(N - 2, -1, -1):
    if S[i] == 'D':
        if T[i] == 'U':
            D[i] = min(D[i + 1], H - 1)
        else:
            D[i] = max(0, D[i + 1] - 1)
    else:
        if T[i] == 'U':
            D[i] = min(H, D[i + 1] + 1)
        else:
            D[i] = D[i + 1]
U = [0] * N
if S[-1] == 'U':
    U[-1] = 2
else:
    U[-1] = 1
for i in range(N - 2, -1, -1):
    if S[i] == 'U':
        if T[i] == 'D':
            U[i] = max(U[i + 1], 2)
        else:
            U[i] = min(H + 1, U[i + 1] + 1)
    else:
        if T[i] == 'D':
            U[i] = max(1, U[i + 1] - 1)
        else:
            U[i] = U[i + 1]
ans = "YES"
if sc < L[0] or sc > R[0] or sr < U[0] or sr > D[0]:
    ans = "NO"
for i in range(N):
    if R[i] < L[i] or D[i] < U[i]:
        ans = "NO"
print(ans)

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


min_i = si
max_i = si
min_j = sj
max_j = sj

piece_remaining = True

for t in range(N):
	if S[t] == 'U':
		min_i -= 1
	elif S[t] == 'D':
		max_i += 1
	elif S[t] == 'L':
		min_j -= 1
	elif S[t] == 'R':
		max_j += 1

	if min_i < 0 or H <= max_i or min_j < 0 or W <= max_j:
		piece_remaining = False
		break

	if T[t] == 'U':
		max_i = max(0, max_i - 1)
	elif T[t] == 'D':
		min_i = min(H - 1, min_i + 1)
	elif T[t] == 'L':
		max_j = max(0, max_j - 1)
	elif T[t] == 'R':
		min_j = min(W - 1, min_j + 1)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

h=sr
for i in range(N):
    if S[i]=='U':
        h-=1
    if h==0:
        NO()
        exit()
    if T[i]=='D':
        if h<H:
            h+=1
h=sr
for i in range(N):
    if S[i]=='D':
        h+=1
    if h>H:
        NO()
        exit()
    if T[i]=='U':
        if h>1:
            h-=1
w=sc
for i in range(N):
    if S[i]=='L':
        w-=1
    if w==0:
        NO()
        exit()
    if T[i]=='R':
        if w<W:
            w+=1
w=sc
for i in range(N):
    if S[i]=='R':
        w+=1
    if w>W:
        NO()
        exit()
    if T[i]=='L':
        if w>1:
            w-=1
YES()

Submission status: Accepted

Solution code:
from sys import exit
from collections import defaultdict
H,W,N = [int(n) for n in input().split()]
Sr,Sc = [int(n) for n in input().split()]
#上に
Dout = H-Sr + 1
#下に
Uout = Sr
#右に落とすのに必要な歩数
Rout = W - Sc + 1
#左に
Lout = Sc
# print(Uout, Dout,Rout, Lout)
S = str(input())
T = str(input())
ptrC = defaultdict(int)
ptrA = defaultdict(int)
for s,t in zip(S,T):
    ptrC[s]+=1
    ptrA[t]+=1
# print(ptrC,ptrA)
# if ptrC["R"] - ptrA["L"] >= Rout or ptrC["L"] - ptrA["R"] >= Lout or ptrC["D"] - ptrA["U"] >= Dout or ptrC["U"] - ptrA["D"] >= Uout:
#     print("NO")
#     exit()
def check(outcnt, C, A, lim, B):
    backed = 0
    for s,t in zip(S,T):
        if s == B and outcnt == lim -1 and backed > 0:
            outcnt -= 1
            backed -= 1
        if( s == C ):
            outcnt -= 1
        if( outcnt == 0 ):
            print("NO")
            exit()
        if( t == A and outcnt < lim ):
            outcnt += 1
            backed += 1

check(Rout, "R", "L", W, "L")
check(Lout, "L", "R", W, "R")
check(Uout, "U", "D", H, "D")
check(Dout, "D", "U", H, "U")
print("YES")

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()) #コマを残したい

lst = [0,0,0,0] #L, R, U, D, 高橋くん(文字列S)くんが強制的に動かせる距離

#print ('sc+1', sc+1)
#print ('W-sc', W-sc)
#print ('sr+1', sr+1)
#print ('H-sr', H-sr)

for i in range(N):
    if S[i] == 'L':
        lst[0] += 1
    if S[i] == 'R':
        lst[1] += 1
    if S[i] == 'U':
        lst[2] += 1
    if S[i] == 'D':
        lst[3] += 1
    #print ('S', lst)
    if lst[0] >= sc+1 or lst[1] >= W-sc or lst[2] >= sr+1 or lst[3] >= H-sr:
        print ('NO')
        sys.exit()
    if T[i] == 'L':
        #lst[0] -= 1
        #if lst[0] < -(W-sc):
        #    lst[0] = -(W-sc)
        lst[1] -= 1
        if lst[1] <= -(sc+1)+1:
            lst[1] = -(sc+1)+1
    if T[i] == 'R':
        #lst[1] -= 1
        #if lst[1] < -sc:
        #    lst[1] = -sc
        lst[0] -= 1
        if lst[0] <= -(W-sc)+1:
            lst[0] = -(W-sc)+1
    if T[i] == 'U':
        #lst[2] -= 1
        #if lst[2] < -(H-sr):
        #    lst[2] = -(H-sr)
        lst[3] -= 1
        if lst[3] <= -(sr+1)+1:
            lst[3] = -(sr+1)+1
    if T[i] == 'D':
        #lst[3] -= 1
        #if lst[3] < -sr:
        #    lst[3] = -sr
        lst[2] -= 1
        if lst[2] <= -(H-sr)+1:
            lst[2] = -(H-sr)+1
    #print ('T', lst)

print ('YES')

Submission status: Accepted

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

atk_U = [0]*N
atk_D = [0]*N
atk_L = [0]*N
atk_R = [0]*N
u = 0
d = 0
l = 0
r = 0
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
  atk_U[i] = u
  atk_D[i] = d
  atk_L[i] = l
  atk_R[i] = r

def_U = [0]*N
def_D = [0]*N
def_L = [0]*N
def_R = [0]*N
u = 0
d = 0
l = 0
r = 0
for i in range(N-1):
  if T[i] == 'U':
    if u + 1 - atk_D[i] < Sr:
      u += 1
  if T[i] == 'D':
    if d + 1 - atk_U[i] < H - Sr + 1:
      d += 1
  if T[i] == 'L':
    if l + 1 - atk_R[i] < Sc:
      l += 1
  if T[i] == 'R':
    if r + 1 - atk_L[i] < W - Sc + 1:
      r += 1
  def_U[i+1] = d
  def_D[i+1] = u
  def_L[i+1] = r
  def_R[i+1] = l

dam_U = max([atk_U[i] - def_U[i] for i in range(N)])
dam_D = max([atk_D[i] - def_D[i] for i in range(N)])
dam_L = max([atk_L[i] - def_L[i] for i in range(N)])
dam_R = max([atk_R[i] - def_R[i] for i in range(N)])
if dam_U < Sr and dam_D < H - Sr + 1 and dam_L < Sc and dam_R < W - Sc + 1:
  print('YES')
else:
  print('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()

# 高橋はUR、青木はDL重視

su = sr
sd = sr
sle = sc
sri = sc 
flag = False
for ss, tt in zip(S, T):

    if ss == "U":
        su -= 1
    if ss == "D":
        sd += 1
    if ss == "R":
        sri += 1
    if ss == "L":
        sle -= 1

    if su == 0 or sd > H or sri > W or sle == 0:
        flag = True    

    if tt == "U" and sd > 1:
        sd -= 1
    if tt == "D" and su < H: 
        su += 1
    if tt == "R" and sle < W:
        sle += 1
    if tt == "L" and sri > 1:
        sri -= 1

if flag==True:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
"""
LRとUDは独立に考えて良い
"""

import unittest


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

    sr, sc = map(int, input().split())

    S = input()
    T = input()

    print(solve(H, W, N, sr, sc, S, T))


def pp(c,s):
    # print(c,''.join(['+' if c == 1 else '-' if c == -1 else ' ' for c in s]))
    return True


def solve(h, w, n, sr, sc, s, t):
    s_lr = []
    s_ud = []
    for c in s[::-1]:
        if c == 'L':
            s_lr.append(-1)
            s_ud.append(0)
        elif c == 'R':
            s_lr.append(+1)
            s_ud.append(0)
        elif c == 'U':
            s_lr.append(0)
            s_ud.append(-1)
        elif c == 'D':
            s_lr.append(0)
            s_ud.append(+1)

    t_lr = []
    t_ud = []
    for c in t[::-1]:
        if c == 'L':
            t_lr.append(-1)
            t_ud.append(0)
        elif c == 'R':
            t_lr.append(+1)
            t_ud.append(0)
        elif c == 'U':
            t_lr.append(0)
            t_ud.append(-1)
        elif c == 'D':
            t_lr.append(0)
            t_ud.append(+1)

    if aoki_win(n, s_ud, t_ud, h, sr) and aoki_win(n, s_lr, t_lr, w, sc):
        return 'YES'
    else:
        return 'NO'


def aoki_win(n, chokudai, aoki, l, sp):
    """
    最後から範囲を削っていく
    """
    # print(l, sp)
    pp('AO', aoki)
    pp('CH',chokudai)

    chokudai_minus = 0
    chokudai_plus = 0
    for c in chokudai:
        if c == 1:
            chokudai_plus += 1
        elif c == -1:
            chokudai_minus += 1
    if 1 <= sp-chokudai_minus and sp+chokudai_plus <= l:
        # print('#1')
        return True
    chokudai_right = l if chokudai[0] == 1 else l+1
    chokudai_left = 1 if chokudai[0] == -1 else 0
    # aoki_right = chokudai_right -1
    # aoki_left = chokudai_right + 1

    for i in range(1, n):
        # print(chokudai_left,']','[', chokudai_right)
        if aoki[i] == 1:
            # aoki_left = max(aoki_left-1, 1)
            chokudai_left = max(0, chokudai_left-1)
        elif aoki[i] == -1:
            # aoki_right = min(aoki_right+1, l)
            chokudai_right = min(l+1, chokudai_right+1)
        if chokudai_left+1 >= chokudai_right:
            # print('#2')
            # print(chokudai_left,']','[', chokudai_right)
            return False
        if chokudai[i] == 1:
            chokudai_right = max(chokudai_right-1, 1)
        elif chokudai[i] == -1:
            chokudai_left = min(chokudai_left+1, l)
        if chokudai_left+1 >= chokudai_right:
            # print('#3')
            # print(chokudai_left,']','[', chokudai_right)
            return False
    
    if sp<=chokudai_left or chokudai_right<=sp:
        # print('#4')
        return False
    
    # print('#5')

    return True

class TestSolve(unittest.TestCase):
    def test_solve_case_1(self):
        h = 2
        w = 3
        n = 3
        sr = 2
        sc = 2
        s = 'RRL'
        t = 'LUD'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'YES'
        self.assertEqual(expected, actual)

    def test_solve_case_2(self):
        h = 4
        w = 3
        n = 5
        sr = 2
        sc = 2
        s = 'UDRRR'
        t = 'LLDUD'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)

    def test_solve_case_3(self):
        h = 5
        w = 6
        n = 11
        sr = 2
        sc = 1
        s = 'RLDRRUDDLRL'
        t = 'URRDRLLDLRD'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)

    def test_solve_case_4(self):
        h = 1
        w = 3
        n = 6
        sr = 1
        sc = 2
        s = 'LRRRRR'
        t = 'LRLLRL'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)

    def test_solve_case_5(self):
        h = 1
        w = 3
        n = 4
        sr = 1
        sc = 2
        s = 'LLLL'
        t = 'RRRR'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'YES'
        self.assertEqual(expected, actual)

    def test_solve_case_6(self):
        h = 3
        w = 2
        n = 4
        sr = 2
        sc = 2
        s = 'UDLUU'
        t = 'DRRRR'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

H, W, N = [int(a) for a in readline().split()]

sr, sc = [int(a) for a in readline().split()]

S = readline().strip()
T = readline().strip()

S_max = {'L':0, 'R':0, 'U':0, 'D':0}
T_max = {'L':0, 'R':0, 'U':0, 'D':0}
maxes = {'L':0, 'R':0, 'U':0, 'D':0}
anti = {'L':'R', 'R':'L', 'U':'D', 'D':'U'}

def check():
    return (-maxes['L'] + sc < 1) or (maxes['R'] + sc > W) or \
    (-maxes['U'] + sr < 1) or (maxes['D'] + sr > H)

for i in range(N):
#    for dir in ['L', 'R', 'U', 'D']:
#        S_max[dir] = S.count(dir)
#        T_max[dir] = T.count(dir)
#        S_max[dir] += 1 if S[i] == 'dir' else 0
#        T_max[dir] += 1 if T[i] == 'dir' else 0        
    
    maxes[S[i]] += 1
    if check():
        print('NO')
        exit()
    maxes[anti[T[i]]] -= 1
    maxes['L'] = max(maxes['L'], sc - W)
    maxes['R'] = max(maxes['R'], -sc + 1)
    maxes['U'] = max(maxes['U'], sr - H)
    maxes['D'] = max(maxes['D'], -sr + 1)
#    if (S_max['R'] - T_max['L'] + sc > W) or \
#        (T_max['R'] - S_max['L'] + sc < 1) or \
#        (S_max['D'] - T_max['U'] + sr > H) or \
#        (T_max['D'] - S_max['U'] + sr < 1):
#        print('NO')
#        exit()

print('YES')

Submission status: Accepted

Solution code:
def li():
    return list(map(int, input().split()))


[H, W, N] = li()
s = li()
S = input()
T = input()

if __name__ == '__main__':
    # 左上戦略
    is_no = False
    takahashi = [s[0], s[1]]
    for index, (S_i, T_i) in enumerate(zip(S, T)):
        if S_i == 'L':
            takahashi[1] = takahashi[1] - 1
        if S_i == 'U':
            takahashi[0] = takahashi[0] - 1
        if min(takahashi) <= 0:
            is_no = True
            break
        if T_i == 'R':
            takahashi[1] = min(takahashi[1] + 1, W)
        if T_i == 'D':
            takahashi[0] = min(takahashi[0] + 1, H)

    if not is_no:
        # 右下戦略
        takahashi = [s[0], s[1]]
        for index, (S_i, T_i) in enumerate(zip(S, T)):
            if S_i == 'R':
                takahashi[1] = takahashi[1] + 1
            if S_i == 'D':
                takahashi[0] = takahashi[0] + 1
            if takahashi[0] > H or takahashi[1] > W:
                is_no = True
                break
            if T_i == 'L':
                takahashi[1] = max(takahashi[1] - 1, 1)
            if T_i == 'U':
                takahashi[0] = max(takahashi[0] - 1, 1)

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