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: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200100)

h,w,n = map(int, input().split(" "))
sr,sc = map(int, input().split(" "))

s = input()
t = input()

stst = ""
for i in range(n):
    stst += s[i]
    stst += t[i]

def dfsx(pos, x):

    sl = s[pos:].count("L")
    sr = s[pos:].count("R")
    tl = t[pos:].count("L")
    tr = t[pos:].count("R")

    if x <= 0 or x > w or (sl - tr) >= x or x+(sr - tl) > w:
        # 落ちたらFalse
        return False
    
    if pos == 2*n:
        # 最後まで落ちていなければTrue
        return True
    
    g = stst[pos]
    a = dfsx(pos+1, x)
    b = dfsx(pos+1, x + int(g=="R") - int(g=="L")) if g in "RL" else a

    if pos % 2 == 0:
        # 落とす未来があればFalse
        return a and b
    else:
        # 落とさない未来があればTrue
        return a or b

def dfsy(pos, y):
    
    su = s[pos:].count("U")
    sd = s[pos:].count("D")
    tu = t[pos:].count("U")
    td = t[pos:].count("D")

    if y <= 0 or y > h or (su - td) >= y or y+(sd - tu) > h :
        # 落ちたらFalse
        return False
    
    if pos == 2*n:
        # 最後まで落ちていなければTrue
        return True
    
    g = stst[pos]
    a = dfsy(pos+1, y)
    b = dfsy(pos+1, y + int(g=="D") - int(g=="U")) if g in "UD" else a

    if pos % 2 == 0:
        # 落とす未来があればFalse
        return a and b
    else:
        # 落とさない未来があればTrue
        return a or b



print("YES" if dfsx(0, sc) and dfsy(0, sr) else "NO")

Submission status: Wrong Answer

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

margins = [sc - 1, w - sc, sr - 1, h - sr]
# print(margins)
s_cum = [0] * 4
t_cum = [0] * 4
for si, ti in zip(s, t):

    s_cum["LRUD".find(si)] += 1

    if s_cum[0] > margins[0]:
        if t_cum[1]:
            t_cum[1] -= 1
            s_cum[0] -= 1
        else:
            print("NO")
            break

    if s_cum[1] > margins[1]:
        if t_cum[0]:
            t_cum[0] -= 1
            s_cum[1] -= 1
        else:
            print("NO")
            break

    if s_cum[2] > margins[2]:
        if t_cum[3]:
            t_cum[3] -= 1
            s_cum[2] -= 1
        else:
            print("NO")
            break

    if s_cum[3] > margins[3]:
        if t_cum[2]:
            t_cum[2] -= 1
            s_cum[3] -= 1
        else:
            print("NO")
            break

    t_cum["LRUD".find(ti)] += 1

    t_cum = [min(i, j) for i, j in zip(t_cum, margins)]

    # print(si, ti, s_cum, t_cum)
else:
    print("YES")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(len(s)):
    if s[i] == 'L':
        s_c = s_c - 1
    elif s[i] == 'R':
        s_c = s_c + 1
    elif s[i] == 'U':
        s_r = s_r - 1
    elif s[i] == 'D':
        s_r = s_r + 1

    print(s_r,s_c)

    if s_r <= 0 or s_r > h or s_c <= 0 or s_c > w:
        print('No')
        break

    if t[i] == 'L':
        s_c = s_c - 1
    elif t[i] == 'R':
        s_c = s_c + 1
    elif t[i] == 'U':
        s_r = s_r - 1
    elif t[i] == 'D':
        s_r = s_r + 1

    print(s_r,s_c)

    if s_r <= 0 or s_r > h or s_c <= 0 or s_c > w:
        print('No')
        break

if s_r > 0 and s_r <= h and s_c > 0 and s_c <= w:
    print('Yes')

Submission status: Wrong Answer

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
        maxL = max(maxL, left[i+1])
    elif S[i] == "R":
        right[i+1] = right[i] + 1
        maxR = max(maxR, right[i+1])
    elif S[i] == "U":
        up[i+1] = up[i] + 1
        maxU = max(maxU, up[i+1])
    else:
        down[i+1] = down[i] + 1
        maxD = max(maxD, down[i+1])
    if T[i] == "L":
        right[i+1] = max(right[i] - 1, -sc + 1)
    elif T[i] == "R":
        left[i+1] = max(left[i] - 1, sc - W)
    elif T[i] == "U":
        down[i+1] = max(down[i] - 1, -sr + 1)
    else:
        up[i+1] = max(up[i] - 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: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
def taka(k,pos):
    if k=='U' and pos[0]-1<h-pos[0]:
        pos[0] -= 1
    if k=='D' and h-pos[0]-1<pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]-1<pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1<w-pos[1]:
        pos[1] -= 1
    return pos
def ao(k,pos):
    if k=='U' and pos[0]-1>h-pos[0]:
        pos[0] -= 1
    if k=='D' and h-pos[0]-1>pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]-1>pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1>w-pos[1]:
        pos[1] -= 1
    return pos
pos = [sr,sc]
for i in range(n):
    pos = taka(s[i],pos)
    if pos[0]<1 or pos[1]<1 or pos[0]>h or pos[1]>w:
        print('NO')
        exit()
    pos = ao(t[i],pos)
print('YES')

Submission status: Wrong Answer

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 - Sr + 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: Wrong Answer

Solution code:
h, w, n = [int(_) for _ in input().split()]
sr, sc = [int(_) for _ in input().split()]
s = input()
t = input()
for i, (se, te) in enumerate(zip(s,t)):
    if se == 'L':
        if min(w-sc+1, sc) > min(w-sc+2, sc-1):
            sc -= 1
    elif se == 'R':
        if min(w-sc+1, sc) > min(w-sc, sc+1):
            sc += 1
    elif se == 'U':
        if min(h-sr+1, sr) > min(h-sr+2, sr-1):
            sr -= 1
    elif se == 'D':
        if min(h-sr+1, sr) > min(h-sr, sr+1):
            sr += 1

    if sr == 0 or sr == h+1 or sc == 0 or sc == w+1:
        break

    if te == 'L':
        if min(w-sc+1, sc) < min(w-sc+2, sc-1):
            sc -= 1
    elif te == 'R':
        if min(w-sc+1, sc) < min(w-sc, sc+1):
            sc += 1
    elif te == 'U':
        if min(h-sr+1, sr) < min(h-sr+2, sr-1):
            sr -= 1
    elif te == 'D':
        if min(h-sr+1, sr) < min(h-sr, sr+1):
            sr += 1

if sr == 0 or sr == h+1 or sc == 0 or sc == w+1:
    print("NO")
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
print("YES")

Submission status: Wrong Answer

Solution code:
def solve(start, newS, newT, newH):
    # print(start, newS, newT, newH)
    s_max = start
    s_min = start
    t_plus = 0
    t_minus = 0
    t_force = 0
    for i, s in enumerate(newS):
        # print("s", s, 't', t_force, t_plus, t_minus)
        if s == 1:
            s_max += 1
            if s_max - t_force >= newH:
                if t_minus <= 0:
                    return False
                # t_force +=1
                s_max -= 1
                t_minus -= 1
        elif s == -1:
            s_min -= 1
            if s_min - t_force < 0:
                if t_plus <= 0:
                    return False
                # t_force -= 1
                s_min += 1
                t_plus -= 1
        if newT[i] == 1:
            t_plus += 1
        elif newT[i] == -1:
            t_minus += 1
    return True

#
# assert solve(1, [0, 0, 0, 1, 1, -1, -1], [1, -1, 0, 0, 0, 0, 0, 0, 0], 3) == False
# assert solve(1, [0, 0, 0], [0, 1, -1], 2)
# assert solve(1, [1, 1, -1], [-1, 0, 0], 3)

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

sr, sc = list(map(int, input().split()))
sr -= 1
sc -= 1
S = input()
T = input()

S1 = [1 if s == 'U' else -1 if s == 'D' else 0 for s in S]
T1 = [1 if s == 'U' else -1 if s == 'D' else 0 for s in T]
S2 = [1 if s == 'R' else -1 if s == 'L' else 0 for s in S]
T2 = [1 if s == 'R' else -1 if s == 'L' else 0 for s in T]

# print(solve(sr, S1, T1, H))
# print(solve(sc, S2, T2, W))
if solve(sr, S1, T1, H) and solve(sc, S2, T2, W):
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:

def Takahashi(now_w,now_h,next_w,next_h,center_w,center_h):
    if EuclidDis(now_w,now_h,center_w,center_h) < EuclidDis(next_w,next_h,center_w,center_h):
        return (next_w,next_h)
    else:
        return (now_w,now_h)
def Aoki(now_w,now_h,next_w,next_h,center_w,center_h):
    if EuclidDis(now_w,now_h,center_w,center_h) > EuclidDis(next_w,next_h,center_w,center_h):
        return (next_w,next_h)
    else:
        return (now_w,now_h)

def EuclidDis(x0,y0,x1,y1):
    return ((x0-x1)**2 + (y0-y1)**2)

def isRemain(sr,sc,op):
    center_h = H / 2
    center_w = W / 2
    now_h = sr-1
    now_w = sc-1
    for i in range(len(op)):
        if op[i] == 'L':
            next_w = now_w - 1
            next_h = now_h
        elif op[i] == 'R':
            next_w = now_w + 1
            next_h = now_h
        elif op[i] == 'U':
            next_w = now_w
            next_h = now_h + 1
        elif op[i] == 'D':
            next_w = now_w
            next_h = now_h - 1
        if i % 2 == 0:#Sのop
            now_w,now_h = Takahashi(now_w,now_h,next_w,next_h,center_w,center_h)
            #print('Takahashi nw={} nh={}'.format(now_w,now_h))
        else:#Tのop
            now_w,now_h = Aoki(now_w,now_h,next_w,next_h,center_w,center_h)
            #print('Aoki nw={} nh={}'.format(now_w,now_h))
        if  0 > now_w or now_w >= W or 0 > now_h or now_h >= H:
            return False
    return True

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

for i in range(N):
    op.append(S[i])
    op.append(T[i])

#print(op)
if isRemain(sr,sc,op):
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# l, r, u, d
move_limit = [-(w-sr+1), -sr, -(h-sc+1), -sc]
max_move = [0] * 4
move = [0] * 4
direction = "LRUD"
reverse = "RLDU"
for i in range(n):
    move[direction.index(s[i])] += 1
    max_move[direction.index(s[i])] = max(move[direction.index(s[i])], max_move[direction.index(s[i])])
    if move[reverse.index(t[i])] < move_limit[reverse.index(t[i])]:
        move[reverse.index(t[i])] -= 1

if max_move[0] >= sr or max_move[1] > w-sr or max_move[2] >= sc or max_move[3] > h-sc:
    print("NO")
else:
    print("YES")

Submission status: Wrong Answer

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

G_L = sc
G_R = sc
G_U = sr
G_D = sr
flag = True
for i in range(len(S)):
   if S[i] == 'L':
       G_L = G_L - 1
   if S[i] == 'R':
       G_R += 1
   if S[i] == 'U':
       G_U -= 1
   if S[i] == 'D':
       G_D += 1
    
   if G_L < 1 or G_R > W or G_U < 1 or G_D > H:
       print('NO')
       flag = False
       break

   if T[i] == 'L':
       G_R -= 1
   if T[i] == 'R':
       G_L += 1
   if T[i] == 'U':
       G_D -= 1
   if T[i] == 'D':
       G_U += 1
    
if(flag == True):
    print('YES')

Submission status: Wrong Answer

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

U=current[0]
D=h-current[0]
L=current[1]
R=w-current[1]
win="NO"
for i in range(n):
  sU=s[0:i].count("U")
  sD=s[0:i].count("D")
  sL=s[0:i].count("L")
  sR=s[0:i].count("R")
  tU=t[0:i].count("U")
  tD=t[0:i].count("D")
  tL=t[0:i].count("L")
  tR=t[0:i].count("R")
  if (U-sU+tU)<=0:
    win="YES"
    break
  if (D-sD+tD)<=0:
    win="YES"
    break
  if (L-sL+tL)<=0:
    win="YES"
    break
  if (R-sR+tR)<=0:
    win="YES"
    break
print(win)

Submission status: Wrong Answer

Solution code:
H,W,N = map(int, input().split())
now = [int(i) for i in input().split()]
S = input()
T = input()
flag = True

for n in range(N):
    if S[n] == 'L':
        if W-now[1] >= now[1]-1:
            now[1] -= 1
    if S[n] == 'R':
        if W-now[1] <= now[1]-1:
            now[1] += 1 
    if S[n] == 'U':
        if H-now[0] >= now[0]-1:
            now[0] -= 1 
    if S[n] == 'D':
        if W-now[0] <= now[0]-1:
            now[0] += 1
    #print(now)

    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break
    
    if T[n] == 'L':
        if W-now[1] < now[1]-1:
            now[1] -= 1
    if T[n] == 'R':
        if W-now[1] > now[1]-1:
            now[1] += 1 
    if T[n] == 'U':
        if H-now[0] < now[0]-1:
            now[0] -= 1 
    if T[n] == 'D':
        if W-now[0] > now[0]-1:
            now[0] += 1
    #print(now)

    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break

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

Submission status: Wrong Answer

Solution code:
import sys
h,w,n = map(int,input().split())
x,y = 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] == 'U':
            down = max(down-1,1)
        elif t[i] == 'D':
            up = min(up+1,h)
        elif t[i] == 'R':
            left = max(left-1,1)
        else:
            right = min(right+1,w)
    if s[i] == 'U':
        up -= 1
    elif s[i] == 'D':
        down += 1
    elif s[i] == 'R':
        right -= 1
    else:
        left += 1
    
    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: Wrong Answer

Solution code:
H, W, N = map(int,input().split())
orig_y,orig_x = map(int,input().split())
orig_y -=1
orig_x -=1

s = input()
t = input()

y = orig_y
END = False
for i in range(N):
    if s[i]=='D':
        y+=1
    if not (0<=y<H):
        END=True
        break
    if t[i]=='U' and y!=0:
        y -=1
y = orig_y
for i in range(N):
    if s[i]=='D':
        y-=1
    if not (0<=y<W):
        END=True
        break
    if t[i]=='U' and y!=H-1:
        y +=1
        
x = orig_x
for i in range(N):
    if s[i]=='R':
        x+=1
    if not (0<=x<W):
        END=True
        break
    if t[i]=='L' and x!=0:
        x -=1
x = orig_x
for i in range(N):
    if s[i]=='L':
        x-=1
    if not (0<=x<W):
        END=True
        break
    if t[i]=='L' and x!=W-1:
        x +=1

if END:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
x,y=map(int,input().split())
s=input()+input()
p=["L","R","D","U"]
q=["R","L","U","D"]
for i in range(4):
    if i<2:
        c=y
        d=w
    else:
        c=x
        d=h
    for j in range(n):
        if s[j]==p[i]:
            if i%2==0:
                c-=1
                if c==0:
                    print("NO")
                    exit()
            else:
                c+=1
                if c>d:
                    print("NO")
                    exit()
        if s[j+n]==q[i]:
            if i%2==0:
                c=min(c+1,d)
            else:
                c=max(c-1,1)
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
H,W,N=input().split()
R,C=input().split()
S=input()
T=input()
h=int(H)
w=int(W)
n=int(N)
r=int(R)
c=int(C)

if S[:n].count('U')>r:
  a=0
elif S[:n].count('D')>h-r:
  a=0
elif S[:n].count('R')>w-c:
  a=0
elif S[:n].count('L')>c:
  a=0
else:
  a=1
    
if a==0:
  print('NO')
else:
  print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
x,y=map(int,input().split())
s=input()+input()
p=["L","R","D","U"]
q=["R","L","U","D"]
for i in range(4):
    if i<2:
        c=y
        d=w
    else:
        c=x
        d=h
    for j in range(n):
        if s[j]==p[i]:
            if i%2==0:
                c-=1
                if c==0:
                    print("NO")
                    exit()
            else:
                c+=1
                if c>d:
                    print("NO")
                    exit()
        if s[j+n]==q[i]:
            if i%2==0:
                c=min(c+1,d)
            else:
                c=max(c-1,0)
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

h,w,n = na()
sh,sw = na()
s = ns()
t = ns()

iit = [sw,sh,h-sh+1,w-sw+1]

print(iit)
#ludr
tshi = [0,0,0,0]
aoki = [0,0,0,0]

for i in range(n):
    if s[i] == 'L':
        tshi[0] += 1
    if s[i] == 'U':
        tshi[1] += 1
    if s[i] == 'D':
        tshi[2] += 1
    if s[i] == 'R':
        tshi[3] += 1
    print(tshi)
    print(aoki)
    for j in range(4):
        if tshi[j] >= iit[j]+aoki[3-j]:
            print('NO')
            exit()
    if t[i] == 'L':
        aoki[0] += 1
    if t[i] == 'U':
        aoki[1] += 1
    if t[i] == 'D':
        aoki[2] += 1
    if t[i] == 'R':
        aoki[3] += 1
    for j in range(4):
        if aoki[j] >= iit[j]+tshi[3-j]:
            aoki[j] -= 1

print('YES')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
C = {}
for dest in 'LRUD':
  C[dest] = [0, 0]
for i in range(N):
  if S[i] == 'L' and sc - C['L'][0] + C['R'][1] == 1:
    print('NO')
    break
  if S[i] == 'R' and sc + C['R'][0] - C['L'][1] == W:
    print('NO')
    break
  if S[i] == 'U' and sr - C['U'][0] + C['D'][1] == 1:
    print('NO')
    break
  if S[i] == 'D' and sr + C['D'][0] - C['U'][1] == H:
    print('NO')
    break
  C[S[i]][0] += 1
  if T[i] == 'L':
    C['L'][1] = min(C['L'][1] + 1, sc + C['R'][0] - 1)
  if T[i] == 'R':
    C['R'][1] = min(C['R'][1] + 1, W - sc - C['L'][0] - 1)
  if T[i] == 'U':
    C['U'][1] = min(C['U'][1] + 1, sr + C['D'][0] - 1)
  if T[i] == 'D':
    C['D'][1] = min(C['D'][1] + 1, H - sr - C['U'][0] - 1)
else:
  print('YES')

Submission status: Wrong Answer

Solution code:
H,W,N = map(int,input().split())
R,C = map(int,input().split())
S = input()
T = input()
sl,sr,su,sd = S.count('L'),S.count('R'),S.count('U'),S.count('D')
tl,tr,tu,td = T.count('L'),T.count('R'),T.count('U'),T.count('D')
u,l,d,r = R - 1,C - 1,H - R,W - C
for n in range(N):
        s,t = S[n],T[n]
        if s == 'U':
                u -= 1
                if u < 0:
                        print('N0')
                        exit()
        if t == 'D' and u < H - 1:
                u += 1
for n in range(N):
        s,t = S[n],T[n]
        if s == 'L':
                l -= 1
                if l < 0:
                        print('N0')
                        exit()
        if t == 'R' and l < W - 1:
                l += 1
for n in range(N):
        s,t = S[n],T[n]
        if s == 'R':
                r -= 1
                if r < 0:
                        print('N0')
                        exit()
        if t == 'L' and r < W - 1:
                r += 1
for n in range(N):
        s,t = S[n],T[n]
        if s == 'D':
                d -= 1
                if d < 0:
                        print('N0')
                        exit()
        if t == 'U' and d < H - 1:
                d += 1
print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n = map(int, input().split())
sx,sy = map(int, input().split())
s = input()
t = input()
sd = dict()
td = dict()
unit = "LRUD"
for x in unit:
  sd[x] = 0
  td[x] = 0
for i in range(n):
  sd[s[i]] += 1
  td[t[i]] += 1
  wb,hb = 0,0
  a1 = (sd["L"]+1,w-sd["R"])
  a2 = (sx-td["L"],sx+td["R"])
  wb = (max(a1[0],a2[0]) > min(a1[1],a2[1]))
  b1 = (sd["U"]+1,w-sd["D"])
  b2 = (sy-td["U"],sy+td["D"])
  hb = (max(b1[0],b2[0]) > min(b1[1],b2[1]))
  if wb and hb:
    print("YES")
    break
else:
  print("NO")

Submission status: Wrong Answer

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

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


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


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


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

if win:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W,N =map(int ,raw_input().split())
Sr,Sc =map(int ,raw_input().split())
S = raw_input()
T = raw_input()

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

if H % 2 == 0:
    Dr = float(H/2 -0.5)
else:
    Dr = (H-1)/2
if W % 2 == 0:
    Dc = float(W/2 -0.5)
else:
    Dc = (W-1)/2 
s = 1
for i in range(N):
    if (S[i] == 'D')and(Sr >=Dr):
        Sr = Sr+1
    elif (S[i] == 'U')and(Sr <=Dr):
        Sr = Sr-1
    elif (S[i] == 'R')and(Sc >=Dc):
        Sc = Sc+1
    elif (S[i] == 'L')and(Sc <=Dc):
        Sc = Sc-1
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
    if (T[i] == 'D')and(Sr <Dr):
        Sr = Sr+1
    elif (T[i] == 'U')and(Sr >Dr):
        Sr = Sr-1
    elif (T[i] == 'R')and(Sc <Dc):
        Sc = Sc+1
    elif (T[i] == 'L')and(Sc >Dc):
        Sc = Sc-1
    
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
if s ==0:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
def solve(start, newS, newT, newH):
    # print(start, newS, newT, newH)
    s_max = start
    s_min = start
    t_plus = 0
    t_minus = 0
    t_force = 0
    for i, s in enumerate(newS):
        # print("s", s, 't', t_force, t_plus, t_minus)
        if s == 1:
            s_max += 1
            if s_max - t_force >= newH:
                if t_minus <= 0:
                    return False
                # t_force +=1
                s_max -= 1
                t_minus -= 1
        elif s == -1:
            s_min -= 1
            if s_min - t_force < 0:
                if t_plus <= 0:
                    return False
                # t_force -= 1
                s_min += 1
                t_plus -= 1
        if newT[i] == 1:
            t_plus += 1
        elif newT[i] == -1:
            t_minus += 1
    return True

#
# assert solve(1, [0, 0, 0, 1, 1, -1, -1], [1, -1, 0, 0, 0, 0, 0, 0, 0], 3) == False
# assert solve(1, [0, 0, 0], [0, 1, -1], 2)
# assert solve(1, [1, 1, -1], [-1, 0, 0], 3)

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

sr, sc = list(map(int, input().split()))
sr -= 1
sc -= 1
S = input()
T = input()

S1 = [1 if s == 'U' else -1 if s == 'D' else 0 for s in S]
T1 = [1 if s == 'U' else -1 if s == 'D' else 0 for s in T]
S2 = [1 if s == 'R' else -1 if s == 'L' else 0 for s in S]
T2 = [1 if s == 'R' else -1 if s == 'L' else 0 for s in T]

# print(solve(sr, S1, T1, H))
# print(solve(sc, S2, T2, W))
if solve(sr, S1, T1, H) and solve(sc, S2, T2, W):
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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':
    u += 1
  if T[i] == 'D':
    d += 1
  if T[i] == 'L':
    l += 1
  if T[i] == 'R':
    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 + 1 and dam_D < H - Sr + 1 and dam_L < Sc + 1 and dam_R < W - Sc + 1:
  print('NO')
else:
  print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n = map(int, input().split())
sx,sy = map(int, input().split())
s = input()
t = input()
sd = dict()
td = dict()
unit = "LRUD"
for x in unit:
  sd[x] = 0
  td[x] = 0
for i in range(n):
  sd[s[i]] += 1
  td[t[i]] += 1
  wb,hb = 0,0
  a1 = (sd["L"]+1,w-sd["R"])
  a2 = (sx-td["L"],sx+td["R"])
  if a1[0] > a1[1]:
    continue
  elif a2[0]>a2[1]:
    wb = 1
  elif a1[0]>a2[1] or a1[1]<a2[0]:
    continue
  else:
    wb = 1
  b1 = (sd["U"]+1,w-sd["D"])
  b2 = (sy-td["U"],sy+td["D"])
  if b1[0] > b1[1]:
    continue
  elif b2[0]>b2[1]:
    print("YES")
    break
  elif b1[0]>b2[1] or b1[1]<b2[0]:
    continue
  else:
    print("YES")
    break 
else:
  print("NO")