結果

問題 No.2411 Reverse Directions
ユーザー ShirotsumeShirotsume
提出日時 2023-08-11 22:41:00
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,268 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 125,428 KB
最終ジャッジ日時 2024-04-29 13:48:13
合計ジャッジ時間 7,440 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
55,936 KB
testcase_01 AC 51 ms
55,680 KB
testcase_02 AC 52 ms
55,552 KB
testcase_03 AC 52 ms
55,936 KB
testcase_04 RE -
testcase_05 AC 52 ms
55,424 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 252 ms
122,116 KB
testcase_11 WA -
testcase_12 AC 171 ms
101,120 KB
testcase_13 WA -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 75 ms
68,736 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 53 ms
56,192 KB
testcase_20 RE -
testcase_21 AC 101 ms
80,256 KB
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 76 ms
70,272 KB
testcase_26 AC 52 ms
56,320 KB
testcase_27 AC 82 ms
72,192 KB
testcase_28 RE -
testcase_29 AC 209 ms
125,428 KB
testcase_30 RE -
testcase_31 AC 108 ms
80,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353


h, w, K, l, r = mi()

s = [input() for _ in range(h)]

dist = [[inf, inf] * w for _ in range(h)]
bef = [[None] * w for _ in range(h)]

dist[0][0] = 0

bef[0][0] = (0, 0)

q = deque()
q.append((0, 0))
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
dz = 'UDLR'
while q:
    nowi, nowj = q.popleft()
    for to in range(4):
        toi = nowi + dx[to]
        toj = nowj + dy[to]
        if 0 <= toi < h and 0 <= toj < w and s[toi][toj] == '.' and dist[toi][toj] > dist[nowi][nowj] + 1:
            dist[toi][toj] = dist[nowi][nowj] + 1
            bef[toi][toj] = (nowi, nowj)
            q.append((toi, toj))

if dist[h - 1][w - 1] > K - (r - l + 1) or dist[h - 1][w - 1] % 2 != K % 2:
    print('No')
else:
    root = [(h - 1, w - 1)]
    nowi, nowj = h - 1, w - 1
    while True:
        toi, toj = bef[nowi][nowj]
        if toi == nowi and toj == nowj:
            break
        root.append((toi, toj))
        nowi = toi
        nowj = toj
    
    root.reverse()
    x = []
    for i in range(len(root) - 1):
        if root[i + 1][0] - root[i][0] == 1:
            x.append('D')
        elif root[i + 1][0] - root[i][0] == -1:
            x.append('U')
        elif root[i + 1][1] - root[i][1] == 1:
            x.append('R')
        else:
            x.append('L')
    
    x = deque(x)
    ans = []
    nowi = 0
    nowj = 0

    while len(ans) < K:
        if l - 1 <= len(ans) <= r - 2 or not x:
            for k in range(4):
                toi = nowi + dx[k]
                toj = nowj + dy[k]
                to2i = nowi + dx[k^1]
                to2j = nowj + dy[k^2]
                if 0 <= toi < h and 0 <= toj < w and s[toi][toj] == '.' and s[to2i][to2j] == '.':
                    ans.append(dz[k])
                    ans.append(dz[k^1])
                    break
            else:
                print('No')
                exit()
        else:
            ans.append(x.popleft())
            nowi += dx[dz.index(ans[-1])]
            nowj += dy[dz.index(ans[-1])]
    print('Yes')
    print(''.join(ans))
0