Problem:
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.

Incorrect submission (verdict: Wrong Answer):
h, w, n = map(int, input().split())
sx, sy = map(int, input().split())

s = list(input())
t = list(input())






flag = True


cnt_up = 0

for i in range(n):
    if s[i] == "U":
        cnt_up += 1
        if cnt_up >= sx:
            flag = False
            break
    if t[i] == "D":
        if cnt_up > 1:
            cnt_up -= 1


cnt_down = 0


for i in range(n):
    if s[i] == "D":
        cnt_down += 1
        if cnt_down >= h - sx + 1:
            flag = False
            break
    if t[i] == "U":
        if cnt_down > 1:
            cnt_down -= 1



cnt_left = 0

for i in range(n):
    if s[i] == "L":
        cnt_left += 1
        if cnt_left >= sy:
            flag = False
            break
    if t[i] == "R":
        if cnt_left > 1:
            cnt_left -= 1


cnt_right = 0

for i in range(n):
    if s[i] == "R":
        cnt_right += 1
        if cnt_right >= w - sy + 1:
            flag = False
            break
    if t[i] == "L":
        if cnt_right > 1:
            cnt_right -= 1


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



Fixed, accepted submission:

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

sr_deque=[]
sc_deque=[]

for i in range(n):
    p=s[i]
    if p=="U":
        
        sr_deque.append((-1,1))
    elif p=="D":
        sr_deque.append((1,1))
    q=t[i]
    if q=="U":
        
        sr_deque.append((-1,2))
    elif q=="D":
        sr_deque.append((1,2))      

for i in range(n):
    p=s[i]
    if p=="R":
        
        sc_deque.append((1,1))
    elif p=="L":
        sc_deque.append((-1,1))
    q=t[i]
    if q=="R":
        
        sc_deque.append((1,2))
    elif q=="L":
        sc_deque.append((-1,2)) 
        
sr_deque=sr_deque[::-1]
sc_deque=sc_deque[::-1]

def judge(start_node,width,deque):
    safe_left=1
    safe_right=width
    flags=True
    for que in deque:
        if que[1]==1:
            
            if que[0]==1:
                if safe_right>0:
                    safe_right-=1
           
            elif que[0]==-1:
                if safe_left<width+1:
                    safe_left+=1 
            
        if que[1]==2:
            
            if que[0]==1:
                if safe_left>1:
                    safe_left-=1
                
                
            elif que[0]==-1:
                if safe_right<width:
                    safe_right+=1
 
        
        if safe_left>safe_right:
            return False
    return safe_left<=start_node<=safe_right
  
def safe():
    return judge(sr,h,sr_deque) and judge(sc,w,sc_deque)
if safe():
    print("YES")
else:
    print("NO")