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: Runtime Error

Solution code:
#https://atcoder.jp/contests/agc033
# -*- coding: utf-8 -*-

h,w,n = map(int,input().split())
current = list(map(int,input().split()))
s = list(input())
t = list(input())

U = current[0]
D = h-current[0]+1
L = current[1]
R = w-current[1]+1

# print(U,D,L,R)
# print(sU,sD,sL,sR)
# print(tU,tD,tL,tR)

win="NO"
for i in range(N):
        U2 = U - s[0:i].count("U") + t[0:i].count("U")
        D2 = D - s[0:i].count("D") + t[0:i].count("D")
        L2 = L - [0:i].count("L") + t[0:i].count("L")
        R2 = R - [0:i].count("R") + t[0:i].count("R")

        if U2 == 0 or D2 == 0 or L2 == 0 or R2 == 0:
                win="YES"
                break
print(win)

Submission status: Runtime Error

Solution code:
import random
if random.random>0.5:
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

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

def mv(i,sr,sc):
    if S[i]=='L':
        sc -= 1
    elif S[i]=='R':
        sc += 1
    elif s[i]=='U':
        sr -= 1
    else:
        sr += 1

def SSS(i,sr,sc):
    if S[i]=='L':
        if sc <= W/2:
            mv(i,sr,sc)
    elif S[i]=='R':
        if sc >= W/2:
            mv(i,sr,sc)
    elif S[i]=='U':
        if sr <= H/2:
            mv(i,sr,sc)
    else:
        if sr >= H/2:
            mv(i,sr,sc)

def TTT(i,sr,sc):
    if T[i]=='L':
        if sc > W/2:
            mv(i,sr,sc)
    elif T[i]=='R':
        if sc < W/2:
            mv(i,sr,sc)
    elif T[i]=='U':
        if sr > H/2:
            mv(i,sr,sc)
    else:
        if sr < H/2:
            mv(i,sr,sc)
    
for i in range(N):
    if sc>W or sc<1 or sr>H or sr<1:
        print('NO')
        exit()
    else:
        SSS(i,sr,sc)
        TTT(i,sr,sc)

if sc>W or sc<1 or sr>H or sr<1:
    print('NO')
else:
    print('YES')

Submission status: Runtime Error

Solution code:
H, W, N = [int(n) for n in input().split()]
sr, sc = [int(n) for n in input().split()]
S = input()
T = input()

def search(i, y, x):
    if y == -1 or y == H or x == -1 or x == W:
        return False
    if i == 2*N:
        return True

    if i%2 == 0:
        M = S
    else:
        M = T

    if M[i//2] == 'L':
        move = search(i + 1, y, x - 1)
    elif M[i//2] == 'R':
        move = search(i + 1, y, x + 1)
    elif M[i//2] == 'U':
        move = search(i + 1, y - 1, x)
    else:
        move = search(i + 1, y + 1, x)
    stop = search(i + 1, y, x)

    if i%2 == 0:
        return move and stop
    else:
        return move or stop

if search(0, sr - 1, sc - 1):
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

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

U=current[0]
D=h-current[0]
L=current[1]
R=w-current[1]
win="NO"
for i in range(n):
  sU=s[0:i].count("U")
  sD=s[0:i].count("D")
  sL=s[0:i].count("L")
  sR=s[0:i].count("R")
  tU=t[0:i].count("U")
  tD=t[0:i].count("D")
  tL=t[0:i].count("L")
  tR=t[0:i].count("R")
  if (U-sU+tU)<=0:
    win="YES"
    break
  if (D-sD+tD)<=0:
    win="YES"
    break
  if (L-sL+tL)<=0:
    win="YES"
    break
  if (R-sR+tR)<=0:
    win="YES"
    break
print(win)

Submission status: Runtime Error

Solution code:
h,w,n = map(int, input().split(" "))
sr,sc = map(int, input().split(" "))

s = input()
t = input()

stst = ""
for i in range(n):
    stst += s[i]
    stst += t[i]

def dfs(pos, x, y):

    if x <= 0 or x > w or y <= 0 or y > h:
        # 落ちたらFalse
        return False
    
    if pos == 2*n:
        # 最後まで落ちていなければTrue
        return True
    
    g = stst[pos]
    a = dfs(pos+1, x + int(g=="R") - int(g=="L"), y + int(g=="D") - int(g=="U"))
    b = dfs(pos+1, x, y)

    if pos % 2 == 0:
        # 落とす未来があればFalse
        return a and b
    else:
        # 落とさない未来があればTrue
        return a or b

print("YES" if dfs(0, sc, sr) else "NO")

Submission status: Runtime Error

Solution code:
h,w,n = map(int, input().split(" "))
sr,sc = map(int, input().split(" "))

s = input()
t = input()

stst = ""
for i in range(n):
    stst += s[i]
    stst += t[i]

def dfs(pos, x, y):

    if x <= 0 or x > w or y <= 0 or y > h:
        # 落ちたらFalse
        return False
    
    if pos == 2*n:
        # 最後まで落ちていなければTrue
        return True
    
    g = stst[pos]
    a = dfs(pos+1, x + int(g=="R") - int(g=="L"), y + int(g=="D") - int(g=="U"))
    b = dfs(pos+1, x, y)

    if pos % 2 == 0:
        # 落とす未来があればFalse
        return a and b
    else:
        # 落とさない未来があればTrue
        return a or b

print("Yes" if dfs(0, sc, sr) else "No")

Submission status: Runtime Error

Solution code:
import numpy as np
H,W,N =map(int ,raw_input().split())
Sr,Sc =map(int ,raw_input().split())
S = raw_input()
T = raw_input()

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

if Dr % 2 == 0:
    Dr = H/2
else:
    Dr = (H-1)/2
if Dc % 2 == 0:
    Dc = W/2
else:
    Dc = (W-1)/2 
s = 1
for i in range(N):
    if (S[i] == 'D')and(Sr >=Dr):
        Sr = Sr+1
    elif (S[i] == 'U')and(Sr <=Dr):
        Sr = Sr-1
    elif (S[i] == 'R')and(Sc >=Dc):
        Sc = Sc+1
    elif (S[i] == 'L')and(Sc <=Dc):
        Sc = Sc-1
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
    if (T[i] == 'D')and(Sr <Dr):
        Sr = Sr+1
    elif (T[i] == 'U')and(Sr >Dr):
        Sr = Sr-1
    elif (T[i] == 'R')and(Sc <Dc):
        Sc = Sc+1
    elif (T[i] == 'L')and(Sc >Dc):
        Sc = Sc-1
    
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
if s ==0:
    print('NO')
else:
    print('YES')

Submission status: Runtime Error

Solution code:
import copy
import numpy as np
H, W = map(int, input().split())
A = np.array([[0 if a == "#" else 1 for a in input()] for _ in range(H)])

def change_color(A):
  new_A = copy.deepcopy(A)
  for i, ai in enumerate(A):
    for j, aij in enumerate(ai):
      if aij == 0:
        if i == H - 1 and j == W - 1:
          new_A[i - 1][j] = 0
          new_A[i][j - 1] = 0
        elif i == H - 1:
          if j == 0:
            new_A[i - 1][j] = 0
            new_A[i][j + 1] = 0
          else:
            new_A[i - 1][j] = 0
            new_A[i][j - 1] = 0
            new_A[i][j + 1] = 0
        elif j == W - 1:
          if i == 0:
            new_A[i + 1][j] = 0
            new_A[i][j - 1] = 0
          else:  
            new_A[i - 1][j] = 0
            new_A[i + 1][j] = 0
            new_A[i][j - 1] = 0
        else:
          if i == 0:
            new_A[i + 1][j] = 0
            new_A[i][j - 1] = 0
            new_A[i][j + 1] = 0  
          elif j == 0:
            new_A[i - 1][j] = 0
            new_A[i + 1][j] = 0
            new_A[i][j + 1] = 0
          else:  
            new_A[i - 1][j] = 0
            new_A[i + 1][j] = 0
            new_A[i][j - 1] = 0
            new_A[i][j + 1] = 0  
          
  return new_A
    
count = 0

while A.sum() != 0:
  A = change_color(A)
  count += 1

print(count)

Submission status: Runtime Error

Solution code:
from sys import exit
from collections import defaultdict
import itertools
H,W,N = [int(n) for n in input().split()]
Sr,Sc = [int(n) for n in input().split()]
#上に
Uout = H-Sr + 1
#下に
Dout = Sr
#右に落とすのに必要な歩数
Rout = W - Sc + 1
#左に
Lout = Sc
S = str(input())
T = str(input())

def check(outcnt, C, A, lim, B):
    re = False
    for s,t in zip(S,T):
        if s == B and outcnt == lim -1:
            re = True
            break
        if( s == C):
            outcnt -= 1
        if(outcnt == 0 ):
            print("NO")
            exit()
        if(t == A and outcnt < lim):
            outcnt += 1
    if(re):
        check(outcnt, C, A, lim-1, B)
check(Rout, "R", "L", W, "L")
check(Lout, "L", "R", W, "R")
check(Uout, "U", "D", H, "D")
check(Dout, "D", "U", H, "U")
print("YES")

Submission status: Runtime Error

Solution code:
H, W, N = map(int, input().split(' '))
s_r, s_c = map(int, input().split(' '))
S = list(input())
T = list(input())
cnt = 0
for i in range(N):
  if S[i] == 'L':
    if abs((s_c-1)-W/2)=>abs((s_c)-W/2):
      s_c -= 1
      if s_c<1:
        break
  elif S[i] == 'R':
    if abs((s_c+1)-W/2)=>abs((s_c)-W/2):
      s_c += 1
      if s_c>W:
        break
  elif S[i] == 'U':
    if abs((s_r-1)-H/2)=>abs((s_r)-H/2):
      s_r -= 1
      if s_r<1:
        break
  else:
    if abs((s_r+1)-H/2)=>abs((s_r)-H/2):
      s_r += 1
      if s_r>H:
        break
  if T[i] == 'L':
    if abs((s_c-1)-W/2)<=abs((s_c)-W/2):
      s_c -= 1
  elif  T[i] == 'R':
    if abs((s_c+1)-W/2)<=abs((s_c)-W/2):
      s_c += 1
  elif T[i] == 'U':
    if abs((s_r-1)-H/2)<=abs((s_r)-H/2):
      s_r -= 1
  else:
    if abs((s_r+1)-H/2)<=abs((s_r)-H/2):
      s_r += 1
  cnt += 1
if cnt==N:
  print('YES')
else:
  print('NO')

Submission status: Runtime Error

Solution code:
import numpy as np
h,w=map(int,input().split())
INF=10**10
a=[[INF for i in range(w)] for j in range(h)]
for i in range(h):
    s=input()
    for j in range(w):
        if s[j]=="#":
            a[i][j]=0
for i in range(h):
    for j in range(w):
        k=a[i][j]+1

        if i!=h-1:
            if k<a[i+1][j]:
                a[i+1][j]=k

        if j!=w-1:
            if k<a[i][j+1]:
                a[i][j+1]=k

for i_ in range(h):
    for j_ in range(w):
        i=h-1-i_
        j=w-1-j_
        k=a[i][j]+1
        if i!=0:
            if k<a[i-1][j]:
                a[i-1][j]=k

        if j!=0:        
            if k<a[i][j-1]:
                a[i][j-1]=k


arr=np.array(a)
ans=arr.max()
print(ans)