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:
def main(n, s, S, T):
    H, W, N= n.split()
    sr, sc = s.split()
    for i in range(int(N)):
        sr, sc = move(sr, sc, S[i]).split()
        sr, sc = move(sr, sc, T[i]).split()
        # Check is Win
        if(sr > H or sc > W):
            return "NO"
        
    return "YES"

def move(H, W, action):
    if(action == "L"):
        w = int(W)-1
        return H+" " + str(w)
    elif(action == "R"):
        w = int(W)+1
        return H+" " + str(w)
    elif(action == "U"):
        h = int(H)-1
        return str(h)+" " + W
    else:
        h = int(H)+1
        return str(h)+" " + W

if __name__ == '__main__':
	print(main(input(), input(), input(), input()))
    #assert(main("2 3 3", "2 2", "DRL", "LUD")=="YES")
    #assert(main("2 3 3", "2 2", "RRL", "LUD")=="YES")
    #assert(main("4 3 5", "2 2", "UDRRR", "LLDUD")=="NO")
    #assert(main("5 6 11", "2 1", "RLDRRUDDLRL", "URRDRLLDLRD")=="NO")

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
h,w,n = map(int,input().split(' '))
h-=1
w-=1
sr,sc = map(int,input().split(' '))
sr-=1
sc-=1
S = list(input())
T = list(input())

for i in range(len(S)):
    if S[i] == 'L':
        S[i]=[0,-1]
    elif S[i] == 'R':
        S[i]=[0,1]
    elif S[i] == 'U':
        S[i]=[-1,0]
    else:
        S[i]=[1,0]
        
for i in range(len(T)):
    if T[i] == 'L':
        T[i]=[0,-1]
    elif T[i] == 'R':
        T[i]=[0,1]
    elif T[i] == 'U':
        T[i]=[-1,0]
    else:
        T[i]=[1,0]

# print(S)
# print(T)
flag=True
for i in range(n):
    # print(sr,sc,h,w)
    if 0<=sr+S[i][0]<=h and 0<=sc+S[i][1]<=w:
        sr+=S[i][0]
        sc+=S[i][1]        
        if 0<=sr+T[i][0]<=h and 0<=sc+T[i][1]<=w:
            sr+=T[i][0]
            sc+=T[i][1]
        else:
            print("NO")
            flag=False
            break
    else:
        print("NO")
        flag=False
        break
if flag is True:
    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()

NO_FLAG = 0

for i in range(n):
    #高橋のターン　駒を出したい（NOにしたい）
    if S[i-1] == 'R':
        if sc == w:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sc = sc + 1
    elif S[i-1] == 'L':
        if sc == 1:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sc = sc - 1   
    elif S[i-1] == 'U':
        if sr == 1:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sr = sr - 1    
    elif S[i-1] == 'D':
        if sr == h:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sr = sr + 1        
    
    #青木のターン　駒をに残したい（YESにしたい）
    """
    青木が駒をRに動かしたいとき
    1.S[i:n-1]の中にLが含まれるとき
    2.
    
    """
    if T[i-1] == 'R':
        if S[i:n-1].find('L'):
            sc = sc + 1
    elif T[i-1] == 'L':
        if S[i:n-1].find('R'):
            sc = sc - 1
    elif T[i-1] == 'U':
        if S[i:n-1].find('D'):
            sr = sr - 1
    elif T[i-1] == 'D':
        if S[i:n-1].find('U'):
            sr = sr + 1
       
if NO_FLAG == 0:
    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)

Dr = int(H/2) 
Dc = int(W/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:
H,W,N = map(int,input().split())
ssr,ssc = map(int,input().split())
S = input()
T = input()

ssr -= 1
ssc -= 1

A = ["L", "R", "U", "D"]

A2 = {"L": -1, "R": 1, "U": -1, "D": 1}

D ={"L": "R", "R": "L", "U": "D", "D": "U"}

ans = "YES"

flg = 0

for a in A :
    
    sr = ssr
    sc = ssc
    
    if flg :
        break
        
    for i in range(N) :
        if (a in ["L", "R"]) and (a == S[i]) :
            sc += A2[a]
            
            if (sc < 0) or (sc >= W) :
                ans = "NO"
                flg = 1
                break
            
            if D[a] == T[i] :
                sc += A2[D[a]]
            
        elif (a in ["U", "D"]) and (a == S[i]) :
            sr += A2[a]
            
            if (sr < 0) or (sr >= W) :
                ans = "NO"
                flg = 1
                break
            
            if T[i] == D[a] :
                sr += A2[D[a]]
        #print((sr,sc))
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
h,w,n = map(int,input().split(' '))
h-=1
w-=1
sr,sc = map(int,input().split(' '))
sr-=1
sc-=1
S = list(input())
T = list(input())

for i in range(len(S)):
    if S[i] == 'L':
        S[i]=[0,-1]
    elif S[i] == 'R':
        S[i]=[0,1]
    elif S[i] == 'U':
        S[i]=[-1,0]
    else:
        S[i]=[1,0]
        
for i in range(len(T)):
    if T[i] == 'L':
        T[i]=[0,-1]
    elif T[i] == 'R':
        T[i]=[0,1]
    elif T[i] == 'U':
        T[i]=[-1,0]
    else:
        T[i]=[1,0]

# print(S)
# print(T)
flag=True
for i in range(n):
    # print(sr,sc,h,w)
    if 0<=sr+S[i][0]<=h and 0<=sc+S[i][1]<=w:
        sr+=S[i][0]
        sc+=S[i][1]        
        if 0<=sr+T[i][0]<=h and 0<=sc+T[i][1]<=w:
            sr+=T[i][0]
            sc+=T[i][1]
    else:
        print("NO")
        flag=False
        break
if flag is True:
    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 == 'L':
                l -= 1
                if l < 0:
                        print('N0')
                        exit()
        elif s == 'R':
                r -= 1
                if r < 0:
                        print('N0')
                        exit()
        elif s == 'U':
                u -= 1
                if u < 0:
                        print('N0')
                        exit()
        elif s == 'D':
                d -= 1
                if d < 0:
                        print('N0')
                        exit()
        if t == 'L' and r < W - 1:
                r += 1
        elif t == 'R' and l < W - 1:
                l += 1
        elif t == 'U' and d < H - 1:
                d += 1
        elif t == 'D' and u < H - 1:
                u += 1
print('YES')

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(int, input().split())
ss = input().strip()
ts = input().strip()

sd, td = {'R': 0, 'L': 0, 'D': 0, 'U': 0}, {'R': 0, 'L': 0, 'D': 0, 'U': 0}
for s, t in zip(ss + 'N', 'N' + ts):
  if t != 'N':
    td[t] += 1
  if sx - td['L'] + sd['R'] <= 0 or sx + sd['L'] - td['R'] > w or \
    sy - td['U'] + sd['D'] <= 0 or sy + sd['U'] - td['D'] > h:
    td[t] -= 1

  if s != 'N':
    sd[s] += 1
  if sx - td['L'] + sd['R'] > w or sx + td['R'] - sd['L'] <= 0 or \
    sy - td['U'] + sd['D'] > h or sy + td['D'] + sd['U'] <= 0:
    print('NO')
    break
else:
  print('YES')

Submission status: Wrong Answer

Solution code:
from collections import Counter
h,w,n = map(int, input().split())
sx,sy = map(int, input().split())
sd = Counter(input())
td = Counter(input())
wb,hb = 0,0
a1 = (sd["L"]+1,w-sd["R"])
a2 = (sx-td["L"],sx+td["R"])
if (a1[0] <= a1[1] and (a1[0]<=a2[1] or a1[1]<=a2[0])) or a2[0]>a2[1]:
  wb = 1
b1 = (sd["U"]+1,w-sd["D"])
b2 = (sy-td["U"],sy+td["D"])
if (b1[0] <= b1[1] and (b1[0]<=b2[1] or b1[1]<=b2[0])) or b2[0]>b2[1]:
  hb = 1
if wb&hb:
  print("YES")
else:
  print("NO")

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
h,w,n = map(int,input().split(' '))
h-=1
w-=1
sr,sc = map(int,input().split(' '))
sr-=1
sc-=1
S = list(input())
T = list(input())

for i in range(len(S)):
    if S[i] == 'L':
        S[i]=[0,-1]
    elif S[i] == 'R':
        S[i]=[0,1]
    elif S[i] == 'U':
        S[i]=[-1,0]
    else:
        S[i]=[1,0]
        
for i in range(len(T)):
    if T[i] == 'L':
        T[i]=[0,-1]
    elif T[i] == 'R':
        T[i]=[0,1]
    elif T[i] == 'U':
        T[i]=[-1,0]
    else:
        T[i]=[1,0]

# print(S)
# print(T)
flag=True
for i in range(n):
    # print(sr,sc,h,w)
    if 0<=sr+S[i][0]<=h and 0<=sc+S[i][1]<=w:
        sr+=S[i][0]
        sc+=S[i][1]        
        if 0<=sr+T[i][0]<=h and 0<=sc+T[i][1]<=w:
            sr+=T[i][0]
            sc+=T[i][1]
    else:
        print("no")
        flag=False
        break
if flag is True:
    print('yes')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split(' '))
s_r, s_c = map(int, input().split(' '))
S = list(input())
T = list(input())
cnt = 0
for i in range(N):
  if S[i] == 'L':
    if abs((s_c-1)-(W+1)/2)>=abs((s_c)-(W+1)/2):
      s_c -= 1
      if s_c<1:
        break
  elif S[i] == 'R':
    if abs((s_c+1)-(W+1)/2)>=abs((s_c)-(W+1)/2):
      s_c += 1
      if s_c>W:
        break
  elif S[i] == 'U':
    if abs((s_r-1)-(H+1)/2)>=abs((s_r)-(H+1)/2):
      s_r -= 1
      if s_r<1:
        break
  elif S[i] == 'D':
    if abs((s_r+1)-(H+1)/2)>=abs((s_r)-(H+1)/2):
      s_r += 1
      if s_r>H:
        break
  if T[i] == 'L':
    if abs((s_c-1)-(W+1)/2)<=abs((s_c)-(W+1)/2):
      s_c -= 1
  elif  T[i] == 'R':
    if abs((s_c+1)-(W+1)/2)<=abs((s_c)-(W+1)/2):
      s_c += 1
  elif T[i] == 'U':
    if abs((s_r-1)-(H+1)/2)<=abs((s_r)-(H+1)/2):
      s_r -= 1
  elif T[i] == 'D':
    if abs((s_r+1)-(H+1)/2)<=abs((s_r)-(H+1)/2):
      s_r += 1
  cnt += 1
if cnt==N:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
from sys import exit
from collections import defaultdict
import itertools
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())

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

sr -= 1
sc -= 1

A = ["L", "R", "U", "D"]

A2 = {"L": -1, "R": 1, "U": -1, "D": 1}

D ={"L": "R", "R": "L", "U": "D", "D": "U"}

ans = "YES"

flg = 0

for a in A :
    
    if flg :
        break
        
    for i in range(N) :
        if (a in ["L", "R"]) and (a == S[i]) :
            sc += A2[a]
            
            if (sc < 0) or (sc >= W) :
                ans = "NO"
                flg = 1
                break
            
            if D[a] == T[i] :
                sc += A2[D[a]]
            
        elif (a in ["U", "D"]) and (a == S[i]) :
            sr += A2[a]
            
            if (sr < 0) or (sr >= W) :
                ans = "NO"
                flg = 1
                break
            
            if T[i] == D[a] :
                sr += A2[D[a]]

print(ans)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/agc033
# -*- coding: utf-8 -*-

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]+1
L = current[1]
R = w-current[1]+1

sU = s.count("U")
sD = s.count("D")
sL = s.count("L")
sR = s.count("R")

tU = t.count("U")
tD = t.count("D")
tL = t.count("L")
tR = t.count("R")

# print(U,D,L,R)
# print(sU,sD,sL,sR)
# print(tU,tD,tL,tR)

win="NO"
if (U-sU+tU)>0:
        win="YES"
if (D-sD+tD)>0:
        win="YES"
if (L-sL+tL)>0:
        win="YES"
if (R-sR+tR)>0:
        win="YES"

print(win)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split(' '))
s_r, s_c = map(int, input().split(' '))
S = list(input())
T = list(input())
cnt = 0
for i in range(N):
  if S[i] == 'L':
    if abs((s_c-1)-(W+1)/2)>=abs((s_c)-(W+1)/2):
      s_c -= 1
      if s_c<1:
        break
  elif S[i] == 'R':
    if abs((s_c+1)-(W+1)/2)>=abs((s_c)-(W+1)/2):
      s_c += 1
      if s_c>W:
        break
  elif S[i] == 'U':
    if abs((s_r-1)-(H+1)/2)>=abs((s_r)-(H+1)/2):
      s_r -= 1
      if s_r<1:
        break
  else:
    if abs((s_r+1)-(H+1)/2)>=abs((s_r)-(H+1)/2):
      s_r += 1
      if s_r>H:
        break
  if T[i] == 'L':
    if abs((s_c-1)-(W+1)/2)<=abs((s_c)-(W+1)/2):
      s_c -= 1
  elif  T[i] == 'R':
    if abs((s_c+1)-(W+1)/2)<=abs((s_c)-(W+1)/2):
      s_c += 1
  elif T[i] == 'U':
    if abs((s_r-1)-(H+1)/2)<=abs((s_r)-(H+1)/2):
      s_r -= 1
  else:
    if abs((s_r+1)-(H+1)/2)<=abs((s_r)-(H+1)/2):
      s_r += 1
  cnt += 1
if cnt==N:
  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 = list(input())
T = list(input())
cnt = 0
for i in range(N):
  if S[i] == 'L':
    if abs((s_c-1)-W+1/2)>=abs((s_c)-W+1/2):
      s_c -= 1
      if s_c<1:
        break
  elif S[i] == 'R':
    if abs((s_c+1)-W+1/2)>=abs((s_c)-W+1/2):
      s_c += 1
      if s_c>W:
        break
  elif S[i] == 'U':
    if abs((s_r-1)-H+1/2)>=abs((s_r)-H+1/2):
      s_r -= 1
      if s_r<1:
        break
  else:
    if abs((s_r+1)-H+1/2)>=abs((s_r)-H+1/2):
      s_r += 1
      if s_r>H:
        break
  if T[i] == 'L':
    if abs((s_c-1)-W+1/2)<=abs((s_c)-W+1/2):
      s_c -= 1
  elif  T[i] == 'R':
    if abs((s_c+1)-W+1/2)<=abs((s_c)-W+1/2):
      s_c += 1
  elif T[i] == 'U':
    if abs((s_r-1)-H+1/2)<=abs((s_r)-H+1/2):
      s_r -= 1
  else:
    if abs((s_r+1)-H+1/2)<=abs((s_r)-H+1/2):
      s_r += 1
  cnt += 1
if cnt==N:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
def inpl(): return list(map(int, input().split()))

H, W, N = inpl()
sr, sc = inpl()
X = input()
Y = input()
S = defaultdict(int)
T = defaultdict(int)
ans = True

x = sc*1
for i in range(N):
    if X[i] == "R":
        x += 1
    if x > W:
        ans *= False
    if Y[i] == "L":
        x = max(1, x-1)

x = sc*1
for i in range(N):
    if X[i] == "L":
        x -= 1
    if x <= 0:
        ans *= False
    if Y[i] == "R":
        x = min(W, x+1)

x = sc*1
for i in range(N):
    if X[i] == "D":
        x += 1
    if x > W:
        ans *= False
    if Y[i] == "U":
        x = max(1, x-1)

x = sc*1
for i in range(N):
    if X[i] == "U":
        x -= 1
    if x <= 0:
        ans *= False
    if Y[i] == "D":
        x = min(H, x+1)

print(["NO", "YES"][ans])

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


def solve():
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if T[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if T[i] == 'D':
            maxd += 1
            if maxd == W+1:
                return "NO"
        if T[i] == 'R':
            maxr += 1
            if maxr == H+1:
                return "NO"
        if T[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"

        if T[i] == 'U' and minu > 1:
            minu -= 1
        if T[i] == 'D' and maxd < W:
            maxd += 1
        if T[i] == 'R' and maxr < H:
            maxr += 1
        if T[i] == 'L' and minl > 1:
            minl -= 1

    return "YES"

print(solve())

Submission status: Wrong Answer

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

NO_FLAG = 0

for i in range(n):
    #高橋のターン　駒を出したい（NOにしたい）
    if S[i-1] == 'R':
        if sc == w:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sc = sc + 1
    elif S[i-1] == 'L':
        if sc == 1:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sc = sc - 1   
    elif S[i-1] == 'U':
        if sr == 1:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sr = sr - 1    
    elif S[i-1] == 'D':
        if sr == h:
            print('NO')
            NO_FLAG = 1
            break
        else:
            sr = sr + 1        
    
    #青木のターン　駒をに残したい（YESにしたい）
    if T[i-1] == 'R':
        if sc < w:
            sc = sc + 1
    elif T[i-1] == 'L':
        if sc == w and sc != 1:
            sc = sc - 1
    elif T[i-1] == 'U':
        if sr == h and sr != 1:
            sr = sr - 1
    elif T[i-1] == 'D':
        if sr < h:
            sr = sr + 1
       
if NO_FLAG == 0:
    print('YES')

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+1]*n
down = [0]*n
right = [w+1]*n
left = [0]*n

if s[-1] == 'U':
    up[-1] = up[-1]-1
elif s[-1] == 'D':
    down[-1] = down[-1]+1
elif s[-1] == 'L':
    left[-1] = left[-1]+1
else:
    right[-1] = right[-1]-1




for i in range(n-2,-1,-1):
    up[i] = up[i+1]
    down[i] = down[i+1]
    left[i] = left[i+1]
    right[i] = right[i+1]
    if s[i] == 'U':
        up[i] = up[i+1]-1
    elif s[i] == 'D':
        down[i] = down[i+1]+1
    elif s[i] == 'L':
        left[i] = left[i+1]+1
    else:
        right[i] = right[i+1]-1

for i in range(n-2,-1,-1):
    if t[i] == 'U':
        down[i] -= 1
    if t[i] == 'D':
        up[i] += 1
    if t[i] == 'R':
        left[i] -= 1
    if t[i] == 'L':
        right[i] += 1


if up[0]<=y or down[0]>=y or right[0]<=x or left[0] >= y:
    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()


def solve():
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if T[i] == 'U':
            minu -= 1
            if minu == 0:
                return "No"
        if T[i] == 'D':
            maxd += 1
            if maxd == W+1:
                return "No"
        if T[i] == 'R':
            maxr += 1
            if maxr == H+1:
                return "No"
        if T[i] == 'L':
            minl -= 1
            if minl == 0:
                return "No"

        if T[i] == 'U' and minu > 1:
            minu -= 1
        if T[i] == 'D' and maxd < W:
            maxd += 1
        if T[i] == 'R' and maxr < H:
            maxr += 1
        if T[i] == 'L' and minl > 1:
            minl -= 1

    return "Yes"

print(solve())

Submission status: Wrong Answer

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

sr -= 1
sc -= 1

A = ["L", "R", "U", "D"]

A2 = {"L": -1, "R": 1, "U": -1, "D": 1}

#D ={"L": "R", "R": "L", "U": "D", "D": "U"}

#D2 = {"L": 1, "R": -1, "U": 1, "D": -1}

ans = "YES"

flg = 0

for a in A :
    
    if flg :
        break
        
    for i in range(N) :
        if (a in ["L", "R"]) and (a == S[i]) :
            sc += A2[a]
            
            if (sc < 0) or (sc >= W) :
                ans = "NO"
                flg = 1
                break
            
            if a == T[i] :
                sc -= A2[a]
            
        elif (a in ["U", "D"]) and (a == S[i]) :
            sr += A2[a]
            
            if (sr < 0) or (sr >= W) :
                ans = "NO"
                flg = 1
                break
            
            if a == T[i] :
                sr -= A2[a]

print(ans)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
sr-=1
sc-=1
s = input()
t = input()
a = [[int(1)]*h]*w
a[sc][sr] = 0
for i in range(n):
    if s[i] == 'L':
        sc-=1
    elif s[i] == 'R':
        sc +=1
    elif s[i] == 'U':
        sr-=1
    elif s[i] == 'D':
        sr +=1
    if ((sr < 0) or (sc < 0) or (sr >= h) or (sc >= w)):
        break
    a[sc][sr] = 0
    if t[i] == 'L':
        sc-=1
    elif t[i] == 'R':
        sc +=1
    elif t[i] == 'U':
        sr-=1
    elif t[i] == 'D':
        sr +=1
    if ((sr < 0) or (sc < 0) or (sr >= h) or (sc >= w)):
        break
    a[sc][sr] = 0

s=0
for i in range(w):
    for j in range(h):
        if a[i][j] ==1:
            s=1

if s==0:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from sys import exit
from collections import defaultdict
import itertools
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)

def check(outcnt, C, A, lim):
    for s,t in zip(S,T):
        if( s == C):
            outcnt -= 1
        if(outcnt == 0 ):
            print("NO")
            exit()
        if(t == A and outcnt < lim):
            outcnt += 1
        # print(s,t)
        # print(outcnt)
# print(Rout, Lout, Uout, Dout)
check(Rout, "R", "L", W)
check(Lout, "L", "R", W)
check(Uout, "U", "D", H)
check(Dout, "D", "U", H)
print("YES")
# #右の検証
# for s,t in itertools.product(S,T):
#     if( s == "R"):
#         Rout, Lout = Rout-1, Lout + 1
#     if(t == "L"):
#         Rout, Lout = Rout+1, Lout - 1
#     if(Rout == 0 ):
#         print("Yes")
#         exit()
# #左
# for s,t in itertools.product(S,T):
#     if( s == "L"):
#         Lout = Lout - 1
#     if(t == "R"):
#         Lout = , Lout + 1
#     if(Rout == 0 ):
#         print("Yes")
#         exit()

Submission status: Wrong Answer

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

# L, R, U, D = (0, -1), (0, 1), (-1, 0), (1, 0)
for i in range(N):
    # Takahashi
    if S[i] == "L" or S[i] == "R":
        if S[i] == "L":
            if x-1 <= W - x:
                x -= 1
        else:
            if x-1 >= W - x:
                x += 1
    else:
        if S[i] == "U":
            if y-1 <= H - y:
                y -= 1
        else:
            if y-1 <= H - y:
                y += 1
    if x <= 0 or x > W or y <= 0 or y > H:
        print("NO")
        exit(0)
    # Aoki
    if T[i] == "L" or T[i] == "R":
        if T[i] == "L":
            if x-1 > W - x:
                x -= 1
        else:
            if x-1 < W - x:
                x += 1
    else:
        if T[i] == "U":
            if y-1 > H - y:
                y -= 1
        else:
            if y-1 < H - y:
                y += 1
    if x <= 0 or x > W or y <= 0 or y > H:
        print("NO")
        exit(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()

Pue = sr
Psita = H-sr + 1

Phidari = sc
Pmigi = W-sc + 1

from collections import Counter
SC = Counter(S)
ST = Counter(T)

# L 
flag = False
if SC["L"] - ST["R"] >= Phidari:
    flag = True
if SC["R"] - ST["L"] >= Pmigi:
    flag = True
if SC["U"] - ST["D"] >= Pue:
    flag = True
if SC["D"] - ST["U"] >= Psita:    
    flag = True

if flag:
    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()
taka = [0,0,0,0]
aoki = [0,0,0,0]
ans = "YES"
for i,j in zip(s,t):
  if i == "L":
    taka[0] -= 1
    if sc + taka[0] + aoki[1] <= 0:
      ans = "NO"
      break
  elif i == "R":
    taka[1] += 1
    if sc + taka[1] + aoki[0] > w:
      ans = "NO"
      break
  elif i == "U":
    taka[2] -= 1
    if sr + taka[2] + aoki[3] <= 0:
      ans = "NO"
      break
  else:
    taka[3] += 1
    if sr + taka[3] + aoki[2] > h:
      ans = "NO"
      break
  if j == "L":
    aoki[0] = max(-sc+1,aoki[0]-1)
  elif j == "R":
    aoki[1] = min(w,aoki[1]+1)
  elif j == "U":
    aoki[2] = max(-sr+1,aoki[2]-1)
  else:
    aoki[3] = min(h,aoki[3]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/agc033/tasks/agc033_b

HWN = [int(i) for i in input().split()]
s = [int(i) for i in input().split()]
S = list(str(input()))
T = list(str(input()))

Rn = HWN[1] - s[1] + 1
Ln = HWN[1] - Rn + 1
Dn = HWN[0] - s[0] + 1
Un = HWN[0] - Dn + 1

L = 0
R = 0
U = 0
D = 0
l = 0
r = 0
u = 0
d = 0
ll = 0
rr = 0
uu = 0
dd = 0
stat = 0
for i in range(len(S)):
    if(S[i] == 'L'):
        L += 1
    if(S[i] == 'R'):
        R += 1
    if(S[i] == 'U'):
        U += 1
    if(S[i] == 'D'):
        D += 1

    if(T[i] == 'L'):
        l += 1
    if(T[i] == 'R'):
        r += 1
    if(T[i] == 'U'):
        u += 1
    if(T[i] == 'D'):
        d += 1
    if(L < r):
        r = L
    if(R < l):
        l = R
    if(U < d):
        d = U
    if(D < u):
        u = D
    ll = L - r
    rr = R - l
    uu = U - d
    dd = D - u
    if ll >= Ln or rr >= Rn or uu >= Un or dd >= Dn:
        stat = 1
        break

if stat == 1:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

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

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

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

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

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

#print(dp)
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()


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

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

    if s=="L":
        l+=1
    elif s=="R":
        r-=1
    elif s=="U":
        u+=1
    else:
        d-=1

    if l > r or u > d:
        print("NO")
        exit()
    #print(l, r, u, d)
if u<=Sr<=d and l<=Sc<=r:
    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()


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

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

    if s=="L":
        l+=1
    elif s=="R":
        r-=1
    elif s=="U":
        u+=1
    else:
        d-=1

    if l > r or u > d:
        print("NO")
        exit()
    #print(l, r, u, d)
if d<=Sr<=u and l<=Sc<=r:
    print("YES")
else:
    print("NO")