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

if sc == 0 and S[0] == "L":
    print("NO")
if sc == W-1 and S[0] == "R":
    print("NO")
if sr == 0 and S[0] == "U":
    print("NO")
if sr == H-1 and S[0] == "D":
    print("NO")

Slr, Sud = 0, 0
Tlr, Tud = 0, 0
for n in range(N):
    if S[n] == "L":
        Slr += -1
    elif S[n] == "R":
        Slr += 1
    elif S[n] == "U":
        Sud += -1
    elif S[n] == "D":
        Sud += 1

    if T[n] == "L":
        Tlr += -1
    elif T[n] == "R":
        Tlr += 1
    elif T[n] == "U":
        Tud += -1
    elif T[n] == "D":
        Tud += 1

    if Slr + Tlr < 1-sc or Slr + Tlr > W-sc:
        print("NO")
        exit()
    if Sud + Tud < 1-sr or Sud + Tud > H-sr:
        print("NO")
        exit()

print("YES")

Submission status: Wrong Answer

Solution code:
import sys

H, W, N = [int(x) for x in input().split(' ')]
x = tuple([int(x) for x in input().split(' ')])
#S = [x for x in input()]
#T = [x for x in input()]
S = input()
T = input()

class Takahashi:
  def __init__(self, S, H, W):
    self.s = S
    self.up = S.count('R')
    self.down = S.count('D')
    self.right = S.count('R')
    self.left = S.count('L')
    
    self.H = H
    self.W = W
    
  def __call__(self, i, s, aoki):
    if self.s[i] == 'U':
      self.up -= 1
      if ((self.up - aoki.down - s[0]) - 1) > ((s[0] + self.down - aoki.up) - H):
        return (s[0] - 1, s[1])
      
    elif self.s[i] == 'D':
      self.down -= 1
      if ((self.up - aoki.down - s[0]) - 1) < ((s[0] + self.down - aoki.up) - H):
        return (s[0] - 1, s[1])
      
    elif self.s[i] == 'R':
      self.right -= 1
      if ((self.left - aoki.right - s[1]) - 1) < ((s[1] + self.right - aoki.left) - W):
        return (s[0], s[1] + 1)

    elif self.s[i] == 'L':
      self.left -= 1
      if ((self.left - aoki.right - s[1]) - 1) > ((s[1] + self.right - aoki.left) - W):
        return (s[0], s[1] - 1)
      
    return s
      
class Aoki:
  def __init__(self, T, H, W):
    self.t = T
    self.up = T.count('R')
    self.down = T.count('D')
    self.right = T.count('R')
    self.left = T.count('L')
    
    self.H = H
    self.W = W
    
  def __call__(self, i, s, takahashi):
    if self.t[i] == 'U':
      self.up -= 1
      if ((takahashi.up - self.down - s[0]) - 1) < ((s[0] + takahashi.down - self.up) - H):
        return (s[0] - 1, s[1])
      
    elif self.t[i] == 'D':
      self.down -= 1
      if ((takahashi.up - self.down - s[0]) - 1) > ((s[0] + takahashi.down - self.up) - H):
        return (s[0] - 1, s[1])
      
    elif self.t[i] == 'R':
      self.right -= 1
      if ((takahashi.left - self.right - s[1]) - 1) > ((s[1] + takahashi.right - self.left) - W):
        return (s[0], s[1] + 1)

    elif self.t[i] == 'L':
      self.left -= 1
      if ((takahashi.left - self.right - s[1]) - 1) < ((s[1] + takahashi.right - self.left) - W):
        return (s[0], s[1] - 1)
      
    return s

takahashi = Takahashi(S, H ,W)
aoki = Aoki(T, H, W)

for i in range(N):
  x = takahashi(i, x, aoki)
  if (x[0] < 1) or (x[0] > H) or (x[1] < 1) or (x[1] > W):
    print('NO')
    sys.exit()
  x = aoki(i, x, takahashi)
  
print('YES')

Submission status: Wrong Answer

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

lef1 = 0
rig1 = 0
up1 = 0
do1 = 0

def mind(ssr,ssc):
    min()
a=0
for i in range(N):
    if(S[i] =="L"):
        lef1 +=1
    if(T[i] =="R"):
        if(lef1-sr>0):
            lef1 -=1
    if(S[i] =="R"):
        rig1 +=1
    if(T[i] =="L"):
        if(rig1-(W-sr+1)>0):
            rig1 -=1
    if(S[i] =="U"):
        up1 +=1
    if(T[i] =="D"):
        if(up1-sc>0):
            up1 -=1
    if(S[i] =="D"):
        do1 +=1
    if(T[i] =="D"):
        if(do1-(H-sc+1)>0):        
            do1 -=1
    if((lef1>=sr)or(up1>=sc)or(rig1>=W-sr+1)or(do1>=H-sc+1)):
        print("NO")
        a=1
        break
if(a==0):
    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()

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


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

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

print(ans)

Submission status: Wrong Answer

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

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

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

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

  # print(lu,ld,ll,lr)

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

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

  # print(lu,ld,ll,lr)

print('YES')

Submission status: 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]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]<=pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]<=pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1<=w-pos[1]+1:
        pos[1] -= 1
    return pos
def ao(k,pos):
    if k=='U' and pos[0]-1>=h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]>=pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]>=pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1>=w-pos[1]+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())
R,C = map(int,input().split())
S = input()
T = input()
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
        if s == 'L':
                l -= 1
                if l < 0:
                        print('N0')
                        exit()
        if t == 'R' and l < W - 1:
                l += 1
        if s == 'R':
                r -= 1
                if r < 0:
                        print('N0')
                        exit()
        if t == 'L' and r < W - 1:
                r += 1
        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())
sr, sc = map(int, input().split())
S = [i for i in input()]
T = [i for i in input()]

lef1 = 0
rig1 = 0
up1 = 0
do1 = 0

def mind(ssr,ssc):
    min()
a=0
for i in range(N):
    if(S[i] =="L"):
        lef1 +=1
    if(T[i] =="R"):
        if(lef1>0):
            lef1 -=1
    if(S[i] =="R"):
        rig1 +=1
    if(T[i] =="L"):
        if(rig1>0):
            rig1 -=1
    if(S[i] =="U"):
        up1 +=1
    if(T[i] =="D"):
        if(up1>0):
            up1 -=1
    if(S[i] =="D"):
        do1 +=1
    if(T[i] =="D"):
        if(do1>0):        
            do1 -=1
    if((lef1>=sr)or(up1>=sc)or(rig1>=W-sr+1)or(do1>=H-sc+1)):
        print("NO")
        a=1
        break
if(a==0):
    print("YES")

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate

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

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

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

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

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

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

        i += 1

    return 'YES' 


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

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]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]<pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]<pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1<w-pos[1]+1:
        pos[1] -= 1
    return pos
def ao(k,pos):
    if k=='U' and pos[0]-1>h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]>pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]>pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1>w-pos[1]+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()

Slr, Sud = 0, 0
Tlr, Tud = 0, 0
for n in range(N):
    if S[n] == "L":
        Slr += -1
    elif S[n] == "R":
        Slr += 1
    elif S[n] == "U":
        Sud += -1
    elif S[n] == "D":
        Sud += 1

    if T[n] == "L":
        Tlr += -1
    elif T[n] == "R":
        Tlr += 1
    elif T[n] == "U":
        Tud += -1
    elif T[n] == "D":
        Tud += 1

    if Slr + Tlr < 1-sc or Slr + Tlr > W-sc:
        print("NO")
        exit()
    if Sud + Tud < 1-sr or Sud + Tud > H-sr:
        print("NO")
        exit()

print("YES")

Submission status: Wrong Answer

Solution code:
import random
if random.random()>0.5:
    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()

Slr, Sud = 0, 0
Tlr, Tud = 0, 0
for n in range(N):
    if S[n] == "L":
        Slr += -1
    elif S[n] == "R":
        Slr += 1
    elif S[n] == "U":
        Sud += -1
    elif S[n] == "D":
        Sud += 1

    if T[n] == "L":
        Tlr += -1
    elif T[n] == "R":
        Tlr += 1
    elif T[n] == "U":
        Tud += -1
    elif T[n] == "D":
        Tud += 1

    if Slr + Tlr < 1-sc or Slr + Tlr > W-sc:
        print("No")
        exit()
    if Sud + Tud < 1-sr or Sud + Tud > H-sr:
        print("No")
        exit()

print("Yes")

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


def move(strt, sr, sc):
    if strt == 'L':
        return sr, sc - 1
    if strt == 'R':
        return sr, sc + 1
    if strt == 'U':
        return sr - 1, sc
    if strt == 'D':
        return sr + 1, sc



fall_lut = [[[] for i in range(w)] for j in range(h)]
for y in range(h):
    for x in range(w):
        if x <= (w - 1) // 2:
            fall_lut[y][x] += 'L'
        if x >= w // 2:
            fall_lut[y][x] += 'R'
        if y <= (h - 1) // 2:
            fall_lut[y][x] += 'U'
        if y >= h // 2:
            fall_lut[y][x] += 'D'

pass



# def fall_dir(sr, sc):
#     dir_ = []
#     if h % 2 == 0:
#         dir
#     dir_ = ['L' if sc <= w // 2 else 'R', 'U' if sr <= h // 2 else 'D']


for iturn in range(n):
    # S's turn
    s_strt = s[iturn]

    if s_strt in fall_lut[sr-1][sc-1]:
        sr, sc = move(s_strt, sr, sc)
    else:
        # stay
        pass

    # check
    if not ((sr - 1) in range(h) and (sc - 1) in range(w)):
        break

    # T's turn
    t_strt = t[iturn]

    if t_strt in fall_lut[sr-1][sc-1]:
        # stay
        pass
    else:
        sr, sc = move(t_strt, sr, sc)

    # check
    if not ((sr - 1) in range(h) and (sc - 1) in range(w)):
        break



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

Submission status: Wrong Answer

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

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

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

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

  # print(lu,ld,ll,lr)

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

  if y=='U':
    ld+=1
  elif y=='D':
    lu+=1
  elif y=='L':
    lr+=1
  elif y=='R':
    ll+=1

  # print(lu,ld,ll,lr)

print('YES')

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())
y,x = map(int,input().split())
s = list(input())
t = list(input())

up = h
down = 1 
right = w
left = 1
for i in range(n-1,-1,-1):
    if i != n-1:
        if t[i] == '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())
sr, sc = map(int, input().split())
S = input()
T = input()
R = [0] * N
if S[-1] == 'R':
    R[-1] = W - 1
else:
    R[-1] = W
for i in range(N - 2, -1, -1):
    if S[i] == 'R':
        if T[i] == 'L':
            R[i] = min(R[i + 1], W - 1)
        else:
            R[i] = max(0, R[i + 1] - 1)
    else:
        if T[i] == 'L':
            R[i] = min(W, R[i + 1] + 1)
        else:
            R[i] = R[i + 1]
L = [0] * N
if S[-1] == 'L':
    L[-1] = 2
else:
    L[-1] = 1
for i in range(N - 2, -1, -1):
    if S[i] == 'L':
        if T[i] == 'R':
            L[i] = max(L[i + 1], 2)
        else:
            L[i] = min(W + 1, L[i + 1] + 1)
    else:
        if T[i] == 'R':
            L[i] = max(1, L[i + 1] - 1)
        else:
            L[i] = L[i + 1]
D = [0] * N
if S[-1] == 'D':
    D[-1] = H - 1
else:
    D[-1] = H
for i in range(N - 2, -1, -1):
    if D[i] == 'D':
        if T[i] == 'U':
            D[i] = min(D[i + 1], H - 1)
        else:
            D[i] = max(0, D[i + 1] - 1)
    else:
        if T[i] == 'U':
            D[i] = min(H, D[i + 1] + 1)
        else:
            D[i] = D[i + 1]
U = [0] * N
if S[-1] == 'U':
    U[-1] = 2
else:
    U[-1] = 1
for i in range(N - 2, -1, -1):
    if S[i] == 'U':
        if T[i] == 'D':
            U[i] = max(U[i + 1], 2)
        else:
            U[i] = min(H + 1, U[i + 1] + 1)
    else:
        if T[i] == 'D':
            U[i] = max(1, U[i + 1] - 1)
        else:
            U[i] = U[i + 1]
ans = "YES"
if sc < L[0] or sc > R[0] or sr < U[0] or sc > D[0]:
    ans = "NO"
for i in range(N):
    if R[i] < L[i] or D[i] < U[i]:
        ans = "NO"
print(ans)

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

Submission status: 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()

L_Best=0
L_Now=0
R_Best=0
R_Now=0
U_Best=0
U_Now=0
D_Best=0
D_Now=0

for t,a in zip(S,T):
    if t=="L":
        L_Best=max(L_Now+1,L_Best)
        L_Now+=1
    if a=="R":
        L_Now=min(L_Now-1,sr-1)

    if t=="R":
        R_Best=max(R_Now+1,R_Best)
        R_Now+=1
    if a=="L":
        R_Now=min(R_Now-1,H-sr)

    if t=="U":
        U_Best=max(U_Now+1,U_Best)
        U_Now+=1
    if a=="D":
        U_Now=min(U_Now-1,sc)

    if t=="D":
        D_Best=max(D_Now+1,D_Best)
        D_Now+=1
    if a=="U":
        D_Now=min(D_Now-1,W-sc)

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

Submission status: 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()

L_Best=0
L_Now=0
R_Best=0
R_Now=0
U_Best=0
U_Now=0
D_Best=0
D_Now=0

for t,a in zip(S,T):
    if t=="L":
        L_Best=max(L_Now+1,L_Best)
        L_Now+=1
    if a=="R":
        L_Now-=1

    if t=="R":
        R_Best=max(R_Now+1,R_Best)
        R_Now+=1
    if a=="L":
        R_Now-=1

    if t=="U":
        U_Best=max(U_Now+1,U_Best)
        U_Now+=1
    if a=="D":
        U_Now-=1

    if t=="D":
        D_Best=max(D_Now+1,D_Best)
        D_Now+=1
    if a=="U":
        D_Now-=1

if sr<=L_Best:
    print("YES")
elif H-sr+1<=R_Best:
    print("YES")
elif sc<=U_Best:
    print("YES")
elif W-sc+1<=D_Best:
    print("YES")
else:
    print("NO")

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

    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

    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())
R,C = map(int,input().split())
S = input()
T = input()
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())
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

    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

    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:
# coding: utf-8
LRUD = ['L', 'R', 'U', 'D']
h, w, n = list(map(int, input().split()))
r, c = list(map(int, input().split()))
r -= 1
c -= 1
s = list(map(LRUD.index, input()))
t = list(map(LRUD.index, input()))

def move(command):
    global r, c
    #_r = r
    #_c = c
    result = True
    if command == 0:
        if c > 0:
            c -= 1
        else:
            result = False
    elif command == 1:
        if c < w - 1:
            c += 1
        else:
            result = False
    elif command == 2:
        if r > 0:
            r -= 1
        else:
            result = False
    elif command == 3:
        if r < h - 1:
            r += 1
        else:
            result = False
    #print("{}:({},{})->({},{})".format(LRUD[command], _r, _c, r, c))
    return result

result = True
i = 0
while result and i < n:
    i += 1
    result = move(s.pop(0))
    if result:
        result = move(t.pop(0))

if result:
    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+0.5,now_h+0.5,center_w,center_h) < EuclidDis(next_w+0.5,next_h+0.5,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+0.5,now_h+0.5,center_w,center_h) > EuclidDis(next_w+0.5,next_h+0.5,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')