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:
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("NO")
        exit()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    H, W, N = map(int, input().split())
    sh, sw = map(int, input().split())
    S = input()
    T = input()
    count_S = defaultdict(int)
    count_T = defaultdict(int)

    for i in range(N):
        s = S[i]
        t = T[i]

        left = min(sw - (count_S["L"] - count_T["R"]), sw)
        right = max(sw + count_S["R"] - count_T["L"], sw)
        top = min(sh - (count_S["U"] - count_T["D"]), sh)
        bottom = max(sh + count_S["D"] - count_T["U"], sh)

        if s == "L":
            if left - 1 == 0:
                print("NO")
                return
        elif s == "R":
            if right == W:
                print("NO")
                return
        elif s == "U":
            if top - 1 == 0:
                print("NO")
                return
        elif s == "D":
            if bottom == H:
                print("NO")
                return

        count_S[s] += 1

        right = max(sw - (count_S["L"] - count_T["R"]), sw)
        left = min(sw + count_S["R"] - count_T["L"], sw)
        bottom = max(sh - (count_S["U"] - count_T["D"]), sh)
        top = min(sh + count_S["D"] - count_T["U"], sh)

        if t == "L":
            if left - 1 == 0:
                continue
        elif t == "R":
            if right + 1 == 0:
                continue
        elif t == "U":
            if top - 1 == 0:
                continue
        elif t == "D":
            if bottom + 1 == 0:
                continue

        count_T[t] += 1

    print("YES")


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

L0 = [0, 0, 0, 0] #LRUD
L1 = [0, 0, 0, 0]
L2 = {'R':0, 'L':1, 'D':2, 'U':3}
L3 = {'L':0, 'R':1, 'U':2, 'D':3}

for i in range(N-1, -1, -1):
    cnt = [0] * 4
    if i != N-1:
        cnt[L2[T[i]]] -= 1
    cnt[L3[S[i]]] += 1
    for j in range(4):
        L1[j] += cnt[j]
        L0[j] = max(L0[j], L1[j])

if L0[0] < sc <= W - L0[1] and L0[2] < sr <= H - L0[3]:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

tmove=[(0,-1),(0,1),(1,0),(-1,0)]
amove=[(0,1),(0,-1),(-1,0),(1,0)]
m=[('L','R'),('R','L'),('U','D'),('D','U')]

for i in range(4):
    nh,nw=sr,sc
    t,a=tmove[i],amove[i]
    for k in range(N):
        if S[k]==m[i][0]:
            nh,nw=nh+t[0],nw+t[1]
        if nh<0 or nw<0 or nh>=H or nw>=W:
            print('NO')
            exit()
        gh,gw=nh+a[0],nw+a[1]
        if T[k]==m[i][1]:
            if gh>=0 and gh<H and gw>=0 and gw<W:
                nh,nw=gh,gw
print('YES')

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
si, sj = map(int, input().split())
s = input()
t = input()


is_safe_lr = [True] * (w + 2)
l = 0
r = w + 1
for i in range(n)[::-1]:
    if t[i] == "R":
        l = max(l - 1, 0)
    if t[i] == "L":
        r = min(r + 1, w + 1)
    if s[i] == "L":
        l = min(l + 1, w + 1)
        is_safe_lr[l] = False
    if s[i] == "R":
        r = max(r - 1, 0)
        is_safe_lr[r] = False

is_safe_ud = [True] * (h + 2)
u = 0
d = h + 1
for i in range(n)[::-1]:
    if t[i] == "D":
        u = max(u - 1, 0)
    if t[i] == "U":
        d = min(d + 1, h + 1)
    if s[i] == "U":
        u = min(u + 1, h + 1)
        is_safe_ud[u] = False
    if s[i] == "D":
        d = max(d - 1, 0)
        is_safe_ud[d] = False

if is_safe_ud[si] and is_safe_lr[sj]:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
si, sj = map(int, input().split())
s = input()
t = input()


is_safe_lr = [True] * (w + 1)
l = 0
r = w + 1
for i in range(n)[::-1]:
    if t[i] == "R":
        l = max(l - 1, 0)
    if t[i] == "L":
        r = min(r + 1, w + 1)
    if s[i] == "L":
        l = min(l + 1, w + 1)
        is_safe_lr[l] = False
    if s[i] == "R":
        r = max(r - 1, 0)
        is_safe_lr[r] = False

is_safe_ud = [True] * (h + 1)
u = 0
d = h + 1
for i in range(n)[::-1]:
    if t[i] == "D":
        u = max(u - 1, 0)
    if t[i] == "U":
        d = min(d + 1, h + 1)
    if s[i] == "U":
        u = min(u + 1, h + 1)
        is_safe_ud[u] = False
    if s[i] == "D":
        d = max(d - 1, 0)
        is_safe_ud[d] = False

if is_safe_ud[si] and is_safe_lr[sj]:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
si, sj = map(int, input().split())
s = input()
t = input()


is_safe_lr = [True] * (w + 1)
l = 0
r = w + 1
for i in range(n)[::-1]:
    if t[i] == "L":
        l = max(l - 1, 0)
    if t[i] == "R":
        r = min(r + 1, w)
    if s[i] == "R":
        l = min(l + 1, w)
        is_safe_lr[l] = False
    if s[i] == "L":
        r = max(r - 1, 0)
        is_safe_lr[r] = False

is_safe_ud = [True] * (h + 1)
u = 0
d = h + 1
for i in range(n)[::-1]:
    if t[i] == "U":
        u = max(u - 1, 0)
    if t[i] == "D":
        d = min(d + 1, h)
    if s[i] == "D":
        u = min(u + 1, h)
        is_safe_ud[u] = False
    if s[i] == "U":
        d = max(d - 1, 0)
        is_safe_ud[d] = False

if is_safe_ud[si] and is_safe_lr[sj]:
    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()[::-1]
t=input()[::-1]
l,r,u,d=0,w+1,0,h+1
for i in range(n):
 si,ti=s[i],t[i]
 if l>r or u>d:break
 r=min(w+1,r+1)if ti=="L"else r
 l=max(0,l-1)if ti=="R"else l
 d=min(h+1,d+1)if ti=="U"else d
 u=max(0,u-1)if ti=="D"else u
 r-=1 if si=="R"else 0
 l+=1 if si=="L"else 0
 d-=1 if si=="D"else 0
 u+=1 if si=="U"else 0
print("YES"if l<sc<r and u<sr<d else"NO")

Submission status: Wrong Answer

Solution code:
h,w,n = list(map(int,input().split()))
sr,sc = list(map(int,input().split()))
s = input()
t = input()
kijyun = "LRUD"
ruisekiS = [{v:0 for v in kijyun} for i in range(n+1)]
ruisekiT = [{v:0 for v in kijyun} for i in range(n+1)]

for i in range(n):
    for c in kijyun:
        if s[i] == c:
            ruisekiS[i+1][c] = ruisekiS[i][c] + 1
        else:
            ruisekiS[i+1][c] = ruisekiS[i][c]
        if t[i] == c:
            ruisekiT[i+1][c] = ruisekiT[i][c] + 1
        else:
            ruisekiT[i+1][c] = ruisekiT[i][c]
kyori = {"L":sc,"R":w-sc+1,"U":sr,"D":h-sr+1}
gyaku = {"L":"R","R":"L","U":"D","D":"U"}

check = 0
for i in range(n):
    for c in kijyun:
        if s[i] == c:
            ruisekiS[i+1][c] = ruisekiS[i][c] + 1
        else:
            ruisekiS[i+1][c] = ruisekiS[i][c]
        if t[i] == c and (ruisekiT[i][c] + 1 - ruisekiS[i][gyaku[c]]) < kyori[c] :
            ruisekiT[i+1][c] = ruisekiT[i][c] + 1
        else:
            ruisekiT[i+1][c] = ruisekiT[i][c]
    if ruisekiS[i+1]["L"] - ruisekiT[i]["R"] >= kyori["L"]:
        check = 1
    if ruisekiS[i+1]["R"] - ruisekiT[i]["L"] >= kyori["R"]:
        check = 1
    if ruisekiS[i+1]["U"] - ruisekiT[i]["D"] >= kyori["U"]:
        check = 1
    if ruisekiS[i+1]["D"] - ruisekiT[i]["U"] >= kyori["D"]:
        check = 1
#print(ruisekiS)
#print(ruisekiT)
if check == 0:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sr,sc=map(lambda x:int(x)-1,input().split())
s=input()
t=input()
answ,ansh=[0,w-1],[0,h-1]
for i in range(n):
    if s[-i-1]=='R':
        answ[1]-=1
    if s[-i-1]=='L':
        answ[0]+=1
    if s[-i-1]=='D':
        ansh[1]-=1
    if s[-i-1]=='U':
        ansh[0]+=1
    if answ[0]>answ[1] or ansh[0]>ansh[1]:
        print('NO')
        exit()
    if i==n-1:
        break
    if t[-i-2]=='R':
        answ[0]=max(0,answ[0]-1)
    if t[-i-2]=='L':
        answ[1]=min(w-1,answ[1]+1)
    if t[-i-2]=='D':
        ansh[0]=max(0,ansh[0]-1)
    if t[-i-2]=='U':
        ansh[1]=min(h-1,answ[1]+1)
if answ[0]<=sc<=answ[1] and ansh[0]<=sr<=ansh[1]:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sr,sc=map(lambda x:int(x)-1,input().split())
s=input()
t=input()
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='R':
      sc0+=1
      if sc0>=w:
          print('NO')
          exit()
  if t[i]=='L':
      sc0=max(0,sc0-1)
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='L':
      sc0-=1
      if sc0<0:
          print('NO')
          exit()
  if t[i]=='R':
      sc0=min(w-1,sc0+1)
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='U':
      sr0+=1
      if sr0>=h:
          print('NO')
          exit()
  if t[i]=='D':
      sr0=max(0,sr0-1)
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='D':
      sr0-=1
      if sr0<0:
          print('NO')
          exit()
  if t[i]=='U':
      sr0=min(h-1,sr0+1)

print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sr,sc=map(lambda x:int(x)-1,input().split())
s=input()
t=input()
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='R':
      sc0+=1
      if sc0>=w:
          print('NO')
          exit()
  if t[i]=='L':
      sc0=max(0,sc0-1)
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='L':
      sc0-=1
      if sc0<0:
          print('NO')
          exit()
  if t[i]=='R':
      sc0=max(0,sc0-1)
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='U':
      sr0+=1
      if sr0>=h:
          print('NO')
          exit()
  if t[i]=='D':
      sr0=max(0,sr0-1)
sr0,sc0=sr,sc
for i in range(n):
  if s[i]=='D':
      sr0-=1
      if sr0<0:
          print('NO')
          exit()
  if t[i]=='U':
      sr0=max(0,sr0-1)

print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sr,sc=map(lambda x:int(x)-1,input().split())
s=input()
t=input()
d_s={'U':0,'D':0,'R':0,'L':0}
d_t={'U':0,'D':0,'R':0,'L':0}
for i in range(n):
  d_s[s[i]]+=1
  if d_s['U']-min(h-sr,d_t['D'])>sr:
    print('NO')
    #print(i,'U')
    exit()
  if d_s['D']-min(sr,d_t['U'])>h-1-sr:
    print('NO')
    #rint(i,'D')
    exit()
  if d_s['L']-min(w-sc,d_t['R'])>sc:
    print('NO')
    #print(i,'L')
    exit()
  if d_s['R']-min(sc,d_t['L'])>w-1-sr:
    print(i,'R')
    #print('NO')
    exit()
  d_t[t[i]]+=1
print('YES')

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    H,W,N = LI()
    R,C = LI()
    S = SI()
    T = SI()

    x = C
    for i in range(N):
        x -= S[i] == 'L'
        if x <= 0:
            print('NO')
            return
        if x <= W:
            x += T[i] == 'R'

    x = C
    for i in range(N):
        x += S[i] == 'R'
        if x > W:
            print('NO')
            return
        if x > 1:
            x -= T[i] == 'L'

    x = R
    for i in range(N):
        x -= S[i] == 'U'
        if x <= 0:
            print('NO')
            return
        if x > 1:
            x += T[i] == 'D'

    x = R
    for i in range(N):
        x += S[i] == 'D'
        if x > H:
            print('NO')
            return
        if x <= H:
            x -= T[i] == 'U'

    print('YES')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['U'])+sd['D'] > h or min(h,r+td['D'])-sd['U'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  if t[i] == 'L' and c+sd['R'] >= w//2+1 and sd['R'] > 0:
    sd['R'] -= 1
  if t[i] == 'R' and c-sd['L'] <= w//2-1 and sd['L'] > 0:
    sd['L'] -= 1
  if t[i] == 'U' and r+sd['D'] >= h//2+1 and sd['U'] > 0:
    sd['U'] -= 1
  if t[i] == 'D' and r-sd['U'] <= h//2-1 and sd['D'] > 0:
    sd['D'] -= 1
  else:
    td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['U'])+sd['D'] > h or min(h,r+td['D'])-sd['U'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  if t[i] == 'L' and c+sd['R'] >= w//2+1:
    sd['R'] -= 1
  if t[i] == 'R' and c-sd['L'] <= w//2-1:
    sd['L'] -= 1
  if t[i] == 'U' and r+sd['D'] >= h//2+1:
    sd['U'] -= 1
  if t[i] == 'D' and r-sd['U'] <= h//2-1:
    sd['D'] -= 1
  else:
    td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['U'])+sd['D'] > h or min(h,r+td['D'])-sd['U'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  if t[i] == 'L' and s[i] == 'R':
    sd['R'] -= 1
  if t[i] == 'R' and s[i] == 'L':
    sd['L'] -= 1
  if t[i] == 'D' and s[i] == 'U':
    sd['U'] -= 1
  if t[i] == 'U' and s[i] == 'D':
    sd['D'] -= 1
  else:
    td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['U'])+sd['D'] > h or min(h,r+td['D'])-sd['U'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  if t[i] == 'L' and sd['R'] > 0:
    sd['R'] -= 1
  if t[i] == 'R' and sd['L'] > 0:
    sd['L'] -= 1
  if t[i] == 'D' and sd['U'] > 0:
    sd['U'] -= 1
  if t[i] == 'U' and sd['D'] > 0:
    sd['D'] -= 1
  else:
    td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['U'])+sd['D'] > h or min(h,r+td['D'])-sd['U'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w,n = map(int,input().rstrip().split())
r,c = map(int,input().rstrip().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input().rstrip()
t = input().rstrip()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['U'])+sd['D'] > h or min(h,r+td['D'])-sd['U'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w,n = map(int,input().rstrip().split())
r,c = map(int,input().rstrip().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input().rstrip()
t = input().rstrip()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['D'])+sd['U'] > h or min(h,r+td['U'])-sd['D'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'YES'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['D'])+sd['U'] > h or min(h,r+td['U'])-sd['D'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'NO'
    break
  td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w,n = map(int,input().split())
r,c = map(int,input().split())
sd = {'L':0,'R':0,'U':0,'D':0}
td = {'L':0,'R':0,'U':0,'D':0}
s = input()
t = input()
ans = 'Yes'
for i in range(n):
  sd[s[i]] += 1
  if max(1,r-td['D'])+sd['U'] > h or min(h,r+td['U'])-sd['D'] < 1 or max(1,c-td['L'])+sd['R'] > w or min(w,c+td['R'])-sd['L'] < 1:
    ans = 'No'
    break
  td[t[i]] += 1
print(ans)

Submission status: Wrong Answer

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

# 左か上に移動させたい
now = [Sr, Sc]
for s, t in zip(S, T):
  # 高橋くんのターン
  if s == 'L':
    now[1] -= 1
  if s == 'U':
    now[0] -= 1
  # 駒を取り除けたかチェック
  if now[0] <= 0 or now[1] <= 0:
    print('NO')
    quit()
  
  # 青木くんのターン
  if t == 'R':
    now[1] = min(now[1] + 1, W)
  if t == 'D':
    now[0] = min(now[0] + 1, H)

# 右か下に移動させたい
now = [Sr, Sc]
for s, t in zip(S, T):
  # 高橋くんのターン
  if s == 'R':
    now[1] += 1
  if s == 'D':
    now[0] += 1
  # 駒を取り除けたかチェック
  if H < now[0] or W < now[1]:
    print('NO')
    quit()
  
  # 青木くんのターン
  if t == 'L':
    now[1] = max(now[1] - 1, 1)
  if t == 'U':
    now[0] = min(now[0] - 1, 1)

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()
r,l,u,d = sc,sc,sr,sr
ans = "YES"
for i,j in zip(s,t):
  if i == "R":
    r+=1
    if r>w:
      ans = "NO"
      break
  elif i =="L":
    l-=1
    if l<1:
      ans = "NO"
      break
  elif i =="U":
    u-=1
    if u<1:
      ans = "NO"
      break
  else:
    d+=1
    if d>h:
      ans = "NO"
      break
  if j == "L":
    r=max(1,r-1)
  elif j == "R":
    l=min(w,l+1)
  elif j == "D":
    u=min(w,u+1)
  else:
    d=max(1,d-1)
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
r,l,u,d = sc,sc,sr,sr
ans = "YES"
for i,j in zip(s,t):
  if i == "R":
    r+=1
    if r>w:
      ans = "NO"
      break
  elif i =="L":
    l-=1
    if l<1:
      ans = "NO"
      break
  elif i =="U":
    u-=1
    if u<1:
      ans = "NO"
      break
  else:
    d+=1
    if d<1:
      ans = "NO"
      break
  if j == "L":
    r=max(1,r-1)
  elif j == "R":
    l=min(w,l+1)
  elif j == "D":
    u=min(w,u+1)
  else:
    d=max(1,d-1)
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
pos = list(map(int,input().split()))
s = input()
t = input()
def npos(pos,move):
  if move == "R":
    pos[1] += 1
    return pos
  if move == "L":
    pos[1] -= 1
    return pos
  if move == "U":
    pos[0] -= 1
    return pos
  if move == "D":
    pos[0] += 1
    return pos
ans = "YES"
for i,j in zip(s,t):
  pos = npos(pos,i)
  if pos[0]<1 or pos[0]>h or pos[1]<1 or pos[0]>w:
    ans = "NO"
    break
  pos = npos(pos,j)
  if pos[0]<1 or pos[0]>h or pos[1]<1 or pos[0]>w:
    ans = "NO"
    break
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    S = S[::-1]
    T = T[::-1]
    L, R, U, D = 1, W, 1, H
    f = False
    if S[0] == "L":
        L += 1
    elif S[0] == "R":
        R -= 1
    elif S[0] == "U":
        U += 1
    else:
        D -= 1
    if R < L or D < U:
        f = True
    for i in range(1, N):
        if (S[i] == "L" and T[i] == "R") or (S[i] == "R" and T[i] == "L") or (S[i] == "U" and T[i] == "D") or (S[i] == "D" and T[i] == "U"):
            continue
        if S[i] == "L":
            L += 1
        elif S[i] == "R":
            R -= 1
        elif S[i] == "U":
            U += 1
        else:
            D -= 1
        if R < L or D < U:
            f = True
    if sr < U or D < sr or sc < L or R < sc:
        f = True
    if f:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    S = S[::-1]
    T = T[::-1]
    L, R, U, D = 0, W, 0, H
    f = False
    if S[0] == "L":
        L += 1
    elif S[0] == "R":
        R -= 1
    elif S[0] == "U":
        U += 1
    else:
        D -= 1
    if R < L or D < U:
        f = True
    for i in range(1, N):
        if (S[i] == "L" and T[i] == "R") or (S[i] == "R" and T[i] == "L") or (S[i] == "U" and T[i] == "D") or (S[i] == "D" and T[i] == "U"):
            continue
        if S[i] == "L":
            L += 1
        elif S[i] == "R":
            R -= 1
        elif S[i] == "U":
            U += 1
        else:
            D -= 1
        if R < L or D < U:
            f = True
    if sr < U or D < sr or sc < L or R < sc:
        f = True
    if f:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
H,W,N=map(int, input().split())
h,w=map(int, input().split())
S=input()
T=input()
h1,h2,w1,w2=h,h,w,w
for i in range(N):
  if S[i]=='U':
    h1+=1
  if h1>H:
    print('NO')
    exit()
  if T[i]=='D':
    h1=max(h1-1,1)
 
  if S[i]=='D':
    h2-=1
  if h2<1:
    print('NO')
    exit()
  if T[i]=='U':
    h2=min(h2+1,H)
  
    
  if S[i]=='R':
    w1+=1
  if w1>W:
    print('NO')
    exit()
  if T[i]=='L':
    w1=max(w1-1,1)
  
    
  if S[i]=='L':
    w2-=1
  if w2<1:
    print('NO')
    exit()
  if T[i]=='R':
    w2=min(w2+1,W)

print('YES')

Submission status: Wrong Answer

Solution code:
#loud game
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":
        #(X,people)=いくつ進めるか、どちらの人間か
        sr_deque.append((-1,1))
    elif p=="D":
        sr_deque.append((1,1))
    q=t[i]
    if q=="U":
        #(X,people)=いくつ進めるか、どちらの人間か
        sr_deque.append((-1,2))
    elif q=="D":
        sr_deque.append((1,2))      

for i in range(n):
    p=s[i]
    if p=="R":
        #(X,people)=いくつ進めるか、どちらの人間か
        sc_deque.append((1,1))
    elif p=="L":
        sc_deque.append((-1,1))
    q=t[i]
    if q=="R":
        #(X,people)=いくつ進めるか、どちらの人間か
        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:
                    safe_left+=1 
            
        elif 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")

Submission status: Wrong Answer

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

def YES(b):
    if not b:
        print('YES')
    else:
        print('NO')
    exit(0)
    return

R = [s[0],s[0]]
C = [s[1],s[1]]

for i in range(n):
    if S[i] == 'L':
        C[0] -= 1
        if not (1 <= C[0] <= w):YES(True)
    elif S[i] == 'R':
        C[1] += 1
        if not (1 <= C[1] <= w):YES(True)
    elif S[i] == 'U':
        R[0] -= 1
        if not (1 <= R[0] <= h):YES(True)
    elif S[i] == 'D':
        R[1] += 1
        if not (1 <= R[0] <= h):YES(True)
    if T[i] == 'R' and C[0] != w:
        C[0] += 1
    elif T[i] == 'L' and C[1] != 1:
        C[1] -= 1
    elif T[i] == 'D' and R[0] != h:
        R[0] += 1
    elif T[i] == 'U' and R[1] != 1:
        R[1] -= 1
YES(False)

Submission status: Wrong Answer

Solution code:
H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
S=input()
T=input()
mtl=S.count("L")
mtr=S.count("R")
mtu=S.count("U")
mtd=S.count("D")
mal=T.count("L")
mar=T.count("R")
mau=T.count("U")
mad=T.count("D")
tl,tr,tu,td=0,0,0,0
al,ar,au,ad=0,0,0,0
a=1
for i in range(N):
    if S[i]=="L":
        tl+=1
        if sr-tl+ar==0:
            a=0
            break
    elif S[i]=="R":
        tr+=1
        if sr+tr-al>W:
            a=0
            break
    elif S[i]=="U":
        tu+=1
        if sc-tu+ad==0:
            a=0
            break
    else:
        td+=1
        if sc+td-au>H:
            a=0
            break
    if T[i]=="L":
        al+=1
        if mtr-tr-mal+al>=W:
            a=0
            break
    elif T[i]=="R":
        tr+=1
        if mtl-tl-mar+ar>=W:
            a=0
            break
    elif T[i]=="U":
        tu+=1
        if mtd-td-mau+au>=H:
            a=0
            break
    else:
        td+=1
        if mtu-tu-mad+ad>=H:
            a=0
            break
print("YES" if a else "NO")

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

h, w,n = map(int, input().split())
r,c = map(int, input().split())
s = input() #sys.stdin.readlineは最後が改行
t = input() #sys.stdin.readlineは最後が改行

toL = c
toR = w-c+1
toU = r
toD = h-r+1

# toR
cntR=0
for i in range(n):
    if s[i] == 'R' : cntR+=1
    if cntR == toR:
        print('NO')
        exit()
    if t[i] == 'L' and cntR>0 : cntR-=1

# toL
cntL = 0
for i in range(n):
    if s[i] == 'L' : cntL+=1
    if cntL == toL:
        print('NO')
        exit()
    if t[i] == 'R' and cntL>0: cntL-=1

# toL
cntU = 0
for i in range(n):
    if s[i] == 'U' : cntU+=1
    if cntU == toU:
        print('NO')
        exit()
    if t[i] == 'D' and cntU>0: cntU-=1

# toL
cntD = 0
for i in range(n):
    if s[i] == 'D' : cntD+=1
    if cntD == toD:
        print('NO')
        exit()
    if t[i] == 'U' and cntD>0: cntD-=1

print('YES')

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
s, t =  input(), input()
l, r, u, d = sx, sx, sy, sy

for i in range(n):
    if s[i] == "L": l -= 1
    elif s[i] == "R": r += 1
    elif s[i] == "U": u -= 1
    else: d += 1
    if l < 0 or r >= w or u < 0 or d >= h:
        print("NO")
        break
    if t[i] == "L" and r > 0: r -= 1
    elif t[i] == "R" and l < w-1: l += 1
    elif t[i] == "U" and d > 0: d -= 1
    elif u < h-1: u += 1
else: print("YES")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
s, t =  input(), input()
l, r, u, d = sx, sx, sy, sy

for i in range(n):
    if s[i] == "L": l -= 1
    elif s[i] == "R": r += 1
    elif s[i] == "U": u -= 1
    else: d += 1
    if l < 0 or r >= w or u >= h or d < 0:
        print("NO")
        break
    if t[i] == "L" and r > 0: r -= 1
    elif t[i] == "R" and l < w-1: l += 1
    elif t[i] == "U" and d > 0: d -= 1
    elif u < h-1: u += 1
else: print("YES")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
s, t =  input(), input()
l, r, u, d = sx, sx, sy, sy

for i in range(n):
    if s[i] == "L": l -= 1
    elif s[i] == "R": r += 1
    elif s[i] == "U": u += 1
    else: d -= 1
    if l < 0 or r >= w or u >= h or d < 0:
        print("NO")
        break
    if t[i] == "L" and r > 0: r -= 1
    elif t[i] == "R" and l < w-1: l += 1
    elif t[i] == "U" and d > 0: d += 1
    elif u < h-1: u -= 1
else: print("YES")

Submission status: Wrong Answer

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

t,r,b,l=0,W-1,H-1,0
for i in range(N):
    if S[i]=='U': t = min(t+1, H-1)
    elif S[i]=='D': b = max(b-1, 0)
    elif S[i]=='L': l = min(l+1, W-1)
    elif S[i]=='R': r = max(r-1, 0)
    if not (t <= sr <= b and l <= sc <= r):
        print('NO')
        exit()
    if T[i]=='U': b = min(b+1, H-1)
    elif T[i]=='D': t = max(t-1, 0)
    elif T[i]=='L': r = min(r+1, W-1)
    elif T[i]=='R': l = max(l-1, 0)
print('YES')