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

Solution (Python):
from sys import stdin
def main():
    h, w, n = map(int, stdin.readline().split())
    r, c = map(int, stdin.readline().split())
    s = stdin.readline().strip()
    t = stdin.readline().strip()
    x = r
    y = c
    blx = 0
    brx = h + 1
    bly = 0
    bry = w + 1
    for i in xrange(n - 1, -1, -1):
        a, b = s[i], t[i]
        if b == 'U':
            if brx <= h:
                brx += 1
        elif b == 'D':
            if blx >= 1:
                blx -= 1
        elif b == 'L':
            if bry <= w:
                bry += 1
        else :
            if bly >= 1:
                bly -= 1
        if a == 'U':
            blx += 1
        elif a == 'D':
            brx -= 1
        elif a == 'L':
            bly += 1
        else:
            bry -= 1
        if brx - blx <= 1 or bry - bly <= 1:
            print 'NO'
            return
    if blx < r < brx and bly < c < bry:
        print 'YES'
    else:
        print 'NO'
main()

Solution (Python):
H,W,N = map(int, input().split())
s_r, s_c = map(int, input().split())
S = input()
T = input()
Judge = False

S_L, S_R, S_U, S_D = 0, 0, 0, 0
T_L, T_R, T_U, T_D = 0, 0, 0, 0

for x in range(N):
    if S[x] == 'L':
        S_L += 1
        if S_L - T_R - s_c == 0:
            Judge = True
    elif S[x] == 'R':
        S_R += 1
        if s_c + (S_R - T_L) == W+1:
            Judge = True
    elif S[x] == 'U':
        S_U += 1
        if S_U - T_D - s_r == 0:
            Judge = True
    elif S[x] == 'D':
        S_D += 1
        if s_r + (S_D - T_U) == H+1:
            Judge = True
    if T[x] == 'L':
        if S_R - T_L + s_c != 1:
            T_L += 1
    if T[x] == 'R':
        if s_c + (T_R - S_L) != W:
            T_R += 1
    if T[x] == 'U':
        if S_D - T_U + s_r != 1:
            T_U += 1
    if T[x] == 'D':
        if s_r + (T_D - S_U) != H:
            T_D += 1
    
if Judge:
    print('NO')
else:
    print('YES')

Solution (Python):
import sys
input = sys.stdin.readline

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


rest=W-sc+1

for i in range(N):
    if S[i]=="R":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

    if T[i]=="L":
        rest=min(W,rest+1)



rest=sc

for i in range(N):
    if S[i]=="L":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

    if T[i]=="R":
        rest=min(W,rest+1)



rest=sr

for i in range(N):
    if S[i]=="U":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

    if T[i]=="D":
        rest=min(H,rest+1)



rest=H-sr+1

for i in range(N):
    if S[i]=="D":
        rest-=1

    if rest==0:
        print("NO")
        sys.exit()

    if T[i]=="U":
        rest=min(H,rest+1)


print("YES")

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

border_U = 0
border_D = H + 1
border_L = 0
border_R = W + 1

res = "YES"

for i in range(N-1):

    
    if S[N-1-i] == "U":
        border_U += 1
    elif S[N-1-i] == "D":
        border_D -= 1
    elif S[N-1-i] == "L":
        border_L += 1
    elif S[N-1-i] == "R":
        border_R -= 1

    
    if border_U >= border_D - 1 or border_L >= border_R - 1:
        res = "NO"

    
    if T[N - 2 - i] == "U":
        border_D += 1
    elif T[N - 2 - i] == "D":
        border_U -= 1
    elif T[N - 2 - i] == "L":
        border_R += 1
    elif T[N - 2 - i] == "R":
        border_L -= 1

    
    border_U = max(border_U, 0)
    border_D = min(border_D, H + 1)
    border_L = max(border_L, 0)
    border_R = min(border_R, W + 1)
    


if S[0] == "U":
    border_U += 1
elif S[0] == "D":
    border_D -= 1
elif S[0] == "L":
    border_L += 1
elif S[0] == "R":
    border_R -= 1

if sr <= border_U:
    res = "NO"
elif sr >= border_D:
    res = "NO"
elif sc <= border_L:
    res = "NO"
elif sc >= border_R:
    res = "NO"


print(res)

Solution (Python):
def check(dist, tak, aok, lim):
    for s, t in zip(ss, ts):
        if s == tak:
            if dist == 0:
                return True
            dist -= 1
        if t == aok:
            if dist < lim:
                dist += 1
    return False


def solve():
    if check(sr - 1, 'U', 'D', h - 1):
        return True
    if check(sc - 1, 'L', 'R', w - 1):
        return True
    if check(h - sr, 'D', 'U', h - 1):
        return True
    if check(w - sc, 'R', 'L', w - 1):
        return True
    return False


h, w, n = list(map(int, input().split()))
sr, sc = map(int, input().split())
ss = input()
ts = input()
print('NO' if solve() else 'YES')

Solution (Python):

import sys

inf = 10 ** 18

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

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

y_t = y_b = sr
x_r = x_l = sc

ans = 'NO'
for i, (Si, Ti) in enumerate(zip(S, T)):
    
    if Si == 'L':
        x_l -= 1
    elif Si == 'R':
        x_r += 1
    elif Si == 'U':
        y_t -= 1
    else:
        y_b += 1
    
    if x_l < 1: break
    if y_t < 1: break
    if x_r > W: break
    if y_b > H: break
    
    if Ti == 'L':
        x_r = max(1, x_r - 1)
    elif Ti == 'R':
        x_l = min(W, x_l + 1)
    elif Ti == 'U':
        y_b = max(1, y_b - 1)
    else:
        y_t = min(H, y_t + 1)
else:
    ans = 'YES'
        
print(ans)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

H, W, N = map(int, input().split())
sr, sc =  map(int, input().split())
sr -= 1
sc -= 1

S = input()
T = input()

def solve2(sc, W, S, T, inc, dec):
    x = sc
    for s, t in zip(S, T):
        if s == inc:
            x += 1
            if x == W:
                return True
        if t == dec:
            x = max(x - 1, 0);

    x = sc
    for s, t in zip(S, T):
        if s == dec:
            x -= 1
            if x == -1:
                return True
        if t == inc:
            x = min(x + 1, W - 1);

    return False


def solve(H, W, N, sr, sc, S, T):
    res1 = solve2(sc, W, S, T, 'R', 'L')
    res2 = solve2(sr, H, S, T, 'D', 'U')
    return res1 or res2

    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    


            


res = solve(H, W, N, sr, sc, S, T)
if res:
    print('NO')
else:
    print('YES')

Solution (Python):
H,W,N = map(int,input().split())
SR,SC = map(int,input().split())
S = input()
T = input()

y = SR
for s,t in zip(S,T):
    if s=='U':
        y -= 1
        if y==0:
            print('NO')
            exit()
    if t=='D' and y < H:
        y += 1

y = SR
for s,t in zip(S,T):
    if s=='D':
        y += 1
        if y==H+1:
            print('NO')
            exit()
    if t=='U' and y > 1:
        y -= 1

x = SC
for s,t in zip(S,T):
    if s=='L':
        x -= 1
        if x==0:
            print('NO')
            exit()
    if t=='R' and x < W:
        x += 1

x = SC
for s,t in zip(S,T):
    if s=='R':
        x += 1
        if x==W+1:
            print('NO')
            exit()
    if t=='L' and x > 1:
        x -= 1

print('YES')