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

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    l, r, u, d = 0, w + 1, 0, h + 1

    for i in range(n - 1, -1, -1):
        if t[i] == "U":
            d += 1
            d = min(d, h)
        if t[i] == "D":
            u -= 1
            u = max(u, 1)
        if t[i] == "L":
            r += 1
            r = min(r, w)
        if t[i] == "R":
            l -= 1
            l = max(l, 1)
        if s[i] == "U":
            u += 1
        if s[i] == "D":
            d -= 1
        if s[i] == "L":
            l += 1
        if s[i] == "R":
            r -= 1

    if sc <= l or r <= sc or sc <= u or d <= sc:
        print("NO")
    else:
        print("YES")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    H, W, N = list(map(int, input().split()))
    sr, sc = list(map(lambda x: int(x) - 1, input().split()))
    S = input()
    T = input()
    # 例；左端から出るまでto_leftマス、右端から出るまでto_rightマスのところにいる場合、
    #   高橋君のR - min(青木君のL, to_left - 1) >= to_right なら高橋君はマスを右端から外に出せる('NO')
    to_left, to_right = sc + 1, W - sc
    to_up, to_down = sr + 1, H - sr
    r_taka, l_aoki = 0, 0
    l_taka, r_aoki = 0, 0
    u_taka, d_aoki = 0, 0
    d_taka, u_aoki = 0, 0
    for s_taka, s_aoki in zip(S, T):
        if s_taka == 'R':
            r_taka += 1
        if s_taka == 'L':
            l_taka += 1
        if s_taka == 'U':
            u_taka += 1
        if s_taka == 'D':
            d_taka += 1
        if r_taka - min(l_aoki, to_left - 1) >= to_right or \
                l_taka - min(r_aoki, to_right - 1) >= to_left or \
                u_taka - min(d_aoki, to_down - 1) >= to_up or \
                d_taka - min(u_aoki, to_up - 1) >= to_down:
            print('NO')
            return
        if s_aoki == 'R':
            r_aoki += 1
        if s_aoki == 'L':
            l_aoki += 1
        if s_aoki == 'U':
            u_aoki += 1
        if s_aoki == 'D':
            d_aoki += 1
    print('YES')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, n = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

    left, right = 0, W
    up, bottom = 0, H
    for s, t in zip(S, T):
        if t == "R":
            left -= 1
            left = max(0, left)
        elif t == "L":
            right += 1
            right = min(W, right)
        elif t == "U":
            bottom += 1
            bottom = min(H, bottom)
        else:
            up -= 1
            up = max(0, up)

        if s == "R":
            right -= 1
        elif s == "L":
            left += 1
        elif s == "U":
            up += 1
        else:
            bottom -= 1

        if left >= right or up >= bottom:
            print("NO")
            break
    else:
        print("YES" if left <= sc <= right and up <= H - sr + 1 <= bottom else "NO")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, n = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

    left, right = 0, W
    up, bottom = 0, H
    for s, t in zip(S, T):
        if t == "R":
            left -= 1
            left = max(0, left)
        elif t == "L":
            right += 1
            right = min(W, right)
        elif t == "U":
            bottom += 1
            bottom = min(H, bottom)
        else:
            up -= 1
            up = max(0, up)

        if s == "R":
            right -= 1
        elif s == "L":
            left += 1
        elif s == "U":
            up += 1
        else:
            bottom -= 1

        if left >= right or up >= bottom:
            print("NO")
            break
    else:
        print("YES" if left <= sr <= right and up <= H - sc <= bottom else "NO")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, n = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

    left, right = 0, W
    bottom, up = 0, H
    for s, t in zip(S, T):
        if t == "R":
            left -= 1
            left = max(0, left)
        elif t == "L":
            right += 1
            right = min(W, right)
        elif t == "U":
            bottom -= 1
            bottom = max(0, bottom)
        else:
            up += 1
            up = max(H, up)

        if s == "R":
            right -= 1
        elif s == "L":
            left += 1
        elif s == "U":
            up -= 1
        else:
            bottom += 1

        if left >= right or bottom >= up:
            print("NO")
            break
    else:
        print("YES" if left <= sr <= right and bottom <= sc <= up else "NO")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

sr,sc = sr-1, sc-1
xmin, xmax, ymin, ymax = 0,W-1, 0,H-1
S = S[::-1]
T = T[::-1]

d = {
  'L': (1,0),
  'R': (-1,0),
  'U': (0,-1),
  'D': (0,1),
}
 
for i in range(N):
  dx,dy = d[T[i]]
  xmin = min(xmin, xmin + dx) if xmin + dx >= 0 else xmin
  xmax = max(xmax, xmax + dx) if xmax + dx < W else xmax
  ymin = min(ymin, ymin + dy) if ymin + dy >= 0 else ymin
  ymax = max(ymax, ymax + dy) if ymax + dy < W else ymax
  
  dx,dy = d[S[i]]
  xmin = max(xmin + dx, xmin) 
  xmax = min(xmax + dx, xmax)
  ymin = max(ymin + dy, ymin)
  ymax = min(ymax + dy, ymax)
  
  if xmin < 0: xmin = 0
  if xmax >= W: xmax = W-1
  if ymin < 0: ymin = 0
  if ymax >= H: ymax = H-1
    
  #print(i,xmin,xmax,ymin,ymax)
  if xmin > xmax or ymin > ymax:
    print("NO")
    exit(0)

if xmin <= sr <= xmax and ymin <= sc <= ymax:
  print("YES")
else:
  print("NO")

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    h, w, n = ns()
    y, x = ns()
    s = list(input())
    t = list(input())

    su, sd, sl, sr = 0, 0, 0, 0
    tu, td, tl, tr = 0, 0, 0, 0

    yes = True

    for si, ti in zip(s, t):
        if si == "U":
            su += 1
        elif si == "D":
            sd += 1
        elif si == "L":
            sl += 1
        else:
            sr += 1

        if y - (su - td) < 1:
            yes = False
        if y + sd - tu > h:
            yes = False
        if x - (sl - tr) < 1:
            yes = False
        if x + sr - tl > w:
            yes = False

        if ti == "U":
            tu += 1
        elif ti == "D":
            td += 1
        elif ti == "L":
            tl += 1
        else:
            tr += 1

    print("YES" if yes else "NO")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    h, w, n = ns()
    sr, sc = ns()
    s = list(input())
    t = list(input())

    su, sd, sl, sr = 0, 0, 0, 0
    tu, td, tl, tr = 0, 0, 0, 0

    yes = True

    for si, ti in zip(s, t):
        if si == "U":
            su += 1
        elif si == "D":
            sd += 1
        elif si == "L":
            sl += 1
        else:
            sr += 1

        if sr - (su - td) < 1:
            yes = False
        if sr + sd - tu > h:
            yes = False
        if sc - (sl - tr) < 1:
            yes = False
        if sc + sr - tl > w:
            yes = False

        if ti == "U":
            tu += 1
        elif ti == "D":
            td += 1
        elif ti == "L":
            tl += 1
        else:
            tr += 1

    print("YES" if yes else "NO")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===

def main():
    h, w, n = ns()
    sr, sc = ns()
    s = list(input())
    t = list(input())

    su, sd, sl, sr = 0, 0, 0, 0
    tu, td, tl, tr = 0, 0, 0, 0

    yes = True

    for si, ti in zip(s, t):
        if si == "U":
            su += 1
        elif si == "D":
            sd += 1
        elif si == "L":
            sl += 1
        else:
            sr += 1

        if su - td >= sr:
            yes = False
        if sd - tu >= h - sr + 1:
            yes = False
        if sl - tr >= sc:
            yes = False
        if sr - tl >= w - sc + 1:
            yes = False

        if ti == "U":
            tu += 1
        elif ti == "D":
            td += 1
        elif ti == "L":
            tl += 1
        else:
            tr += 1

    print("YES" if yes else "NO")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# D : sr
# U : H - sr + 1
# L : sc
# R : W - sc + 1
# Takahashi, Aoki, Count, max]
pat = [
  ["D", "U", sr, H],
  ["U", "D", H-sr+1, H],
  ["L", "R", sc, W],
  ["R", "L", W-sc+1, W]
]
def fin(c):
  if c<=0:
    print("NO")
    exit(0)
  return
  
for t,a,c,m in pat:
  #print(t,a,c,m)
  for i in range(N):
    #print(c)
    if S[i] == t:
      c -= 1
      fin(c)
    if T[i] == a and c < m:
      c += 1
    
print("YES")

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

    left, right = 1, W
    up, bottom = 1, H
    for s, t in zip(S, T):
        if t == "R":
            left -= 1
            left = max(1, left)
        elif t == "L":
            right += 1
            right = min(W, right)
        elif t == "U":
            up -= 1
            up = max(1, up)
        elif t == "D":
            bottom += 1
            bottom = min(H, bottom)

        if s == "R":
            right -= 1
        elif s == "L":
            left += 1
        elif s == "U":
            bottom -= 1
        elif s == "D":
            up += 1

        if left > right or up > bottom:
            print("NO")
            exit()

    print("YES" if left <= sc <= right and up <= sr <= bottom else "NO")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

    left, right = 1, W
    up, bottom = 1, H
    for s, t in zip(S, T):
        if t == "R":
            left -= 1
            left = max(1, left)
        elif t == "L":
            right += 1
            right = min(W, right)
        elif t == "U":
            up -= 1
            up = max(1, up)
        elif t == "D":
            bottom += 1
            bottom = min(H, bottom)

        if s == "R":
            right -= 1
            right = max(1, right)
        elif s == "L":
            left += 1
            left = min(H, left)
        elif s == "U":
            bottom -= 1
            bottom = max(1, bottom)
        elif s == "D":
            up += 1
            up = min(H, up)

        if left > right or up > bottom:
            print("NO")
            exit()

    print("YES" if left <= sc <= right and up <= sr <= bottom else "NO")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

    left, right = 1, W
    bottom, up = 1, H
    for s, t in zip(S, T):
        if t == "R":
            left -= 1
            left = max(1, left)
        elif t == "L":
            right += 1
            right = min(W, right)
        elif t == "U":
            up -= 1
            up = max(1, up)
        elif t == "D":
            bottom += 1
            bottom = min(H, bottom)

        if s == "R":
            right -= 1
            right = max(1, right)
        elif s == "L":
            left += 1
            left = min(H, left)
        elif s == "U":
            bottom -= 1
            bottom = max(1, bottom)
        elif s == "D":
            up += 1
            up = min(H, up)

        if left > right or up > bottom:
            print("NO")
            exit()

    print("YES" if left <= sc <= right and up <= sr <= bottom else "NO")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

    left, right = 1, W
    up, bottom = 1, H
    for s, t in zip(S, T):
        if t == "R":
            left -= 1
            left = max(1, left)
        elif t == "L":
            right += 1
            right = min(W, right)
        elif t == "U":
            up -= 1
            up = max(1, bottom)
        elif t == "D":
            bottom += 1
            bottom = min(H, bottom)

        if s == "R":
            right -= 1
            right = max(1, right)
        elif s == "L":
            left += 1
            left = min(H, left)
        elif s == "U":
            bottom -= 1
            bottom = max(1, bottom)
        elif s == "D":
            up += 1
            up = min(H, up)

        if left > right or up > bottom:
            print("NO")
            exit()

    print("YES" if left <= sc <= right and up <= sr <= bottom else "NO")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    left, right = 1, W
    for i in reversed(range(N)):
        if i != N - 1:
            if T[i] == "L":
                    right += 1
                    right = min(W, right)
            elif T[i] == "R":
                    left -= 1
                    left = max(1, left)
        if S[i] == "L":
                left += 1
                left = min(W, left)
        elif S[i] == "R":
                right -= 1
                right = max(1, right)
        if not (left <= sr <= right):
            print("NO")
            exit()

    up, bottom = 1, H
    for i in reversed(range(N)):
        if i != N - 1:
            if T[i] == "U":
                bottom += 1
                bottom = min(H, bottom)
            elif T[i] == "D":
                up -= 1
                up = max(1, up)
        if S[i] == "U":
            up += 1
            up = min(H, up)
        elif S[i] == "D":
            bottom -= 1
            bottom = max(1, bottom)
        if not (up <= sc <= bottom):
            print("NO")
            exit()

    print("YES")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

H,W,N = IL()
sy,sx = IL()
A = S()
B = S()

X = [sx,sx]
Y = [sy,sy]
for i in range(N):
  a = A[i]
  if a == "L":
    if X[0] == 1:
      print("NO")
      exit()
    else:
      X[0] -= 1
  if a == "R":
    if X[1] == W:
      print("NO")
      exit()
    else:
      X[1] += 1
  if a == "U":
    if Y[0] == 1:
      print("NO")
      exit()
    else:
      Y[0] -= 1
  if a == "D":
    if Y[1] == H:
      print("NO")
      exit()
    else:
      Y[1] += 1
  a = B[i]
  if a == "L":
    X[1] = max(X[0],X[1]-1)
  if a == "R":
    X[0] = min(X[1],X[0]+1)
  if a == "U":
    Y[1] = max(Y[0],Y[1]-1)
  if a == "D":
    Y[0] = min(Y[1],Y[0]+1)
  
print("YES")

Submission status: Wrong Answer

Solution code:
# 以下の貪欲法は嘘解法（通るらしい）。
# 「高橋くんは右方向に落とすように、なるべく右を選ぶ。青木くんは落ちない範囲で左を選ぶ。これで落ちるか落ちないか判定する。
# これを上下左右で繰り返し、落ちなければ」
# 盤面が縦が十分大きい×横3、初期位置が中央、
# UULLRR
# RLUUUU
# は正解NOだが、貪欲でYESになってしまう。

# https://www.youtube.com/watch?v=Rr5ul_LDMiw
# コメントより：
# 3×3中央で、
# DRLL
# RUDD
# 初手青木くんがRなら高橋くんはそのままRするし、でなければLLしてどのみち外に出せる。

h, w, n = list(map(int, input().split()))
row, column = list(map(int, input().split()))
s_sente = input()
s_gote = input()

ans = [0, h-1, 0, w-1]
for i in reversed(range(n)):
    g = s_gote[i]
    if g == 'U':
        ans[1] = min(ans[1] + 1, h-1)
    elif g == 'D':
        ans[0] = max(ans[0] - 1, 0)
    elif g == 'L':
        ans[3] = min(ans[3] + 1, w-1)
    elif g == 'R':
        ans[2] = max(ans[2] - 1, 0)

    s = s_sente[i]
    if s == 'U':
        ans[0] = ans[0] + 1
    elif s == 'D':
        ans[1] = ans[1] - 1
    elif s == 'L':
        ans[2] = ans[2] + 1
    elif s == 'R':
        ans[3] = ans[3] - 1
    
    if not (ans[0] <= ans[1] and ans[2] <= ans[3]):
        print('NO')
        exit()
    # print(ans)

if ans[0] <= row <= ans[1] and ans[2] <= column <= ans[3]:
    # 「最後までコマが残るような初期配置の範囲」に、所与の初期配置が含まれる
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
# 以下の貪欲法は嘘解法（通るらしい）。
# 「高橋くんは右方向に落とすように、なるべく右を選ぶ。青木くんは落ちない範囲で左を選ぶ。これで落ちるか落ちないか判定する。
# これを上下左右で繰り返し、落ちなければ」
# 盤面が縦が十分大きい×横3、初期位置が中央、
# UULLRR
# RLUUUU
# は正解NOだが、貪欲でYESになってしまう。

# https://www.youtube.com/watch?v=Rr5ul_LDMiw
# コメントより：
# 3×3中央で、
# DRLL
# RUDD
# 初手青木くんがRなら高橋くんはそのままRするし、でなければLLしてどのみち外に出せる。

h, w, n = list(map(int, input().split()))
row, column = list(map(int, input().split()))
s_sente = input()
s_gote = input()

ans = [0, h-1, 0, w-1]
for i in reversed(range(n)):
    g = s_gote[i]
    if g == 'U':
        ans[1] = min(ans[1] + 1, h-1)
    elif g == 'D':
        ans[0] = max(ans[0] - 1, 0)
    elif g == 'R':
        ans[3] = min(ans[3] + 1, w-1)
    elif g == 'L':
        ans[2] = max(ans[2] - 1, 0)

    s = s_sente[i]
    if s == 'U':
        ans[0] = ans[0] + 1
    elif s == 'D':
        ans[1] = ans[1] - 1
    elif s == 'R':
        ans[2] = ans[2] + 1
    elif s == 'L':
        ans[3] = ans[3] - 1
    
    if not (ans[0] <= ans[1] and ans[2] <= ans[3]):
        print('NO')
        exit()

if ans[0] <= row <= ans[1] and ans[2] <= column <= ans[3]:
    # 「最後までコマが残るような初期配置の範囲」に、所与の初期配置が含まれる
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
# 以下の貪欲法は嘘解法（通るらしい）。
# 「高橋くんは右方向に落とすように、なるべく右を選ぶ。青木くんは落ちない範囲で左を選ぶ。これで落ちるか落ちないか判定する。
# これを上下左右で繰り返し、落ちなければ」
# 盤面が縦が十分大きい×横3、初期位置が中央、
# UULLRR
# RLUUUU
# は正解NOだが、貪欲でYESになってしまう。

# https://www.youtube.com/watch?v=Rr5ul_LDMiw
# コメントより：
# 3×3中央で、
# DRLL
# RUDD
# 初手青木くんがRなら高橋くんはそのままRするし、でなければLLしてどのみち外に出せる。

h, w, n = list(map(int, input().split()))
row, column = list(map(int, input().split()))
s_sente = input()
s_gote = input()

ans = [0, h-1, 0, w-1]
for i in reversed(range(n)):
    g = s_gote[i]
    if g == 'U':
        ans[1] = min(ans[1] + 1, h-1)
    elif g == 'D':
        ans[0] = max(ans[0] - 1, 0)
    elif g == 'R':
        ans[3] = min(ans[3] + 1, w-1)
    elif g == 'L':
        ans[2] = max(ans[2] - 1, 0)

    s = s_sente
    if s == 'U':
        ans[0] = max(ans[0] + 1, h-1)
    elif s == 'D':
        ans[1] = min(ans[1] - 1, 0)
    elif s == 'R':
        ans[2] = max(ans[2] + 1, w-1)
    elif s == 'L':
        ans[3] = min(ans[3] - 1, 0)
    
    if not (ans[0] <= ans[1] and ans[2] <= ans[3]):
        print('NO')
        exit()

if ans[0] <= row-1 <= ans[1] and ans[2] <= column-1 <= ans[3]:
    # 「最後までコマが残るような初期配置の範囲」に、所与の初期配置が含まれる
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
# 以下の貪欲法は嘘解法（通るらしい）。
# 「高橋くんは右方向に落とすように、なるべく右を選ぶ。青木くんは落ちない範囲で左を選ぶ。これで落ちるか落ちないか判定する。
# これを上下左右で繰り返し、落ちなければ」
# 盤面が縦が十分大きい×横3、初期位置が中央、
# UULLRR
# RLUUUU
# は正解NOだが、貪欲でYESになってしまう。

# https://www.youtube.com/watch?v=Rr5ul_LDMiw
# コメントより：
# 3×3中央で、
# DRLL
# RUDD
# 初手青木くんがRなら高橋くんはそのままRするし、でなければLLしてどのみち外に出せる。

h, w, n = list(map(int, input().split()))
row, column = list(map(int, input().split()))
s_sente = input()
s_gote = input()

ans = [0, h-1, 0, w-1]
for i in reversed(range(n)):
    g = s_gote[i]
    if g == 'U':
        ans[1] = min(ans[1] + 1, h-1)
    elif g == 'D':
        ans[0] = max(ans[0] - 1, 0)
    elif g == 'R':
        ans[3] = min(ans[3] + 1, w-1)
    elif g == 'L':
        ans[2] = max(ans[2] - 1, 0)

    s = s_sente
    if s == 'U':
        ans[0] = ans[0] + 1
    elif s == 'D':
        ans[1] = ans[1] - 1
    elif s == 'R':
        ans[2] = ans[2] + 1
    elif s == 'L':
        ans[3] = ans[3] - 1
    
    if not (ans[0] <= ans[1] and ans[2] <= ans[3]):
        print('NO')
        exit()

if ans[0] <= row <= ans[1] and ans[2] <= column <= ans[3]:
    # 「最後までコマが残るような初期配置の範囲」に、所与の初期配置が含まれる
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
# 以下の貪欲法は嘘解法（通るらしい）。
# 「高橋くんは右方向に落とすように、なるべく右を選ぶ。青木くんは落ちない範囲で左を選ぶ。これで落ちるか落ちないか判定する。
# これを上下左右で繰り返し、落ちなければ」
# 盤面が縦が十分大きい×横3、初期位置が中央、
# UULLRR
# RLUUUU
# は正解NOだが、貪欲でYESになってしまう。

# https://www.youtube.com/watch?v=Rr5ul_LDMiw
# コメントより：
# 3×3中央で、
# DRLL
# RUDD
# 初手青木くんがRなら高橋くんはそのままRするし、でなければLLしてどのみち外に出せる。

h, w, n = list(map(int, input().split()))
row, column = list(map(int, input().split()))
s_sente = input()
s_gote = input()

ans = [0, h-1, 0, w-1]
for i in reversed(range(n)):
    g = s_gote[i]
    if g == 'U':
        ans[1] = min(ans[1] + 1, h-1)
    elif g == 'D':
        ans[0] = max(ans[0] - 1, 0)
    elif g == 'R':
        ans[3] = min(ans[3] + 1, w-1)
    elif g == 'L':
        ans[2] = max(ans[2] - 1, 0)

    s = s_sente
    if s == 'U':
        ans[0] = ans[0] + 1
    elif s == 'D':
        ans[1] = ans[1] - 1
    elif s == 'R':
        ans[2] = ans[2] + 1
    elif s == 'L':
        ans[3] = ans[3] - 1

if ans[0] <= row <= ans[1] and ans[2] <= column <= ans[3]:
    # 「最後までコマが残るような初期配置の範囲」に、所与の初期配置が含まれる
    print('NO')
else:
    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()

#最後の状態から、後手が勝てる範囲をさかのぼって見ていく
nL, nR = 1, W
nD, nU = 1, H

for s,t in zip(S[::-1],T[::-1]):
    # 青木くんは自分の領土を広げる方向
    # 動かせる駒の逆方向に領土を広げられる
    if t == "R" :nL -= 1
    elif t == "L": nR += 1
    elif t == "U": nD -= 1
    elif t == "D": nU += 1

    # 青木くんはマスの領域以上には広がらない
    if nL < 1: nL = 1
    if nR > W: nR = W
    if nD < 1: nD = 1
    if nU > H: nU = H

    # 高橋くんは領土を縮める方向
    if s == "R" :nR -= 1
    elif s == "L": nL += 1
    elif s == "U": nU -= 1
    elif s == "D": nD += 1


    if nL > nR or nD > nU:
        print("YES")
        exit()

if (nL <= sc <= nR) and (nD <= sr <= nU):
    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()

#最後の状態から、後手が勝てる範囲をさかのぼって見ていく
nL, nR = 1, W
nD, nU = 1, H

for s,t in zip(S[::-1],T[::-1]):
    # 青木くんは自分の領土を広げる方向
    # 動かせる駒の逆方向に領土を広げられる
    if t == "R" :nL -= 1
    elif t == "L": nR += 1
    elif t == "U": nD -= 1
    elif t == "D": nU += 1

    # 青木くんはマスの領域以上には広がらない
    if nL < 1: nL = 1
    if nR > W: nR = W
    if nD < 1: nD = 1
    if nU > H: nU = H

    # 高橋くんは領土を縮める方向
    if s == "R" :nR -= 1
    elif s == "L": nL += 1
    elif s == "U": nU -= 1
    elif s == "D": nD += 1


    if nL > nR or nD > nU:
        print("NO")
        exit()

if (nL <= sc <= nR) and (nD <= sr <= nU):
    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()

#最後の状態から、後手が勝てる範囲をさかのぼって見ていく
nL, nR = 1, W
nD, nU = 1, H

for s,t in zip(S[::-1],T[::-1]):
    # 青木くんは自分の領土を広げる方向
    # 動かせる駒の逆方向に領土を広げられる
    if t == "R" :nL -= 1
    elif t == "L": nR += 1
    elif t == "U": nD -= 1
    elif t == "D": nU += 1

    # 青木くんはマスの領域以上には広がらない
    if nL < 1: nL = 1
    if nR > W: nR = W
    if nU < 1: nU = 1
    if nD > H: nD = H

    # 高橋くんは領土を縮める方向
    if s == "R" :nR -= 1
    elif s == "L": nL += 1
    elif s == "U": nU -= 1
    elif s == "D": nD += 1


    if nL > nR or nD > nU:
        print("YES")
        exit()

if (nL <= sc <= nR) and (nD <= sr <= nU):
    print("YES")
else:
    print("NO")