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:
H,W,N = map(int, input().split())
sr,sc = map(int, input().split())
S = input()
T = input()

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

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

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

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


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

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

    if si == "U":
        up -= 1
    elif si == "D":
        down += 1

    if up - down == 1:
        print("NO")
        exit()

if sr <= down or up <= sc:
    print("NO")
    exit()

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

left, right = 0, W+1
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for si,ti in zip(S[:-1][::-1],T[:-1][::-1]):
    # print(si,ti)
    # print(left, right)
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(0, left)
    right = min(W+1, right)

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

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

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


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

for si,ti in zip(S[:-1][::-1],T[:-1][::-1]):
    if ti == "U":
        down -= 1
    elif ti == "D":
        up += 1
    down = max(0, down)
    up = min(H+1, up)

    if si == "U":
        up -= 1
    elif si == "D":
        down += 1

    if up - down == 1:
        print("NO")
        exit()

if not (down < sr < up):
    print("NO")
    exit()

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

left, right = 1, W
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for si,ti in zip(S[:-1][::-1],T[:-1][::-1]):
    # print(si,ti)
    # print(left, right)
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(1, left)
    right = min(W, right)

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

    if left > right:
        print("NO")
        exit()

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


down, up = 1, H
if S[-1] == "U":
    up -= 1
elif S[-1] == "D":
    down += 1

for si,ti in zip(S[:-1][::-1],T[:-1][::-1]):
    if ti == "U":
        down -= 1
    elif ti == "D":
        up += 1
    down = max(1, down)
    up = min(H, up)

    if si == "U":
        up -= 1
    elif si == "D":
        down += 1

    if down > up:
        print("NO")
        exit()

if not (down <= sr <= up):
    print("NO")
    exit()

print("YES")

Submission status: Wrong Answer

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


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return False
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 1:
                    now[dim] += step
    return True
        
Dim = ['U','D','R','L']
for d in Dim:
    if simu(d):
        print('YES')
        exit()
print('NO')

Submission status: Wrong Answer

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


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return False
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 0:
                    now[dim] += step
    return True
        
Dim = ['U','D','R','L']
for d in Dim:
    if simu(d):
        print('YES')
        exit()
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()

left, right = 1, W
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for si,ti in zip(S[::-2],T[::-2]):
    # print(si,ti)
    # print(left, right)
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(1, left)
    right = min(W, right)

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

    if left > right:
        print("NO")
        exit()

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


down, up = 1, H
if S[-1] == "U":
    up -= 1
elif S[-1] == "D":
    down += 1
    
for si,ti in zip(S[::-2],T[::-2]):
    if ti == "U":
        down -= 1
    elif ti == "D":
        up += 1
    down = max(1, down)
    up = min(H, up)

    if si == "U":
        up -= 1
    elif si == "D":
        down += 1

    if down > up:
        print("NO")
        exit()

if not (down <= sr <= up):
    print("NO")
    exit()

print("YES")

Submission status: Wrong Answer

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


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 0:
                    now[dim] += step
    return False
        
Dim = ['U','D','R','L']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')

Submission status: Wrong Answer

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


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 0:
                    now[dim] += step
        
D = ['U','D','R','L']
for d in D:
    if simu(d):
        print('YES')
        exit()
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()

left, right = 1, W
for si,ti in zip(S[::-1],T[::-1]):
    # print(si,ti)
    # print(left, right)
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(1, left)
    right = min(W, right)

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

    if left > right:
        print("NO")
        exit()

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


down, up = 1, H
for si,ti in zip(S[::-1],T[::-1]):
    if ti == "U":
        down -= 1
    elif ti == "D":
        up += 1
    down = max(1, down)
    up = min(H, up)

    if si == "U":
        up -= 1
    elif si == "D":
        down += 1

    if down > up:
        print("NO")
        exit()

if not (down <= sr <= up):
    print("NO")
    exit()

print("YES")

Submission status: Wrong Answer

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


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,-1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,1,0
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,1,1
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == -1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 0:
                    now[dim] += step
        
D = ['U','D','R','L']
for d in D:
    if simu(d):
        print('YES')
        exit()
print('NO')

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
H, W, N = tuple(map(int, input().split()))
r, c = tuple(map(int, input().split()))
Ta = input()
Ao = input()


def convert(ch):
    if ch == "R":
        return (0, 1)
    elif ch == "L":
        return (0, -1)
    elif ch == "U":
        return (1, 0)
    else:
        return (-1, 0)


# 青木くんのターンになったらやばいマップを逆方向に1動かして重ねた部分がやばくなる
Hmap, Wmap = np.zeros(H+2), np.zeros(W+2)
Hmap[1:-1], Wmap[1:-1] = np.ones(H), np.ones(W)

for idx, (t, a) in enumerate(zip(reversed(Ta), reversed(Ao))):
    if idx != 0:

        dy, dx = convert(a)
        # 動かした方向にやばくない場所があれば復帰する
        # つまり フィルタをかけてしまえばいいので，元の場所が0かつ動かした先が1であればよい，その場所を1にする
        Hmap[1:-1] += (1-Hmap)[1:-1] * Hmap[1+dy:H+1+dy]
        Wmap[1:-1] += (1-Wmap)[1:-1] * Wmap[1+dx:W+1+dx]
        # print("A",a,yabaiMap)
    dy, dx = convert(t)
    Hmap[1:-1] *= Hmap[1+dy:H+1+dy]
    Wmap[1:-1] *= Wmap[1+dx:W+1+dx]

    # print("T",t,yabaiMap)
print("YES" if Hmap[r]*Wmap[c] == 1 else "NO")

Submission status: Wrong Answer

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

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

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

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


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




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


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

Submission status: Wrong Answer

Solution code:
H,W,N=map(int,input().split())
sy,sx=map(int,input().split())
S=input()
T=input()
inf,sup=1,H
for i in range(N)[::-1]:
  if S[i]=="U":
    if T[i]=="U":
      sup-=1
      inf-=1
    elif T[i]=="D":
      sup=min(sup,H-1)
    else:
      sup-=1
  elif S[i]=="D":
    if T[i]=="D":
      sup+=1
      inf+=1
    elif T[i]=="U":
      inf=max(inf,2)
    else:
      inf+=1
  else:
    if T[i]=="U":
      #if inf<H:
        inf-=1
    elif T[i]=="D":
      #if sup>1:
        sup+=1
  inf=max(inf,1)
  sup=min(sup,H)
  if inf>sup:
    print("NO")
    exit()
  #print("H",inf,sup)
if not inf<=sy<=sup:
  print("NO")
  exit()
  
inf,sup=1,W
for i in range(N)[::-1]:
  if S[i]=="R":
    if T[i]=="R":
      sup-=1
      inf-=1
    elif T[i]=="L":
      sup=min(sup,W-1)
    else:
      sup-=1
  elif S[i]=="L":
    if T[i]=="L":
      sup+=1
      inf+=1
    elif T[i]=="R":
      inf=max(inf,2)
    else:
      inf+=1
  else:
    if T[i]=="R":
      #if inf<W:
        inf-=1
    elif T[i]=="L":
      #if sup>1:
        sup+=1
  inf=max(inf,1)
  sup=min(sup,W)
  if inf>sup:
    print("NO")
    exit()
  #print("W",inf,sup)
if not inf<=sx<=sup:
  print("NO")
  exit()
print("YES")

Submission status: Wrong Answer

Solution code:
H,W,N=map(int,input().split())
sy,sx=map(int,input().split())
S=input()
T=input()
inf,sup=1,H
for i in range(N)[::-1]:
  if S[i]=="U":
    if T[i]=="U":
      sup-=1
      inf-=1
    elif T[i]=="D":
      sup=min(sup,H-1)
    else:
      sup-=1
  elif S[i]=="D":
    if T[i]=="D":
      sup+=1
      inf+=1
    elif T[i]=="U":
      inf=max(inf,2)
    else:
      inf+=1
  else:
    if T[i]=="U":
      if inf<H:
        inf-=1
    elif T[i]=="D":
      if sup>1:
        sup+=1
  inf=max(inf,1)
  sup=min(sup,H)
  if inf>sup:
    print("NO")
    exit()
  #print("H",inf,sup)
if not inf<=sy<=sup:
  print("NO")
  exit()
  
inf,sup=1,W
for i in range(N)[::-1]:
  if S[i]=="R":
    if T[i]=="R":
      sup-=1
      inf-=1
    elif T[i]=="L":
      sup=min(sup,W-1)
    else:
      sup-=1
  elif S[i]=="L":
    if T[i]=="L":
      sup+=1
      inf+=1
    elif T[i]=="R":
      inf=max(inf,2)
    else:
      inf+=1
  else:
    if T[i]=="R":
      if inf<W:
        inf-=1
    elif T[i]=="L":
      if sup>1:
        sup+=1
  inf=max(inf,1)
  sup=min(sup,W)
  if inf>sup:
    print("NO")
    exit()
  #print("W",inf,sup)
if not inf<=sx<=sup:
  print("NO")
  exit()
print("YES")

Submission status: Wrong Answer

Solution code:
H,W,N=map(int,input().split())
sy,sx=map(int,input().split())
S=input()
T=input()
inf,sup=1,H
for i in range(N)[::-1]:
  if S[i]=="U":
    if T[i]=="U":
      sup-=1
      inf-=1
    elif T[i]=="D":
      sup=min(sup,H-1)
    else:
      sup-=1
  elif S[i]=="D":
    if T[i]=="D":
      sup+=1
      inf+=1
    elif T[i]=="U":
      inf=max(inf,2)
    else:
      inf+=1
  else:
    if T[i]=="U":
      inf-=1
    elif T[i]=="D":
      sup+=1
  inf=max(inf,1)
  sup=min(sup,W)
if not inf<=sy<=sup:
  print("NO")
  exit()
  
inf,sup=1,W
for i in range(N)[::-1]:
  if S[i]=="R":
    if T[i]=="R":
      sup-=1
      inf-=1
    elif T[i]=="L":
      sup=min(sup,W-1)
    else:
      sup-=1
  elif S[i]=="L":
    if T[i]=="L":
      sup+=1
      inf+=1
    elif T[i]=="R":
      inf=max(inf,2)
    else:
      inf+=1
  else:
    if T[i]=="R":
      inf-=1
    elif T[i]=="L":
      sup+=1
  inf=max(inf,1)
  sup=min(sup,W)
  #print(inf,sup)
if not inf<=sx<=sup:
  print("NO")
  exit()
print("YES")

Submission status: Wrong Answer

Solution code:
H,W,N=map(int,input().split())
sy,sx=map(int,input().split())
S=input()
T=input()
inf,sup=1,H
for i in range(N)[::-1]:
  if S[i]=="U":
    if T[i]=="U":
      sup-=1
      inf-=1
    elif T[i]=="D":
      sup=min(sup,H-1)
    else:
      sup-=1
  elif S[i]=="D":
    if T[i]=="D":
      sup+=1
      inf+=1
    elif T[i]=="U":
      inf=max(inf,2)
    else:
      inf+=1
if not inf<=sy<=sup:
  print("NO")
  exit()
  
inf,sup=1,W
for i in range(N)[::-1]:
  if S[i]=="R":
    if T[i]=="R":
      sup-=1
      inf-=1
    elif T[i]=="L":
      sup=min(sup,W-1)
    else:
      sup-=1
  elif S[i]=="L":
    if T[i]=="L":
      sup+=1
      inf+=1
    elif T[i]=="R":
      inf=max(inf,2)
    else:
      inf+=1
if not inf<=sx<=sup:
  print("NO")
  exit()
print("YES")

Submission status: Wrong Answer

Solution code:
import numpy as np

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

S = input()
T = input()

piece_remaining = True

low_min = 0
high_max = H - 1

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

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


low_min = 0
high_max = W - 1

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

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

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

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x)-1, input().split())
stS = [v for v in input()[:-1]]
stT = [v for v in input()[:-1]]
stS = stS[::-1]
stT = stT[::-1]

u_lim = 0
d_lim = H-1

for s, t in zip(stS, stT):
    if t == "U":
        u_lim -= 1
    elif t == "D":
        d_lim += 1
    if s == "U":
        u_lim += 1
    elif t == "D":
        d_lim -= 1

l_lim = 0
r_lim = W-1

for s, t in zip(stS, stT):
    if t == "R":
        l_lim -= 1
    elif t == "L":
        r_lim += 1
    if s == "R":
        l_lim += 1
    elif t == "L":
        r_lim -= 1

if u_lim <= sr <= d_lim and l_lim <= sr <= r_lim:
    ans = "YES"
else:
    ans = "NO"
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
stS = [v for v in input()[:-1]]
stT = [v for v in input()[:-1]]
stS = stS[::-1]
stT = stT[::-1]

u_lim = 0
d_lim = H

for s, t in zip(stS, stT):
    if t == "U":
        u_lim -= 1
    elif t == "D":
        d_lim += 1
    if s == "U":
        u_lim += 1
    elif t == "D":
        d_lim -= 1

l_lim = 0
r_lim = W

for s, t in zip(stS, stT):
    if t == "R":
        l_lim -= 1
    elif t == "L":
        r_lim += 1
    if s == "R":
        l_lim += 1
    elif t == "L":
        r_lim -= 1

if u_lim <= sr <= d_lim and l_lim <= sr <= r_lim:
    ans = "YES"
else:
    ans = "NO"
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
stS = [v for v in input()[:-1]]
stT = [v for v in input()[:-1]]
stS = stS[::-1]
stT = stT[::-1]

u_lim = 0
d_lim = H

for s, t in zip(stS, stT):
    if t == "U":
        u_lim -= 1
    elif t == "D":
        d_lim += 1
    if s == "U":
        u_lim += 1
    elif t == "D":
        d_lim -= 1

l_lim = 0
r_lim = W

for s, t in zip(stS, stT):
    if t == "R":
        l_lim -= 1
    elif t == "L":
        r_lim += 1
    if s == "R":
        l_lim += 1
    elif t == "L":
        r_lim -= 1

if u_lim <= sr <= d_lim and l_lim <= sr <= r_lim:
    ans = "Yes"
else:
    ans = "No"
print(ans)

Submission status: Wrong Answer

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


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

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

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

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

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

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[::-1]
T = input()[::-1]
o = [0, 0, W, H]
for t, s in zip(T, S):
    if t == 'L':
        o[0] -= 1
        o[2] += 1
    elif t == 'U':
        o[3] += 1
    elif t == 'R':
        o[2] += 1
    elif t == 'D':
        o[1] -= 1
        o[3] += 1
    for i in range(2):
        if 0 > o[i]:
            o[i] = 0
        elif o[i] >= [W, H][i]:
            o[i] = [W, H][i]
    for i in range(2, 4):
        if 0 > o[i]:
            o[i] = 0
        elif o[i] + o[i - 2] > [W, H][i - 2]:
            o[i] = [W,  H][i - 2] - o[i - 2]
    if s == 'L':
        o[0] += 1
        o[2] -= 1
    elif s == 'U':
        o[3] -= 1
    elif s == 'R':
        o[2] -= 1
    elif s == 'D':
        o[1] += 1
        o[3] -= 1
    if o[2] == 0 or o[3] == 0:
        print('NO')
        exit()
if o[0] < sc <= o[0] + o[2] or o[1] < sr <= o[1] + o[3]:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

sys.setrecursionlimit(100000)


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


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


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


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


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


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


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


def mt(f):
    import time

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

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

    return wrap


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

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

        if left > right:
            return 'NO'
        elif bottom > top:
            return 'NO'

    s = s_
    if not (left <= s[1]-1 <= right):
        return 'NO'
    elif not (bottom <= s[0]-1 <= top):
        return 'NO'
    return 'YES'


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

sys.setrecursionlimit(100000)


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


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


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


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


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


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


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


def mt(f):
    import time

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

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

    return wrap


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

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

        if left > right:
            return 'NO'
        elif bottom > top:
            return 'NO'

    s = s_
    if not (left <= s[1]-1 <= right):
        return 'NO'
    elif not (bottom <= s[0]-1 <= top):
        return 'NO'
    return 'YES'


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


if __name__ == '__main__':
    main()