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())
sr, sc = map(int, input().split())
s = input()
t = input()

ans = 0

tu = sr - 1
td = h - sr
tl = sc - 1
tr = w - sc

tkh_count_u = 0
tkh_count_d = 0
tkh_count_l = 0
tkh_count_r = 0

aok_count_u = 0
aok_count_d = 0
aok_count_l = 0
aok_count_r = 0

for i in range(n):
    if s[i] == "U":
        tkh_count_u += 1
    if s[i] == "D":
        tkh_count_d += 1
    if s[i] == "L":
        tkh_count_l += 1
    if s[i] == "R":
        tkh_count_r += 1

    if tu < tkh_count_u - aok_count_d:
        ans = 1
        break
    if td < tkh_count_d - aok_count_u:
        ans = 1
        break
    if tl < tkh_count_l - aok_count_r:
        ans = 1
        break
    if tr < tkh_count_r - aok_count_l:
        ans = 1
        break

    if t[i] == "U":
        if aok_count_u - tkh_count_d < tu:
          aok_count_u += 1
    if t[i] == "D":
        if aok_count_d - tkh_count_u < td:
          aok_count_d += 1
    if t[i] == "L":
        if aok_count_l - tkh_count_r < tl:
          aok_count_l += 1
    if t[i] == "R":
        if aok_count_r - tkh_count_l < tr:
          aok_count_r += 1

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

Submission status: Accepted

Solution code:
def solve(H, W, N, sr, sc, S, T):
    # Up
    i = sr
    for s, t in zip(S, T):
        if s == "U":
            i -= 1
        if i < 1:
            return False
        if t == "D" and i + 1 <= H:
            i += 1
    # Down
    i = sr
    for s, t in zip(S, T):
        if s == "D":
            i += 1
        if i > H:
            return False
        if t == "U" and i - 1 >= 1:
            i -= 1

    # Right
    i = sc
    for s, t in zip(S, T):
        if s == "R":
            i += 1
        if i > W:
            return False
        if t == "L" and i - 1 >= 1:
            i -= 1

    # Left
    i = sc
    for s, t in zip(S, T):
        if s == "L":
            i -= 1
        if i < 1:
            return False
        if t == "R" and i + 1 <= W:
            i += 1

    return True

if __name__ == "__main__":
    H, W, N = tuple(map(int, input().split(" ")))
    sr, sc = tuple(map(int, input().split(" ")))
    S = input()
    T = input()
    print("YES" if solve(H, W, N, sr, sc, S, T) else "NO")

Submission status: Accepted

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

ans = True
si = sr
sj = sc

for i in range(N):
    if S[i] == "R":
        sj += 1
    if sj > W:
        ans = False
        break
    if T[i] == "L":
        sj = max(1,sj -1)
si = sr
sj = sc

for i in range(N):
    if S[i] == "L":
        sj -= 1
    if sj < 1 or (not ans):
        ans = False
        break
    if T[i] == "R":
        sj = min(W,sj +1)
si = sr
sj = sc

for i in range(N):
    if S[i] == "D":
        si += 1
    if si > H or (not ans):
        ans = False
        break
    if T[i] == "U":
        si = max(1,si -1)
si = sr
sj = sc

for i in range(N):
    if S[i] == "U":
        si -= 1
    if si < 1 or (not ans):
        ans = False
        break
    if T[i] == "D":
        si = min(H,si +1)

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

Submission status: Accepted

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

#drop by up
r = R
for i in range(N):
    if S[i] == 'U':
        r -= 1
        if r == 0:
            print('NO')
            exit()
    if T[i] == 'D' and r < H:
        r += 1

#drop by down
r = R
for i in range(N):
    if S[i] == 'D':
        r += 1
        if r == H + 1:
            print('NO')
            exit()
    if T[i] == 'U' and r > 1:
        r -= 1

#drop by left
c = C
for i in range(N):
    if S[i] == 'L':
        c -= 1
        if c == 0:
            print('NO')
            exit()
    if T[i] == 'R' and c < W:
        c += 1

#drop by right
c = C
for i in range(N):
    if S[i] == 'R':
        c += 1
        if c == W + 1:
            print('NO')
            exit()
    if T[i] == 'L' and c > 1:
        c -= 1

print('YES')

Submission status: Accepted

Solution code:
H,W,N = list(map(int,input().split()))
h,w = list(map(int,input().split()))
h2 = h
w2 = w

S = input()
T = input()

result = "YES"

#左上を目指す
for i in range(N):
    if S[i] == "L":
        w -= 1
        if w < 1:
            result = "NO"
            break
    elif S[i] == "U":
        h -= 1
        if h < 1:
            result = "NO"
            break
    
    if T[i] == "R":
        w += 1
        if w > W:
            w -= 1
    elif T[i] == "D":
        h += 1
        if h > H:
            h -= 1


#右下を目指す
if result == "YES":
    for i in range(N):
        if S[i] == "R":
            w2 += 1
            if w2 > W:
                result = "NO"
                break
        elif S[i] == "D":
            h2 += 1
            if h2 > H:
                result = "NO"
                break
        
        if T[i] == "L":
            w2 -= 1
            if w2 < 1:
                w2 += 1
        elif T[i] == "U":
            h2 -= 1
            if h2 < 1:
                h2 += 1
        


print(result)

Submission status: Accepted

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

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

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

k = sr - 1
for i in range(N):
  if S[i] == "D":
    k += 1
    if k >= H:
      print("NO")
      exit()
  if T[i] == "U":
    k = max(k - 1, 0)

k = sr - 1
for i in range(N):
  if S[i] == "U":
    k -= 1
    if k < 0:
      print("NO")
      exit()
  if T[i] == "D":
    k = min(k + 1, H - 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 = 0
right = 0
left = 0
up = 0
down = 0

for i in range(n):
    if s[i] == "R":
        right += 1
    if right >= w - sc + 1:
        flag = 1
        break
    if t[i] == "L" and -right < sc - 1:
        right -= 1


    if s[i] == "L":
        left += 1
    if left >= sc:
        flag = 1
        break
    if t[i] == "R" and -left < w - sc:
        left -= 1

    if s[i] == "U":
        up += 1
    if up >= sr:
        flag = 1
        break
    if t[i] == "D" and -up < h - sr:
        up -= 1

    if s[i] == "D":
        down += 1
    if down >= h - sr + 1:
        flag = 1
        break
    if t[i] == "U" and -down < sr - 1:
        down -= 1


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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    h,w = LI()
    a = [sys.stdin.readline() for i in range(h)]
    s = 0
    p = [None for i in range(w*h)]
    q = [None for j in range(w*h)]
    l = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                p[s] = (i,j)
                s += 1
            else:
                q[l] = (i,j)
                l += 1
    if s == h*w:
        print(0)
        quit()
    ans = 0
    M = w*h
    for i,j in q[:h*w-s]:
        m = M
        for x,y in p[:s]:
            if abs(i-x)+abs(j-y) < m:
                m = abs(i-x)+abs(j-y)
        if m > ans:
            ans = m
    print(ans)
    return

#B
def B():
    h,w,n = LI()
    sy,sx = LI()
    sy -= 1
    sx -= 1
    s = S()
    t = S()
    ks = list("LRUD")
    kt = list("RLDU")
    ds = [[0,-1],[0,1],[-1,0],[1,0]]
    dt = [[0,1],[0,-1],[1,0],[-1,0]]
    for i in range(4):
        y,x = [sy,sx]
        for j in range(n):
            if s[j] == ks[i]:
                y+=ds[i][0]
                x+=ds[i][1]
                if y < 0 or y >= h or x < 0 or x >= w:
                    print("NO")
                    quit()
            if t[j] == kt[i]:
                y += dt[i][0]
                x += dt[i][1]
                if y < 0:y = 0
                elif y >= h:y = h-1
                elif x < 0:x=0
                elif x >= w:x=w-1
    print("YES")
    return

#C
def C():
    n = I()
    return

#D
def D():
    n = I()
    return

#E
def E():
    n = I()
    return

#F
def F():
    n = I()
    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

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

for [s, t, d, l] in [["L", "R", sw, w], ["R", "L", w-sw+1, w], ["U", "D", sh, h], ["D", "U", h-sh+1, h]]:
#  print(s, t, d)
  x = d
  for i in range(n):
#    print(x)
    if S[i] == s:
      x -= 1
    if x == 0:
      print("NO")
      exit()
    if T[i] == t and x < l:
      x += 1
    

print("YES")

Submission status: Accepted

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

from collections import deque
import heapq
import math
import bisect


def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S, T = input(), input()
    dic = {"L" : 0, "R" : 1, "U" : 2, "D" : 3}
    dic2 = {"L" : 1, "R" : 0, "U" : 3, "D" : 2}

    cnt = [0] * 4
    need = [sc, W-sc+1, sr, H-sr+1]
    for i in range(N):
        cnt[dic[S[i]]] += 1
        for j in range(4):
            if need[j] <= cnt[j]:
                print("NO")
                exit()

        if T[i] == "L" and 1 < sc + cnt[1]:
            cnt[dic2[T[i]]] -= 1
        elif T[i] == "R" and sc - cnt[0] < W:
            cnt[dic2[T[i]]] -= 1
        elif T[i] == "U" and 1 < sr + cnt[3]:
            cnt[dic2[T[i]]] -= 1
        elif T[i] == "D" and sr - cnt[2] < H:
            cnt[dic2[T[i]]] -= 1

    print("YES")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    h, w, n = LI()
    sr, sc = LI_()
    S = SI()
    T = SI()

    D = {0: ('L', 'R', -1), 1: ('R', 'L', 1), 2: ('U', 'D', -1), 3: ('D', 'U', 1)}
    is_drop = False
    for t in range(2):
        taka, aoki, d = D[t]
        x = sc
        for i in range(n):
            if S[i] == taka:
                x += d
                if x < 0 or x >= w:
                    is_drop = True
                    break
            if T[i] == aoki:
                x -= d
                if x < 0:
                    x = 0
                if x >= w:
                    x = w - 1

    for t in range(2, 4):
        taka, aoki, d = D[t]
        y = sr
        for i in range(n):
            if S[i] == taka:
                y += d
                if y < 0 or y >= h:
                    is_drop = True
                    break
            if T[i] == aoki:
                y -= d
                if y < 0:
                    y = 0
                if y >= h:
                    y = h - 1

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



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
list=input().split(" ")
h=int(list[0])
w=int(list[1])
n=int(list[2])

list=input().split(" ")
y=int(list[0])
x=int(list[1])

#l,r,u,d
ato=[x, w-x+1, y, h-y+1]
countt=[0,0,0,0]
counta=[0,0,0,0]
counta2=[0,0,0,0] #dokudan
kano=[0,0,0,0]
#print(ato)

taka=input()
aoki=input()

switch=0

for i in range(n):
    if i>0:
        if aoki[i-1]=='L':
            if counta[0]-countt[1]<x-1:
                counta[0]+=1
        elif aoki[i-1]=='R':
            if counta[1]-countt[0]<w-x:
                counta[1]+=1
        elif aoki[i-1]=='U':
            if counta[2]-countt[3]<y-1:
                counta[2]+=1
        elif aoki[i-1]=='D':
            if counta[3]-countt[2]<h-y:
                counta[3]+=1
    
    if taka[i]=='L':
        countt[0]+=1
    elif taka[i]=='R':
        countt[1]+=1
    elif taka[i]=='U':
        countt[2]+=1
    elif taka[i]=='D':
        countt[3]+=1
    
    
    countn=[countt[0]-counta[1], countt[1]-counta[0], countt[2]-counta[3], countt[3]-counta[2]]
    
    
    #print(countn)
    for j in range(4):
        if countn[j]>=ato[j]:
            switch=1

if switch==1:
    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()


l = 1
r = W
u = 1
d = H
#print(l, r, u, d,)
for s, t in zip(S[::-1], T[::-1]):

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

    #print(d)
    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()
    #print(l, r, u, d, " st", s, t)
if u<=Sr<=d and l<=Sc<=r:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]

H, W, n = map(int, input().split())
r, c = map(int, input().split())
s = input()
t = input()

L, R, U, D = c, W+1-c, r, H+1-r

dp = [[0 for _ in range(4)] for _ in range(n+1)]
dp[0][0] = L
dp[0][1] = R
dp[0][2] = U
dp[0][3] = D
for i in range(1, n+1):
	if dp[i-1][0] == 1 and s[i-1] == "L":
		print("NO")
		sys.exit()
	if dp[i-1][1] == 1 and s[i-1] == "R":
		print("NO")
		sys.exit()
	if dp[i-1][2] == 1 and s[i-1] == "U":
		print("NO")
		sys.exit()
	if dp[i-1][3] == 1 and s[i-1] == "D":
		print("NO")
		sys.exit()
	for j in range(4):
		dp[i][j] = dp[i-1][j]
	if s[i-1] == "L":# and t[i-1] != "R":
		dp[i][0] -= 1
	if s[i-1] == "R":# and t[i-1] != "L":
		dp[i][1] -= 1
	if s[i-1] == "U":# and t[i-1] != "D":
		dp[i][2] -= 1
	if s[i-1] == "D":# and t[i-1] != "U":
		dp[i][3] -= 1
	if t[i-1] == "L" and dp[i][1] < W:
		dp[i][1] += 1
	if t[i-1] == "R" and dp[i][0] < W:
		dp[i][0] += 1
	if t[i-1] == "U" and dp[i][3] < H:
		dp[i][3] += 1
	if t[i-1] == "D" and dp[i][2] < H:
		dp[i][2] += 1

	#print(dp[i-1], dp[i])

#print(dp)
print("YES")

Submission status: Accepted

Solution code:
H, W, n = [int(i) for i in input().split()]
r, c = [int(i) for i in input().split()]
S = input()
T = input()

L = 0
R = 1
U = 2
D = 3
f = {'L': 0, 'R': 1, 'U': 2, 'D': 3}
rf = {'L': 1, 'R': 0, 'U': 3, 'D': 2}

need = [c, W - c + 1, r, H - r + 1]

for i in range(n):
    need[f[S[i]]] -= 1
    if need[f[S[i]]] <= 0:
        print('NO')
        exit()
    need[rf[T[i]]] = min(W if T[i] == 'L' or T[i] == 'R' else H , need[rf[T[i]]] + 1)

print('YES')

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

h,w,n = getList()
sy, sx = getList()
taka = input()
aoki = input()

def game(taka, aoki, mt, ma, buf, bmax):
    for ta, ao in zip(taka, aoki):
        if ta == mt:
            buf -= 1
            if buf == 0:
                return False
        if ao == ma:
            buf = min(buf + 1, bmax)

    return True

ga = game(taka,aoki,"U", "D", sy, h)
gb = game(taka,aoki,"D", "U", h - sy + 1, h)
gc = game(taka,aoki,"L", "R", sx, w)
gd = game(taka,aoki,"R", "L", w - sx + 1, w)

if ga and gb and gc and gd:
    print("YES")

else:
    print("NO")

Submission status: Accepted

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

h,w,n = [int(x) for x in input().split()]

sy,sx = [int(x) for x in input().split()]

S = input().rstrip()
T = input().rstrip()

smallx = bigx = sx
smally = bigy = sy

dx = 0
dy = 0

for i in range(n):
    c = S[i]
    if c=='R':
        bigx += 1
    elif c=='L':
        smallx -= 1
    elif c=='U':
        smally -= 1
    elif c=='D':
        bigy += 1

    if smallx<1:
        tmp = min(1 - smallx, dx)
        dx -= tmp
        smallx += tmp
        bigx += tmp
    
    if smally<1:
        tmp = min(1 - smally, dy)
        dy -= tmp
        smally += tmp
        bigy += tmp
    

    if smallx < 1 or bigx > w or smally < 1 or bigy > h:
        print 'NO'
        sys.exit()


    c = T[i]
    if c=='R':
        smallx += 1
    elif c=='L':
        bigx -= 1
    elif c=='U':
        bigy -= 1
    elif c=='D':
        smally += 1

    # default to left
    if smallx>bigx:
        smallx = bigx
        dx += 1
    if smally>bigy:
        smally = bigy
        dy += 1

    if smallx<1:
        tmp = min(1 - smallx, dx)
        dx -= tmp
        smallx += tmp
        bigx += tmp
    
    if smally<1:
        tmp = min(1 - smally, dy)
        dy -= tmp
        smally += tmp
        bigy += tmp


    if smallx < 1 or bigx > w or smally < 1 or bigy > h:
        print 'NO'
        sys.exit()
print 'YES'
sys.exit()

Submission status: Accepted

Solution code:
H,W,N = map(int,input().split())
sr,sc = map(int,input().split())
S = input()
T = input()
hr = sc
hl = sc
hu = sr
hd = sr
State = "YES"
for i in range(N):
    if S[i] == "R":
        hr += 1
        if hr > W:
            State = "NO"
            break
    elif S[i] == "L":
        hl -= 1
        if hl < 1:
            State = "NO"
            break
    elif S[i] == "U":
        hu -= 1
        if hu < 1:
            State = "NO"
            break
    else:
        hd += 1
        if hd > H:
            State = "NO"
            break
    if T[i] == "R":
        if hl < W:
            hl += 1
    if T[i] == "L":
        if hr > 1:
            hr -= 1
    if T[i] == "U":
        if hd > 1:
            hd -= 1
    if T[i] == "D":
        if hu < H:
            hu += 1
print(State)

Submission status: Accepted

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

flag = 0
hidari = sc
migi = sc
ue = sr
shita = sr
for i in range(N):
  if S[i] == 'L':
    hidari -= 1
  if S[i] == 'R':
    migi += 1
  if S[i] == 'U':
    ue -= 1
  if S[i] == 'D':
    shita += 1
  if hidari == 0 or migi == W+1 or ue == 0 or shita == H+1 :
    flag = 1
  
  if T[i] == 'R' and hidari != W:
    hidari += 1
  if T[i] == 'L' and migi != 1:
    migi -= 1
  if T[i] == 'D' and ue != H:
    ue += 1
  if T[i] == 'U' and shita != 1:
    shita -= 1

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

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    h, w, n = map(int, stdin.readline().split())
    r, c = map(int, stdin.readline().split())
    s = stdin.readline().strip()
    t = stdin.readline().strip()
    x = r
    y = c
    blx = 0
    brx = h + 1
    bly = 0
    bry = w + 1
    for i in xrange(n - 1, -1, -1):
        a, b = s[i], t[i]
        if b == 'U':
            if brx <= h:
                brx += 1
        elif b == 'D':
            if blx >= 1:
                blx -= 1
        elif b == 'L':
            if bry <= w:
                bry += 1
        else :
            if bly >= 1:
                bly -= 1
        if a == 'U':
            blx += 1
        elif a == 'D':
            brx -= 1
        elif a == 'L':
            bly += 1
        else:
            bry -= 1
        if brx - blx <= 1 or bry - bly <= 1:
            print 'NO'
            return
    if blx < r < brx and bly < c < bry:
        print 'YES'
    else:
        print 'NO'
main()

Submission status: Accepted

Solution code:
H,W,N = map(int, input().split())
s_r, s_c = map(int, input().split())
S = input()
T = input()
Judge = False

S_L, S_R, S_U, S_D = 0, 0, 0, 0
T_L, T_R, T_U, T_D = 0, 0, 0, 0

for x in range(N):
    if S[x] == 'L':
        S_L += 1
        if S_L - T_R - s_c == 0:
            Judge = True
    elif S[x] == 'R':
        S_R += 1
        if s_c + (S_R - T_L) == W+1:
            Judge = True
    elif S[x] == 'U':
        S_U += 1
        if S_U - T_D - s_r == 0:
            Judge = True
    elif S[x] == 'D':
        S_D += 1
        if s_r + (S_D - T_U) == H+1:
            Judge = True
    if T[x] == 'L':
        if S_R - T_L + s_c != 1:
            T_L += 1
    if T[x] == 'R':
        if s_c + (T_R - S_L) != W:
            T_R += 1
    if T[x] == 'U':
        if S_D - T_U + s_r != 1:
            T_U += 1
    if T[x] == 'D':
        if s_r + (T_D - S_U) != H:
            T_D += 1
    
if Judge:
    print('NO')
else:
    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().strip()
T=input().strip()

#高橋君が右に動かすとき
rest=W-sc+1

for i in range(N):
    if S[i]=="R":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

    if T[i]=="L":
        rest=min(W,rest+1)

#左

rest=sc

for i in range(N):
    if S[i]=="L":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

    if T[i]=="R":
        rest=min(W,rest+1)

#上

rest=sr

for i in range(N):
    if S[i]=="U":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

    if T[i]=="D":
        rest=min(H,rest+1)

#下

rest=H-sr+1

for i in range(N):
    if S[i]=="D":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

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


print("YES")

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 range(N-1):

    # Takahashi's turn
    if S[N-1-i] == "U":
        border_U += 1
    elif S[N-1-i] == "D":
        border_D -= 1
    elif S[N-1-i] == "L":
        border_L += 1
    elif S[N-1-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[N - 2 - i] == "U":
        border_D += 1
    elif T[N - 2 - i] == "D":
        border_U -= 1
    elif T[N - 2 - i] == "L":
        border_R += 1
    elif T[N - 2 - i] == "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)
    # print(border_U, border_D, border_L, border_R)

# 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(border_U, border_D, border_L, border_R)
print(res)

Submission status: Accepted

Solution code:
def check(dist, tak, aok, lim):
    for s, t in zip(ss, ts):
        if s == tak:
            if dist == 0:
                return True
            dist -= 1
        if t == aok:
            if dist < lim:
                dist += 1
    return False


def solve():
    if check(sr - 1, 'U', 'D', h - 1):
        return True
    if check(sc - 1, 'L', 'R', w - 1):
        return True
    if check(h - sr, 'D', 'U', h - 1):
        return True
    if check(w - sc, 'R', 'L', w - 1):
        return True
    return False


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

Submission status: Accepted

Solution code:
# encoding: utf-8
import sys
# input = sys.stdin.readline
inf = 10 ** 18

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

S = list(input())
T = list(input())

y_t = y_b = sr
x_r = x_l = sc

ans = 'NO'
for i, (Si, Ti) in enumerate(zip(S, T)):
    # tak
    if Si == 'L':
        x_l -= 1
    elif Si == 'R':
        x_r += 1
    elif Si == 'U':
        y_t -= 1
    else:
        y_b += 1
    # check
    if x_l < 1: break
    if y_t < 1: break
    if x_r > W: break
    if y_b > H: break
    # aok
    if Ti == 'L':
        x_r = max(1, x_r - 1)
    elif Ti == 'R':
        x_l = min(W, x_l + 1)
    elif Ti == 'U':
        y_b = max(1, y_b - 1)
    else:
        y_t = min(H, y_t + 1)
else:
    ans = 'YES'
        
print(ans)

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

H, W, N = map(int, input().split())
sr, sc =  map(int, input().split())
sr -= 1
sc -= 1

S = input()
T = input()

def solve2(sc, W, S, T, inc, dec):
    x = sc
    for s, t in zip(S, T):
        if s == inc:
            x += 1
            if x == W:
                return True
        if t == dec:
            x = max(x - 1, 0);

    x = sc
    for s, t in zip(S, T):
        if s == dec:
            x -= 1
            if x == -1:
                return True
        if t == inc:
            x = min(x + 1, W - 1);

    return False


def solve(H, W, N, sr, sc, S, T):
    res1 = solve2(sc, W, S, T, 'R', 'L')
    res2 = solve2(sr, H, S, T, 'D', 'U')
    return res1 or res2

    # x = sc
    # for s, t in zip(S, T):
    #     if s == 'R':
    #         x += 1
    #         if x == W:
    #             return True
    #     if t == 'L':
    #         x = max(x - 1, 0);

    # x = sc
    # for s, t in zip(S, T):
    #     if s == 'L':
    #         x -= 1
    #         if x == -1:
    #             return True
    #     if t == 'R':
    #         x = min(x + 1, W - 1);


            


res = solve(H, W, N, sr, sc, S, T)
if res:
    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()

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

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

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

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

print('YES')

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
  int H, W, N;
  cin>>H>>W>>N;
  int Sr, Sc;
  cin>>Sr>>Sc;
  Sr--;
  Sc--;
  string S, T;
  cin>>S>>T;
  int migi=W, hidari=-1, ue=-1, shita=H;
  bool can = true;
  for(int i=N-1;i>=0;--i) {
    if(T[i]=='L') {
      migi = min(W, migi+1);
    }
    if(T[i]=='R') {
      hidari = max(-1, hidari-1);
    }
    if(T[i]=='U') {
      shita = min(H, shita+1);
    }
    if(T[i]=='D') {
      ue = max(-1, ue-1);
    }
    if(S[i]=='L') {
      hidari++;
    }
    if(S[i]=='R') {
      migi--;
    }
    if(S[i]=='U') {
      ue++;
    }
    if(S[i]=='D') {
      shita--;
    }
    if(ue+1>=shita || hidari+1>=migi) can = false;
  }
  if(Sr<=ue || Sr>=shita || Sc>=migi || Sc<=hidari) can = false;
  if(can) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
}