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:
import sys
sys.setrecursionlimit(10**6)
#input = sys.stdin.buffer.readline

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

sr -= 1
sc -= 1

memot = [[-1]*w for i in range(h)]
memoa = [[-1]*w for i in range(h)]

def move(r, c, m):
    if m == 'L':
        nr, nc = r, c-1
    elif m == 'R':
        nr, nc = r, c+1
    elif m == 'U':
        nr, nc = r-1, c+1
    else:
        nr, nc = r+1, c
    return nr, nc

def judget(r, c, i):
    if memot[r][c] != -1:
        return memot[r][c]
    nr, nc = move(r, c, s[i])
    if nr < 0 or nr > h-1 or nc < 0 or nc > w-1:
        return 1

    memot[r][c] = 1 - judgea(nr, nc, i)
    return memot[r][c]

def judgea(r, c, i):
    if memoa[r][c] != -1:
        return memoa[r][c]
    nr, nc = move(r, c, t[i])
    if nr < 0 or nr > h-1 or nc < 0 or nc > w-1:
        nr, nc = r, c
    if i == n-1:
        memoa[r][c] = 1
        return memoa[r][c]
    else:
        memoa[r][c] = 1 - judget(nr, nc, i+1)
        return memoa[r][c]

judget(sr, sc, 0)
#print(memot)
#print(memoa)
if memot[sr][sc] == 1:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
s_y, s_x = map(int, input().split())
S = str(input())
T = str(input())

# 高橋: 落とそうとする
# 青木: 残そうとする

lr = []
ud = []

for tup in zip(S, T):
    for n, i in enumerate(tup):
        if i == "L" or i == "R":
            lr.append((n, i))
        else:
            ud.append((n, i))

def f():
    crd = s_x
    # 貪欲に左へ
    # i == 0 -> 高橋
    # i == 1 -> 青木
    for i, j in lr:
        if i == 0 and j == "L":
            crd -= 1
        if i == 1 and j == "R" and crd < W:
            crd += 1
        if crd == 0:
            return 0
    # 貪欲に右へ
    crd = s_x
    for i, j in lr:
        if i == 0 and j == "R":
            crd += 1
        if i == 1 and j == "L" and crd > 1:
            crd -= 1
        if crd == W + 1:
            return 0
    # 貪欲に上へ
    crd = s_y
    for i, j in lr:
        if i == 0 and j == "U":
            crd -= 1
        if i == 1 and j == "D" and crd < H:
            crd += 1
        if crd == 0:
            return 0
    # 貪欲に下へ
    crd = s_y
    for i, j in lr:
        if i == 0 and j == "D":
            crd += 1
        if i == 1 and j == "U" and crd > 1:
            crd -= 1
        if crd == H + 1:
            return 0
    return 1

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

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    s, r = map(int, input().split())
    S = input().strip("\n")
    T = input().strip("\n")
    Lb, Rb, Ub, Db = -1, W, -1, H
    for i in reversed(range(N)):
        if i != N - 1:
            if T[i] == "L": Rb = min(Rb + 1, W)
            elif T[i] == "R": Lb = max(Lb - 1, -1)
            elif T[i] == "U": Db = min(Db + 1, H)
            elif T[i] == "D": Ub = max(Ub - 1, 0)

        if S[i] == "L": Lb = Lb + 1
        elif S[i] == "R": Rb = Rb - 1
        elif S[i] == "U": Ub = Ub + 1
        elif S[i] == "D": Db = Db - 1

        if Lb >= W - 1 or Rb <= 0 or Ub >= H - 1 or Db <= 0:
            print("No")
            break
    else:
        if Lb < r - 1 < Rb and Ub < s - 1 < Db: print("Yes")
        else: print("No")
    
    #print(Lb, Rb, Ub, Db)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

command = []
for i in range(N):
    command.append(S[i])
    command.append(T[i])


# X方向
nowL = 0
nowR = W + 1
for i, c in enumerate(command):
    if i % 2 == 0:
        if c == "L":
            nowL -= 1
            nowL = max(nowL, 0)
        elif c == "R":
            nowR += 1
            nowR = min(nowR, W + 1)
        else:
            continue
    elif i % 2 == 1:
        if c == "L":
            nowL += 1
            if nowL == sy:
                print("NO")
                exit()
        elif c == "R":
            nowR -= 1
            if nowR == sy:
                print("NO")
                exit()
        else:
            continue

# Y方向
nowU = 0
nowD = H + 1
for i, c in enumerate(command):
    if i % 2 == 0:
        if c == "D":
            nowU -= 1
            nowU = max(nowU, 0)
        elif c == "U":
            nowD += 1
            nowD = min(nowD, H + 1)
        else:
            continue

    elif i % 2 == 1:
        if c == "D":
            nowD += 1
            if nowD == sx:
                print("NO")
                exit()
        elif c == "U":
            nowU -= 1
            if nowU == sx:
                print("NO")
                exit()
        else:
            continue


print("YES")

Submission status: Wrong Answer

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

command = []
for i in range(N):
    command.append(S[i])
    command.append(T[i])


# X方向
nowL = 0
nowR = W + 1
for i, c in enumerate(command):
    if i % 2 == 0:
        if c == "R":
            nowL -= 1
            nowL = max(nowL, 0)
        elif c == "L":
            nowR += 1
            nowR = min(nowR, W + 1)
        else:
            continue
    elif i % 2 == 1:
        if c == "R":
            nowL += 1
            if nowL == sy:
                print("NO")
                exit()
        elif c == "L":
            nowR -= 1
            if nowR == sy:
                print("NO")
                exit()
        else:
            continue

# Y方向
nowU = 0
nowD = H + 1
for i, c in enumerate(command):
    if i % 2 == 0:
        if c == "U":
            nowU -= 1
            nowU = max(nowU, 0)
        elif c == "D":
            nowD += 1
            nowD = min(nowD, H + 1)
        else:
            continue

    elif i % 2 == 1:
        if c == "U":
            nowD += 1
            if nowD == sx:
                print("NO")
                exit()
        elif c == "D":
            nowU -= 1
            if nowU == sx:
                print("NO")
                exit()
        else:
            continue


print("YES")

Submission status: Wrong Answer

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

command = []
for i in range(N):
    command.append(S[i])
    command.append(T[i])


# X方向
nowL = 0
nowR = W + 1
for i, c in enumerate(command):
    if i % 2 == 0:
        if c == "L":
            nowL -= 1
            nowL = max(nowL, 0)
        elif c == "R":
            nowR += 1
            nowR = min(nowR, W + 1)
        else:
            continue
    elif i % 2 == 1:
        if c == "R":
            nowL += 1
            if nowL == sy:
                print("NO")
                exit()
        elif c == "L":
            nowR -= 1
            if nowR == sy:
                print("NO")
                exit()
        else:
            continue

# Y方向
nowU = 0
nowD = H + 1
for i, c in enumerate(command):
    if i % 2 == 0:
        if c == "U":
            nowU -= 1
            nowU = max(nowU, 0)
        elif c == "D":
            nowD += 1
            nowD = min(nowD, H + 1)
        else:
            continue

    elif i % 2 == 1:
        if c == "U":
            nowD += 1
            if nowD == sx:
                print("NO")
                exit()
        elif c == "D":
            nowU -= 1
            if nowU == sx:
                print("NO")
                exit()
        else:
            continue


print("YES")

Submission status: Wrong Answer

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

command = []
for i in range(N):
    command.append(S[i])
    command.append(T[i])


# X方向
nowL = 0
nowR = W + 1
for i, c in enumerate(command):
    if c is None:
        continue
    if i % 2 == 0:
        if c == "L":
            nowL -= 1
            nowL = max(nowL, 0)
        elif c == "R":
            nowR += 1
            nowR = min(nowR, W + 1)
        else:
            continue
    elif i % 2 == 1:
        if c == "R":
            nowL += 1
            if nowL == sx:
                print("NO")
                exit()
        elif c == "L":
            nowR -= 1
            if nowR == sx:
                print("NO")
                exit()
        else:
            continue

# Y方向
nowU = 0
nowD = H + 1
for i, c in enumerate(command):
    if i % 2 == 0:
        if c == "U":
            nowU -= 1
            nowU = max(nowU, 0)
        elif c == "D":
            nowD += 1
            nowD = min(nowD, H + 1)
        else:
            continue

    elif i % 2 == 1:
        if c == "U":
            nowD += 1
            if nowD == sx:
                print("NO")
                exit()
        elif c == "D":
            nowU -= 1
            if nowU == sx:
                print("NO")
                exit()
        else:
            continue


print("YES")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
src = list(map(int,input().split()))

s = input()
t = input()
s_c, t_c = [], []
for i in ["U", "D", "L", "R"]:
    s_c.append(s.count(i))
    t_c.append(t_c.count(i))
if src[0] <= s_c[0]-t_c[1] or h-src[0]+1<=s_c[1]-t_c[0] or src[1]<=s_c[2]-t_c[3] or w-src[1]+1<=s_c[3]-s_c[2]:
    result = "NO"
else:
    result = "YES"
    for i in range(n):
        if s[i]=="U" and src[0]<=h-src[0]+1:
            src[0] -= 1
        elif s[i]=="D" and h-src[0]+1<=src[0]:
            src[0] += 1
        elif s[i]=="L" and src[1]<=w-src[1]+1:
            src[1] -= 1
        elif s[i]=="R" and w-src[1]+1<=src[1]:
            src[1] += 1
        if src[0]<1 or src[0]>h or src[1]<1 or src[1]>w:
            result = "NO"
            break

        if t[i]=="U" and src[0]>h-src[0]+1:
            src[0] -= 1
        elif t[i]=="D" and h-src[0]+1>src[0]:
            src[0] += 1
        elif t[i]=="L" and src[1]>w-src[1]+1:
            src[1] -= 1
        elif t[i]=="R" and w-src[1]+1>src[1]:
            src[1] += 1

print(result)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
src = list(map(int,input().split()))

s = input()
t = input()
result = "YES"
for i in range(n):
    if s[i]=="U" and src[0]<h-src[0]+1:
        src[0] -= 1
    elif s[i]=="D" and h-src[0]+1<src[0]:
        src[0] += 1
    elif s[i]=="L" and src[1]<w-src[1]+1:
        src[1] -= 1
    elif s[i]=="R" and w-src[1]+1<src[1]:
        src[1] += 1
    if src[0]<1 or src[0]>h or src[1]<1 or src[1]>w:
        result = "NO"
        break

    if t[i]=="U" and src[0]>h-src[0]+1:
        src[0] -= 1
    elif t[i]=="D" and h-src[0]+1>src[0]:
        src[0] += 1
    elif t[i]=="L" and src[1]>w-src[1]+1:
        src[1] -= 1
    elif t[i]=="R" and w-src[1]+1>src[1]:
        src[1] += 1

print(result)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
src = list(map(int,input().split()))

s = input()
t = input()
result = "YES"
for i in range(n):
    if s[i]=="U" and src[0]<=h-src[0]+1:
        src[0] -= 1
    elif s[i]=="D" and h-src[0]+1<=src[0]:
        src[0] += 1
    elif s[i]=="L" and src[1]<=w-src[1]+1:
        src[1] -= 1
    elif s[i]=="R" and w-src[1]+1<=src[1]:
        src[1] += 1
    if src[0]<1 or src[0]>h or src[1]<1 or src[1]>w:
        result = "NO"
        break

    if t[i]=="U" and src[0]>h-src[0]+1:
        src[0] -= 1
    elif t[i]=="D" and h-src[0]+1>src[0]:
        src[0] += 1
    elif t[i]=="L" and src[1]>w-src[1]+1:
        src[1] -= 1
    elif t[i]=="R" and w-src[1]+1>src[1]:
        src[1] += 1

print(result)

Submission status: Wrong Answer

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

S = input()
T = input()
mvs = {"L":0, "R":1,"U":2,"D":3}

# 高橋くんが選ぶ戦略 : どの方向に進んでいくか

def accX(S):
    accs = [[0,0,0,0]] #L,R,U,D
    for s in S[::-1]:
        l,r,u,d = accs[-1]
        if s == "L" : l += 1
        elif s == "R": r += 1
        elif s == "U" : u += 1
        else:d += 1
        accs.append([l,r,u,d])
    return accs[::-1]
accS = accX(S)

now = [sr-1,sc-1]
for i,(s,t) in enumerate(zip(S,T)):
    h,w = now
    # 高橋
    if s == "L" : 
        if w - accS[i][0] < 0:
            w -= 1
    elif s == "R":
        if w + accS[i][1] >= W:
            w += 1
    elif s == "U" :
        if h - accS[i][2] < 0:
            h -= 1
    else:
        if h + accS[i][3] >= H:
            h += 1
    if w < 0 or w >= W or h < 0 or h >= H:
        print("NO")
        exit()
    # 青木
    if t == "L" : 
        if min(w+1,W-w) < min(w-1+1,W-w+1):
            w -= 1
    if t == "R" :
        if min(w+1,W-w) < min(w+1+1,W-w-1):
            w += 1
    elif t == "U" :
        if min(h+1,H-h) < min(h-1+1,H-h+1):
            h -= 1
    else:
        if min(h+1,H-h) < min(h+1+1,H-h-1):
            h += 1
    now = [h,w]
    # print(now)


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()
mvs = {"L":0, "R":1,"U":2,"D":3}

# 高橋くんが選ぶ戦略 : どの方向に進んでいくか

def accX(S):
    accs = [[0,0,0,0]] #L,R,U,D
    for s in S[::-1]:
        l,r,u,d = accs[-1]
        if s == "L" : l += 1
        elif s == "R": r += 1
        elif s == "U" : u += 1
        else:d += 1
        accs.append([l,r,u,d])
    return accs[::-1]
accS = accX(S)
accT = accX(T)

now = [sr-1,sc-1]
for i,(s,t) in enumerate(zip(S,T)):
    h,w = now
    # 高橋
    if s == "L" : 
        if w - accS[i][0] < 0:
            w -= 1
    elif s == "R":
        if w + accS[i][1] > W:
            w += 1
    elif s == "U" :
        if h - accS[i][2] < 0:
            h -= 1
    else:
        if h + accS[i][3] > H:
            h += 1
    if w < 0 or w >= W or h < 0 or h >= H:
        print("NO")
        exit()
    # 青木
    if t == "L" : 
        if min(w+1,W-w) < min(w-1+1,W-w+1):
            w -= 1
    if t == "R" :
        if min(w+1,W-w) < min(w+1+1,W-w-1):
            w += 1
    elif t == "U" :
        if min(h+1,H-h) < min(h-1+1,H-h+1):
            h -= 1
    else:
        if min(h+1,H-h) < min(h+1+1,H-h-1):
            h += 1
    now = [h,w]
    # print(now)


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()
mvs = {"L":0, "R":1,"U":2,"D":3}

# 高橋くんが選ぶ戦略 : どの方向に進んでいくか

def accX(S):
    accs = [[0,0,0,0]] #L,R,U,D
    for s in S[::-1]:
        l,r,u,d = accs[-1]
        if s == "L" : l += 1
        elif s == "R": r += 1
        elif s == "U" : u += 1
        else:d += 1
        accs.append([l,r,u,d])
    return accs[::-1]
accS = accX(S)
accT = accX(T)

now = [sr-1,sc-1]
for i,(s,t) in enumerate(zip(S,T)):
    h,w = now
    # 高橋
    if s == "L" : 
        if w - accS[i][0] < 0:
            w -= 1
    elif s == "R":
        if w + accS[i][1] >= W:
            w += 1
    elif s == "U" :
        if h - accS[i][2] < 0:
            h -= 1
    else:
        if h + accS[i][3] >= H:
            h += 1
    if w < 0 or w >= W or h < 0 or h >= H:
        print("NO")
        exit()
    # 青木
    if t == "L" : 
        if min(w,W-w) <= min(w-1,W-w+1):
            w -= 1
    if t == "R" :
        if min(w,W-w) <= min(w+1,W-w-1):
            w += 1
    elif t == "U" :
        if min(h,H-h) <= min(h-1,H-h+1):
            h -= 1
    else:
        if min(h,H-h) <= min(h+1,H-h-1):
            h += 1
    now = [h,w]
    # print(now)


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()
mvs = {"L":0, "R":1,"U":2,"D":3}

# 高橋くんが選ぶ戦略 : どの方向に進んでいくか

def accX(S):
    accs = [[0,0,0,0]] #L,R,U,D
    for s in S[::-1]:
        l,r,u,d = accs[-1]
        if s == "L" : l += 1
        elif s == "R": r += 1
        elif s == "U" : u += 1
        else:d += 1
        accs.append([l,r,u,d])
    return accs[::-1]
accS = accX(S)
accT = accX(T)

now = [sr-1,sc-1]
for i,(s,t) in enumerate(zip(S,T)):
    h,w = now
    # 高橋
    if s == "L" : 
        if w - accS[i][0] < 0:
            w -= 1
    elif s == "R":
        if w + accS[i][1] >= W:
            w += 1
    elif s == "U" :
        if h - accS[i][2] < 0:
            h -= 1
    else:
        if h + accS[i][3] >= H:
            h += 1
    if w < 0 or w >= W or h < 0 or h >= H:
        print("NO")
        exit()
    # 青木
    if t == "L" : 
        if min(w,W-w) < min(w-1,W-w+1):
            w -= 1
    if t == "R" :
        if min(w,W-w) < min(w+1,W-w-1):
            w += 1
    elif t == "U" :
        if min(h,H-h) < min(h-1,H-h+1):
            h -= 1
    else:
        if min(h,H-h) < min(h+1,H-h-1):
            h += 1
    now = [h,w]
    # print(now)


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()
mvs = {"L":0, "R":1,"U":2,"D":3}

# 高橋くんが選ぶ戦略 : どの方向に進んでいくか

def accX(S):
    accs = [[0,0,0,0]] #L,R,U,D
    for s in S[::-1]:
        l,r,u,d = accs[-1]
        if s == "L" : l += 1
        elif s == "R": r += 1
        elif s == "U" : u += 1
        else:d += 1
        accs.append([l,r,u,d])
    return accs[::-1]
accS = accX(S)
accT = accX(T)

now = [sr-1,sc-1]
for i,(s,t) in enumerate(zip(S,T)):
    h,w = now
    # 高橋
    if s == "L" : 
        if w - accS[i][0] < 0:
            w -= 1
    elif s == "R":
        if w + accS[i][1] > W:
            w += 1
    elif s == "U" :
        if h - accS[i][2] < 0:
            h -= 1
    else:
        if h + accS[i][3] > H:
            h += 1
    if w < 0 or w >= W or h < 0 or h >= H:
        print("NO")
        exit()
    # 青木
    if t == "L" : 
        if min(w,W-w) < min(w-1,W-w+1):
            w -= 1
    if t == "R" :
        if min(w,W-w) < min(w+1,W-w-1):
            w += 1
    elif t == "U" :
        if min(h,H-h) < min(h-1,H-h+1):
            h -= 1
    else:
        if min(h,H-h) < min(h+1,H-h-1):
            h += 1
    now = [h,w]
    # print(now)


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()
mvs = {"L":0, "R":1,"U":2,"D":3}

# 高橋くんが選ぶ戦略 : どの方向に進んでいくか

def accX(S):
    accs = [[0,0,0,0]] #L,R,U,D
    for s in S[::-1]:
        l,r,u,d = accs[-1]
        if s == "L" : l += 1
        elif s == "R": r += 1
        elif s == "U" : u += 1
        else:d += 1
        accs.append([l,r,u,d])
    return accs[::-1]
accS = accX(S)
accT = accX(T)

now = [sr,sc]
for i,(s,t) in enumerate(zip(S,T)):
    h,w = now
    if s == "L" : 
        if w - accS[i][0] < 0:
            w -= 1
    elif s == "R":
        if w + accS[i][1] > W:
            w += 1
    elif s == "U" :
        if h - accS[i][2] < 0:
            h -= 1
    else:
        if h + accS[i][3] > H:
            h += 1
    if w < 0 or w > W or h < 0 or h > H:
        print("NO")
        exit()
    # 青木
    if t == "L" : 
        if min(w,W-w) < min(w-1,W-w+1):
            w -= 1
    if t == "R" :
        if min(w,W-w) < min(w+1,W-w-1):
            w += 1
    elif t == "U" :
        if min(h,H-h) < min(h-1,H-h+1):
            h -= 1
    else:
        if min(h,H-h) < min(h+1,H-h-1):
            h += 1
    now = [h,w]
    # print(now)


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()
mvs = {"L":0, "R":1,"U":2,"D":3}

# 高橋くんが選ぶ戦略 : どの方向に進んでいくか

def accX(S):
    accs = [[0,0,0,0]] #L,R,U,D
    for s in S[::-1]:
        l,r,u,d = accs[-1]
        if s == "L" : l += 1
        elif s == "R": r += 1
        elif s == "U" : u += 1
        else:d += 1
        accs.append([l,r,u,d])
    return accs[::-1]
accS = accX(S)
accT = accX(T)

now = [sr,sc]
for i,(s,t) in enumerate(zip(S,T)):
    h,w = now
    if s == "L" : 
        if w - accS[i][0] < 0:
            w -= 1
    elif s == "R":
        if w + accS[i][1] > W:
            w += 1
    elif s == "U" :
        if h - accS[i][2] < 0:
            h -= 1
    else:
        if h + accS[i][3] > H:
            h += 1
    if w < 0 or w > W or h < 0 or h > H:
        print("NO")
        exit()
    # 青木
    if t == "L" : 
        if min(w,W-w) < min(w-1,W-w+1):
            w -= 1
    if t == "R" :
        if min(w,W-w) < min(w+1,W-w+1):
            w += 1
    elif t == "U" :
        if min(h,H-h) < min(h-1,H-h+1):
            h -= 1
    else:
        if min(h,H-h) < min(h+1,H-h+1):
            h += 1
    now = [h,w]
    # print(now)


print("YES")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d, x):
    if d=='L':
      r[L] = (min(max(0, r[L]-x), w-1) )
    if d=='R':
      r[R] = (min(max(0, r[R]+x), w-1) )
    if d=='U':
      r[U] = (min(max(0, r[U]-x), h-1) )
    if d=='D':
      r[D] = (min(max(0, r[D]+x), h-1) )
      
  def check():
    if r[L] >= r[R]:
      return False
    if r[U] >= r[D]:
      return False
    return True
  
  f(ls[0], 1)
  for s, t in zip(ls[1:], lt[1:]):
    f(t, -1)
    f(s, 1)
    if check():
      print('NO')
      return
  print('YES')

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d, x):
    if d=='L':
      r[L] = (min(max(0, r[L]-x), w-1) )
    if d=='R':
      r[R] = (min(max(0, r[R]+x), w-1) )
    if d=='U':
      r[U] = (min(max(0, r[U]-x), h-1) )
    if d=='D':
      r[D] = (min(max(0, r[D]+x), h-1) )
      
  def check():
    if r[L] >= r[R]:
      return False
    if r[U] >= r[D]:
      return False
    return True
  
  f(ls[0], 1)
  for s, t in zip(ls[1:], lt[1:]):
    f(s, 1)
    f(t, -1)
    if check():
      print('NO')
      return
  print('YES')
    
    
    

if __name__=='__main__':
  main()

Submission status: Wrong Answer

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

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

# 上にオーバーしてしまう場合
# 下にオーバーしてしまう場合
# 左にオーバーしてしまう場合
# 右にオーバーしてしまう場合

flag = True

# 上にオーバーしてしまう時
cnt_up = sx
# cnt_up>=sxとなったら終了
for i in range(n):
    if s[i] == "U":
        cnt_up += 1
        if cnt_up > h:
            flag = False
            break
    if t[i] == "D":
        if cnt_up > 1:
            cnt_up -= 1

# 下にオーバーしてしまう場合
cnt_down = sx

# cnt_down>=h-s+1となったら終わり
for i in range(n):
    if s[i] == "D":
        cnt_down -= 1
        if cnt_down <= 0:
            flag = False
            break
    if t[i] == "U":
        if cnt_down < h:
            cnt_down += 1


# 左にオーバーしてしまう時
cnt_left = sy
# cnt_left>=syとなったら終わり
for i in range(n):
    if s[i] == "L":
        cnt_left -= 1
        if cnt_left <= 0:
            flag = False
            break
    if t[i] == "R":
        if cnt_left < w:
            cnt_left += 1

# みぎにオーバーしてしまう時
cnt_right = sy
# cnt_right>=w-sy+1となったら終わり
for i in range(n):
    if s[i] == "R":
        cnt_right += 1
        if cnt_right > w:
            flag = False
            break
    if t[i] == "L":
        if cnt_right > 1:
            cnt_right -= 1


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

Submission status: Wrong Answer

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

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

# 上にオーバーしてしまう場合
# 下にオーバーしてしまう場合
# 左にオーバーしてしまう場合
# 右にオーバーしてしまう場合

flag = True

# 上にオーバーしてしまう時
cnt_up = 0
# cnt_up>=sxとなったら終了
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 > 0:
            cnt_up -= 1

# 下にオーバーしてしまう場合
cnt_down = 0

# cnt_down>=h-s+1となったら終わり
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 > 0:
            cnt_down -= 1


# 左にオーバーしてしまう時
cnt_left = 0
# cnt_left>=syとなったら終わり
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 > 0:
            cnt_left -= 1

# みぎにオーバーしてしまう時
cnt_right = 0
# cnt_right>=w-sy+1となったら終わり
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 > 0:
            cnt_right -= 1


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

Submission status: Wrong Answer

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

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

# 上にオーバーしてしまう場合
# 下にオーバーしてしまう場合
# 左にオーバーしてしまう場合
# 右にオーバーしてしまう場合

flag = True

# 上にオーバーしてしまう時
cnt_up = 0
# cnt_up>=sxとなったら終了
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

# cnt_down>=h-s+1となったら終わり
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
# cnt_left>=syとなったら終わり
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
# cnt_right>=w-sy+1となったら終わり
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")

Submission status: Wrong Answer

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

slist_lr,slist_ud=[],[]
tlist_lr,tlist_ud=[],[]
for i in range(N):
  if S[i]=="L":
    slist_lr.append(-1)
    slist_ud.append(0)
  elif S[i]=="R":
    slist_lr.append(1)
    slist_ud.append(0)
  elif S[i]=="U":
    slist_lr.append(0)
    slist_ud.append(-1)
  elif S[i]=="D":
    slist_lr.append(0)
    slist_ud.append(1)

  if T[i]=="L":
    tlist_lr.append(-1)
    tlist_ud.append(0)
  elif T[i]=="R":
    tlist_lr.append(1)
    tlist_ud.append(0)
  elif T[i]=="U":
    tlist_lr.append(0)
    tlist_ud.append(-1)
  elif T[i]=="D":
    tlist_lr.append(0)
    tlist_ud.append(1)

print(slist_lr)
print(slist_ud)
print(tlist_lr)
print(tlist_ud)

Submission status: Wrong Answer

Solution code:

import numpy as np
from functools import *

import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline


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


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



H, W, NN = acinput()

initp= np.array(acinput())

S = list(input())[:-1]
T = list(input())[:-1]
#S = list(input().split(""))
#T = list(input().split(""))


def position_relative(direction,direction_op,cs,flg,seq1,seq2):

    N=len(seq1)
    for i in range(len(seq1)):
        s1=seq1[i]
        s2=seq2[i]
        if s1==direction:
            cs += flg
            
        if cs<=0 or cs>N:
            return False
        if s2==direction_op:
            cs-=flg
        
        if cs <= 0 or cs > N:
            return False
            
    return cs



sr=position_relative("R","L",initp[0],1,S,T)
sl = position_relative("L","R",initp[0],-1,S,T)

su = position_relative("U","D",initp[1],-1,S, T)
sd = position_relative("D","U",initp[1],1, S, T)

#print(sr,sl,su,sd)

#su=0
#if sr or sl or su or sd:
if sr*sl*su*sd==0:
    print("NO")
else:
    print("YES")

Submission status: Wrong Answer

Solution code:

import numpy as np
from functools import *

import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline


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


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



H, W, N = acinput()

initp= np.array(acinput())

S = list(input())[:-1]
T = list(input())[:-1]
#S = list(input().split(""))
#T = list(input().split(""))


def position_relative(direction,direction_op,cs,flg,seq1,seq2):

    N=len(seq1)
    for i in range(len(seq1)):
        s1=seq1[i]
        s2=seq2[i]
        if s1==direction:
            cs += flg
            
        if cs<=0 or cs>N:
            return False
        if s2==direction_op:
            cs-=flg
        
        if cs <= 0 or cs > N:
            return False
            
    return cs



sr=position_relative("R","L",initp[0],1,S,T)
sl = position_relative("L","R",initp[0],-1,S,T)

su = position_relative("U","D",initp[1],-1,S, T)
sd = position_relative("D","U",initp[1],1, S, T)

#print(sr,sl,su,sd)

#if sr or sl or su or sd:
if sr*sl*su*sd:
    print("NO")
else:
    print("YES")