Problem:


Score : 600 points

Problem Statement

We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
On this grid, there is a piece, which is initially placed at square (s_r,s_c).

Takahashi and Aoki will play a game, where each player has a string of length N.
Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: L, R, U and D.

The game consists of N steps. The i-th step proceeds as follows:

First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.

Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.

Here, to move the piece in the direction of L, R, U and D, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.

Takahashi wants to remove the piece from the grid in one of the N steps.
Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid.
Determine if the piece will remain on the grid at the end of the game when both players play optimally.

Constraints

2 \leq H,W \leq 2 \times 10^5

2 \leq N \leq 2 \times 10^5

1 \leq s_r \leq H

1 \leq s_c \leq W

|S|=|T|=N

S and T consists of the four kinds of letters L, R, U and D.

Input

Input is given from Standard Input in the following format:

H W N
s_r s_c
S
T

Output

If the piece will remain on the grid at the end of the game, print YES; otherwise, print NO.

Sample Input 1

2 3 3
2 2
RRL
LUD

Sample Output 1

YES

Here is one possible progress of the game:

Takahashi moves the piece right. The piece is now at (2,3).

Aoki moves the piece left. The piece is now at (2,2).

Takahashi does not move the piece. The piece remains at (2,2).

Aoki moves the piece up. The piece is now at (1,2).

Takahashi moves the piece left. The piece is now at (1,1).

Aoki does not move the piece. The piece remains at (1,1).

Sample Input 2

4 3 5
2 2
UDRRR
LLDUD

Sample Output 2

NO

Sample Input 3

5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD

Sample Output 3

NO



Sample input:
2 3 3
2 2
RRL
LUD


Sample output:
YES

Submission status: Accepted

Solution code:
import sys
import decimal

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

H, W, N = rl()
start = rl()
S = list(rs())
T = list(rs())
bool = True

# 上下左右で場合分け
cur = start[0]
for i in range(N):
    if S[i] == 'U':
        cur -= 1
    if cur == 0:
        bool = False
        break
    if T[i] == 'D' and cur < H:
        cur += 1
    
cur = start[0]
for i in range(N):

    if S[i] == 'D':
        cur += 1
    if cur == H+1:
        bool = False
        break
    if T[i] == 'U' and cur > 1:
        cur -= 1
    
cur = start[1]
for i in range(N):
    if S[i] == 'R':
        cur += 1
    if cur == W+1:
        bool = False
        break
    if T[i] == 'L' and cur > 1:
        cur -= 1    

cur = start[1]
for i in range(N):
    if S[i] == 'L':
        cur -= 1
    if cur == 0:
        bool = False
        break
    if T[i] == 'R' and cur < W:
        cur += 1

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

Submission status: Accepted

Solution code:
def main():
    h,w,n=map(int,input().split())
    r,c=map(int,input().split())
    r-=1
    c-=1
    s=input()
    t=input()
    rl=[]
    ud=[]
    for i in range(n-1,-1,-1):
        if t[i]=="D":
            ud.append("+"+"S")
        elif t[i]=="U":
            ud.append("-"+"S")
        elif t[i]=="R":
            rl.append("+"+"S")
        else:
            rl.append("-"+"S")
        if s[i]=="D":
            ud.append("+"+"T")
        elif s[i]=="U":
            ud.append("-"+"T")
        elif s[i]=="R":
            rl.append("+"+"T")
        else:
            rl.append("-"+"T")
    #print(rl)
    #print(ud)

    L=0
    R=w-1
    for i in rl:
        if i[1]=="T":
            if i[0]=="-":
                L+=1
            else:
                R-=1
        else:
            if i[0]=="+":
                L=max(L-1,0)
            else:
                R=min(R+1,w-1)
        if L>R:
            print("NO")
            return 0
    if c<L or R<c:
        print("NO")
        return 0

    L=0
    R=h-1
    for i in ud:
        if i[1]=="T":
            if i[0]=="-":
                L+=1
            else:
                R-=1
        else:
            if i[0]=="+":
                L=max(L-1,0)
            else:
                R=min(R+1,h-1)
        if L>R:
            print("NO")
            return 0
    if r<L or R<r:
        print("NO")
        return 0

    print("YES")
main()

Submission status: Accepted

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

l, r, u, d = 0, 0, 0, 0

for i in range(N):
    if T[i] == 'L' and r > 0:
        r -= 1
    elif T[i] == 'R' and l > 0:
        l -= 1
    elif T[i] == 'U' and d > 0:
        d -= 1
    elif T[i] == 'D' and u > 0:
        u -= 1

    if S[i] == 'L':
        l += 1
    elif S[i] == 'R':
        r += 1
    elif S[i] == 'U':
        u += 1
    elif S[i] == 'D':
        d += 1

    if l+r == W or u+d == H:
        print('NO')
        exit()

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

Submission status: Accepted

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

    xl,xr,yu,yd = sc,sc,sr,sr

    for i in range(n):
        if s[i] == "L":
            xl -= 1
        if s[i] == "R":
            xr += 1
        if s[i] == "U":
            yu -= 1
        if s[i] == "D":
            yd += 1
        if xl <= 0 or xr > w or yu <= 0 or yd > h:
            return "NO"
        if t[i] == "R" and xl < w:
            xl += 1
        if t[i] == "L" and xr > 1:
            xr -= 1
        if t[i] == "D" and yu < h:
            yu += 1
        if t[i] == "U" and yd > 1:
            yd -= 1
    
    return("YES")
print(main())

Submission status: Accepted

Solution code:
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
a=0
for i,j in enumerate(d):
  x=[h-r+1,c,w-c+1,r][i]
  for k in range(n):
    x+=(s[k]==j)
    a|=x>[h,w,w,h][i]
    x-=(dict(zip(d,d[::-1]))[t[k]]==j)*x>1
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
b=[h,w,w,h]
o=[h-r+1,c,w-c+1,r]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    a|=x>b[i]
    x-=(dict(zip(d,d[::-1]))[t[k]]==j)*x>1
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
b=[h,w,w,h]
o=[h-r+1,c,w-c+1,r]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    x-=(dict(zip(d,d[::-1]))[t[k]]==j)*x>1
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
b=[h,w,w,h]
o=[h-r+1,c,w-c+1,r]
t=[dict(zip(d,d[::-1]))[i] for i in t]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    x-=(t[k]==j)*x>1
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
def M():return map(int,input().split())
d='URLD'
h,w,n=M()
b=[h,w,w,h]
r,c=M()
o=[h-r+1,c,w-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    x-=(t[k]==j)*x>1
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
def M():return map(int,input().split())
d='URLD'
h,w,n=M()
b=[h,w,w,h]
r,c=M()
o=[h-r+1,c,w-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    if x>1:x-=(t[k]==j)
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
def M():return map(int,input().split())
d='URLD'
*b,n=M()
b+=b[::-1]
r,c=M()
o=[b[0]-r+1,c,b[1]-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    if x>1:x-=(t[k]==j)
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
d='URLD'
h,w,n=map(int,input().split())
b=[h,w,w,h]
r,c=map(int,input().split())
o=[h-r+1,c,w-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    if x>1:x-=(t[k]==j)
print('YNEOS'[a::2])

Submission status: Accepted

Solution code:
d='URLD'
m={'U':0,'R':1,'D':2,'U':3}
m=dict(zip(d,range(4)))
v={'L':'R','R':'L','D':'U','U':'D'}
v=dict([(d[i],d[3-i]) for i in range(4)])
v=dict(zip(d,d[::-1]))
h,w,n=map(int,input().split())
b=[h,w,w,h]
r,c=map(int,input().split())
o=[h-r+1,c,w-c+1,r]
s=input()
t=[v[i] for i in input()]
a=1
for j,i in m.items():
    c=o[i]
    for k in range(n):
        c+=(s[k]==j)
        if c>b[i]:
            a=0
        if c>1:
            c-=(t[k]==j)
if a:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
#show_flg=True

di='URLD'
mp=dict(zip(list(di),range(4)))
rv=dict(zip(list(di),list(di[::-1])))

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[h-r+1,c,w-c+1,r]
s=input()
t=[rv[i] for i in input()]
ans=True
for j,i in mp.items():
    c=st[i]
    for k in range(n):
        c+=1*(s[k]==j)
        if c>b[i]:
            ans=False
        if c>1:
            c-=1*(t[k]==j)
            
show(s)
show(t)
show(mp,rv,di)
if ans:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
H,W,N=map(int,input().split())
r,c=map(int,input().split())

choku=list(input())
ao=list(input())
choku=choku[::-1]
ao=ao[::-1]

Dwn=1
Up=H
Lef=1
Rgt=W
#print(Dwn,r,Up,Lef,c,Rgt)
def chokudai(c):
    global Dwn
    global Up
    global Lef
    global Rgt
    if choku[c]=="D":
        Dwn+=1
    if choku[c]=='U':
        Up-=1
    if choku[c]=='L':
        Lef+=1
    if choku[c]=='R':
        Rgt-=1
    if Dwn>Up or Lef>Rgt:
        print("NO")
        exit()
#chokudai(0)

def kensho(k):
    global Dwn
    global Up
    global Lef
    global Rgt
    if ao[k]=="U":
        Dwn=max(1,Dwn-1)
    if ao[k]=="D":
        Up=min(H,Up+1)
    if ao[k]=="R":
        Lef=max(1,Lef-1)
    if ao[k]=="L":
        Rgt=min(W,Rgt+1)
#print(Dwn,r,Up,Lef,c,Rgt)
for i in range(N):
    kensho(i)
    #print(Dwn,r,Up,Lef,c,Rgt)
    chokudai(i)
    #print(Dwn,r,Up,Lef,c,Rgt)

if Dwn<=H-r+1 and H-r+1<=Up and Lef<=c and c<=Rgt:
    print("YES")
else:
    print("NO")
    #print(Dwn,r,Up,Lef,c,Rgt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
H, W, N = map(int,input().split())
sr, sc = map(int,input().split())
S = str(input())
T = str(input())
tk_ls = ['L', 'R', 'U', 'D']
ao_ls = ['R', 'L', 'D', 'U']
move_ls = [sc, W-sc+1, sr, H-sr+1]
limit_ls = [W, W, H, H]
for j in range(4):
    tk = tk_ls[j]
    ao = ao_ls[j]
    move = move_ls[j]
    limit = limit_ls[j]
    for i in range(N):
        if S[i] == tk:
            move -= 1
        if move == 0:
            print("NO")
            exit()
        if T[i] == ao and move < limit:
            move += 1
print("YES")

Submission status: Accepted

Solution code:
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()

#LR
x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if l==0:
        l=1
    if r>w:
        r=w
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
#UD
x=h+1-R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if l==0:
        l=1
    if r>h:
        r=h
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")

Submission status: Accepted

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

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

ok = True

now_r = sc
for i in range(N):
    if S[i] == 'R':
        if now_r == W:
            ok = False
            break
        now_r += 1
    if T[i] == 'L':
        now_r = max(now_r-1, 1)

now_l = sc
for i in range(N):
    if S[i] == 'L':
        if now_l == 1:
            ok = False
            break
        now_l -= 1
    if T[i] == 'R':
        now_l = min(now_l+1, W)

now_u = sr
for i in range(N):
    if S[i] == 'U':
        if now_u == 1:
            ok = False
            break
        now_u -= 1
    if T[i] == 'D':
        now_u = min(now_u+1, H)

now_d = sr
for i in range(N):
    if S[i] == 'D':
        if now_d == H:
            ok = False
            break
        now_d += 1
    if T[i] == 'U':
        now_d = max(now_d-1, 1)

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
# from copy import deepcopy, copy

import queue
from collections import deque



def main():
    high, width, num = map(int, input().split())
    s_h, s_w = map(int, input().split())
    data1 = list(input())
    data2 = list(input())

    nokori = s_h - 1
    count = 0
    for i in range(num):
        if data1[i] == 'U':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'D' and s_h - count < high:
            count -= 1

    nokori = s_w - 1
    count = 0
    for i in range(num):
        if data1[i] == 'L':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'R' and s_w - count < width:
            count -= 1

    nokori = high - s_h
    count = 0
    for i in range(num):
        if data1[i] == 'D':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'U' and s_h - 1 + count > 0:
            count -= 1

    nokori = width - s_w
    count = 0
    for i in range(num):
        if data1[i] == 'R':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'L' and s_w - 1 + count > 0:
            count -= 1
        # print(count)

    print('YES')



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

u = 1
d = H
l = 1
r = W
flag = False
for i in range(N-1,-1,-1):
  if i != N-1:
    if T[i] == 'U': d = min(d+1,H)
    elif T[i] == 'D': u = max(u-1,1)
    elif T[i] == 'L': r = min(r+1,W)
    else: l = max(l-1,1)
  if S [i] == 'U': u += 1
  elif S[i] == 'D': d -= 1
  elif S[i] == 'L': l += 1
  else: r -= 1
  if l>r or u>d:
    flag = True
    break
#print(flag,u,d,l,r)
if flag or not (u<=sr<=d and l<=sc<=r):
  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()


def b():
    M = [[h-sr,h-1], [sr-1,h-1], [w-sc,w-1], [sc-1,w-1]]
    D = [["U","D"], ["D","U"], ["L","R"], ["R","L"]]

    for i in range(4):
        # print(M[i])
        j = 0
        while j < n and M[i][0] >= 0:
            if s[j] == D[i][1]:
                M[i][0] -= 1
                if M[i][0] < 0:
                    return "NO"
            if t[j] == D[i][0]:
                M[i][0] = min(M[i][0]+1, M[i][1])                
            # print(i, M[i][0])
            j += 1
    return "YES"

print(b())

Submission status: Accepted

Solution code:

import sys
def input():
	return sys.stdin.readline().strip()

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

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

for times in range(N - 1, -1, -1):
	#print(left, right, up, down)
	direct = T[times]
	if direct == 'L':
		right = min(W, right + 1)
	elif direct == 'R':
		left = max(0, left - 1)
	elif direct == 'U':
		down = min(H, down + 1)
	elif direct == 'D':
		up = max(0, up - 1)
	direct = S[times]
	if direct == 'L':
		left = min(W, left + 1)
	elif direct == 'R':
		right = max(0, right - 1)
	elif direct == 'U':
		up = min(H, up + 1)
	elif direct == 'D':
		down = max(0, down - 1)
	if up >= down or left >= right:
		print("NO")
		exit()


if sc - 1 < right and left <= sc - 1 and sr - 1 >= up and down > sr - 1:
	print("YES")
else:
	print("NO")

Submission status: Accepted

Solution code:
H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
T=input()
A=input()
t,b,l,r=0,H,0,W
for i in range(N-1,-1,-1):
    if A[i]=='U':
        b=min(b+1,H)
    elif A[i]=='D':
        t=max(t-1,0)
    elif A[i]=='L':
        r=min(r+1,W)
    else:
        l=max(l-1,0)
    if T[i]=='U':
        t+=1
    elif T[i]=='D':
        b-=1
    elif T[i]=='L':
        l+=1
    else:
        r-=1
    if t==b or l==r:
        break
print(['NO','YES'][t<sr<=b and l<sc<=r])

Submission status: Accepted

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


def check(pos_dir, neg_dir, minlim, maxlim, start):
    lim = maxlim
    count = 0
    for sc, tc in zip(s[::-1], t[::-1]):
        if tc == pos_dir:
            minlim = max(minlim - 1, 0)
        elif tc == neg_dir:
            maxlim = min(maxlim + 1, lim)
        if sc == pos_dir:
            maxlim -= 1
        elif sc == neg_dir:
            minlim += 1
        count += 1
        if minlim > maxlim:
            return False
    return minlim <= start <= maxlim


for elem in [
    ('R', 'L', 0, w - 1, x - 1),
    ('D', 'U', 0, h - 1, y - 1),
]:
    if not check(*elem):
        print('NO')
        break
else:
    print('YES')

Submission status: Accepted

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


def check(direction, opposite_direction, pos, target):
    for i in range(n):
        if s[i] == direction:
            pos += 1
        if pos == target:
            return False
        if t[i] == opposite_direction and pos > 1:
            pos -= 1
    return True


for d, od, pos, target in [
    ('R', 'L', x, w + 1),
    ('L', 'R', w + 1 - x, w + 1),
    ('D', 'U', y, h + 1),
    ('U', 'D', h + 1 - y, h + 1),
]:
    ok = check(d, od, pos, target)
    if not ok:
        print('NO')
        break
else:
    print('YES')

Submission status: Accepted

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

TS, AS = TS[::-1], AS[::-1]


def calc(length, start, target_chars):
    alive_section = [1, length]

    # [区間のどちらに作用するか, どのように作用するか]
    TX = {'L': [0, 1], 'R': [1, -1], 'U': [0, 1], 'D': [1, -1]}
    AX = {'L': [1, 1], 'R': [0, -1], 'U': [1, 1], 'D': [0, -1]}

    # 最後の青木くんのターンから
    for i in range(N):
        if AS[i] in target_chars:
            j, d = AX[AS[i]]
            alive_section[j] += d

            # 盤外にまで広げることはできない
            alive_section[0] = max(1, alive_section[0])
            alive_section[1] = min(length, alive_section[1])

        if TS[i] in target_chars:
            j, d = TX[TS[i]]
            alive_section[j] += d

        cross_check(alive_section)

    if not (alive_section[0] <= start <= alive_section[1]):
        print('NO')
        exit()


def cross_check(alive_section):
    if alive_section[0] > alive_section[1]:
        print('NO')
        exit()


calc(H, sh, ['U', 'D'])
calc(W, sw, ['L', 'R'])

print('YES')

Submission status: Accepted

Solution code:
import collections

h, w, n = map(int, input().split())
y, x = map(int, input().split())
s = input().rstrip()
t = input().rstrip()
counter = collections.Counter()
for c in s:
    counter[c] += 1


def check(counts, direction, opposite_direction, pos, target):
    for i in range(n):
        counts[direction] -= 1
        if s[i] == direction:
            pos += 1
        if pos == target:
            return False
        if t[i] == opposite_direction and pos > 1 and counts[
                opposite_direction] < pos:
            pos -= 1

    return True


for d, od, pos, target in [
    ('R', 'L', x, w + 1),
    ('L', 'R', w + 1 - x, w + 1),
    ('D', 'U', y, h + 1),
    ('U', 'D', h + 1 - y, h + 1),
]:
    ok = check(collections.Counter(counter.items()), d, od, pos, target)
    if not ok:
        print('NO')
        break
else:
    print('YES')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC33 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
h -= 1
w -= 1
s = input()
t = input()
l,r,d,u = 0,w,0,h
for i in range(n)[::-1]:
    if t[i] == 'R':
        if l > 0:
            l -= 1
    elif t[i] == 'L':
        if r < w:
            r += 1
    elif t[i] == 'D':
        if d > 0:
            d -= 1
    elif t[i] == 'U':
        if u < h:
            u += 1
    
    if s[i] == 'R':
        r -= 1
        if r < 0:
            print('NO')
            quit()
    elif s[i] == 'L':
        l += 1
        if l > w:
            print('NO')
            quit()
    elif s[i] == 'D':
        u -= 1
        if u < 0:
            print('NO')
            quit()
    elif s[i] == 'U':
        d += 1
        if d > h:
            print('NO')
            quit()
    
if l <= sc <= r and d <= sr <= u:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC33 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
h -= 1
w -= 1
s = input()
t = input()
l,r,d,u = 0,w,h,0
for i in range(n)[::-1]:
    if t[i] == 'R':
        if l > 0:
            l -= 1
    elif t[i] == 'L':
        if r < w:
            r += 1
    elif t[i] == 'U':
        if d < h:
            d += 1
    elif t[i] == 'D':
        if u > 0:
            u -= 1
    if s[i] == 'R':
        r -= 1
        if r < 0:
            print('NO')
            quit()
    elif s[i] == 'L':
        l += 1
        if l > w:
            print('NO')
            quit()
    elif s[i] == 'U':
        u += 1
        if u > h:
            print('NO')
            quit()
    elif s[i] == 'D':
        d -= 1
        if d < 0:
            print('NO')
            quit()
    
if l <= sc <= r and u <= sr <= d:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
B = [1, W, 1, H]
for i in range(N - 1, -1, -1):
  if T[i] == 'L': B[1] = min(B[1] + 1, W)
  if T[i] == 'R': B[0] = max(B[0] - 1, 1)
  if T[i] == 'U': B[3] = min(B[3] + 1, H)
  if T[i] == 'D': B[2] = max(B[2] - 1, 1)
  if S[i] == 'L': B[0] += 1
  if S[i] == 'R': B[1] -= 1
  if S[i] == 'U': B[2] += 1
  if S[i] == 'D': B[3] -= 1
  if B[1] < B[0] or B[3] < B[2]:
    print('NO')
    break
else:
  print('YES' if B[0] <= sc <= B[1] and B[2] <= sr <= B[3] else 'NO')

Submission status: Accepted

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

left = 1
right = W
bottom = 1
top = H

if S[-1] == 'L':
    left += 1
elif S[-1] == 'R':
    right -= 1
elif S[-1] == 'U':
    bottom += 1
elif S[-1] == 'D':
    top -= 1

for i in range(N - 1)[:: -1]:
    if T[i] == 'L':
        right = min(W, right + 1)
    elif T[i] == 'R':
        left = max(1, left - 1)
    elif T[i] == 'U':
        top = min(H, top + 1)
    elif T[i] == 'D':
        bottom = max(1, bottom - 1)

    if S[i] == 'L':
        left += 1
    elif S[i] == 'R':
        right -= 1
    elif S[i] == 'U':
        bottom += 1
    elif S[i] == 'D':
        top -= 1

    if left > right or bottom > top:
        print('NO')
        break
else:
    if left <= sc <= right and bottom <= sr <= top:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

left = 1
right = w
bot = 1
top = h

if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    bot += 1
elif s[-1] == "D":
    top -= 1

for i in range(n-2, -1, -1):
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(1, left-1)
    elif t[i] == "U":
        top = min(h, top+1)
    elif t[i] == "D":
        bot = max(1, bot-1)
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        bot += 1
    elif s[i] == "D":
        top -= 1
    if left > right or bot > top:
        print("NO")
        exit()
if left <= sc <= right and bot <= sr <=top:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
t = input()
s = input()
hei = [1,h]
wid = [1,w]
for i in range(n-1,-1,-1):
  if s[i] == "L":
    wid[1] = min(wid[1]+1,w)
  elif s[i] == "R":
    wid[0] = max(wid[0]-1,1)
  elif s[i] == "U":
    hei[1] = min(hei[1]+1,h)
  elif s[i] == "D":
    hei[0] = max(hei[0]-1,1)
  if t[i] == "L":
    wid[0] += 1
  elif t[i] == "R":
    wid[1] -= 1
  elif t[i] == "U":
    hei[0] += 1
  elif t[i] == "D":
    hei[1] -= 1
  if wid[0]>wid[1] or hei[0]>hei[1]:
    print("NO")
    exit(0)
if hei[0] <= sr <= hei[1] and wid[0] <= sc <= wid[1]:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

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

sr = H+1-sr

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

    if s == 'L': L += 1
    elif s == 'R': R -= 1
    elif s == 'D': D += 1
    elif s == 'U': U -= 1
    
    if L>R or D>U:
        # 後手の勝てる場所が存在せず
        break

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

Submission status: Accepted

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()
l, u = 1, 1
r = w
d = h
for i in range(n-1, -1, -1):
    if t[i] == 'L':
        r = min(w, r+1)
    if t[i] == 'R':
        l = max(1, l-1)
    if t[i] == 'U':
        d = min(h, d+1)
    if t[i] == 'D':
        u = max(1, 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")
        quit()
print("YES" if l<=sc<=r and u<=sr<=d else "NO")

Submission status: Accepted

Solution code:

H, W, N = map(int, input().split())
Sr, Sc = map(int, input().split())
Taka = input()
Aoki = input()

mincol = 1
maxcol = W
ans = True
for i in range(N-1, -1, -1):
    if Aoki[i] == "R" and mincol > 1:
        mincol -= 1
    if Aoki[i] == "L" and maxcol < W:
        maxcol += 1

    # print((mincol, maxcol))

    if Taka[i] == "L":
        mincol += 1
    if Taka[i] == "R":
        maxcol -= 1
    # print((mincol, maxcol))
    if mincol > maxcol:
        ans = False
        break
if Sc < mincol or Sc > maxcol:
    ans = False

# print("-------------")

minrow = 1
maxrow = H
for i in range(N-1, -1, -1):
    if Aoki[i] == "D" and minrow > 1:
        minrow -= 1
    if Aoki[i] == "U" and maxrow < H:
        maxrow += 1
    # print((minrow, maxrow))

    if Taka[i] == "U":
        minrow += 1
    if Taka[i] == "D":
        maxrow -= 1
    # print((minrow, maxrow))
    if minrow > maxrow:
        ans = False
        break

if Sr < minrow or Sr > maxrow:
    ans = False

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