結果

問題 No.2411 Reverse Directions
ユーザー miya145592miya145592
提出日時 2023-08-11 23:49:05
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,817 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 127,720 KB
最終ジャッジ日時 2024-04-29 15:06:18
合計ジャッジ時間 7,825 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,888 KB
testcase_01 AC 47 ms
54,272 KB
testcase_02 AC 49 ms
54,400 KB
testcase_03 AC 48 ms
54,272 KB
testcase_04 AC 74 ms
79,616 KB
testcase_05 AC 48 ms
54,528 KB
testcase_06 AC 48 ms
54,400 KB
testcase_07 AC 133 ms
85,376 KB
testcase_08 AC 46 ms
54,528 KB
testcase_09 AC 47 ms
54,400 KB
testcase_10 RE -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 48 ms
55,040 KB
testcase_14 RE -
testcase_15 WA -
testcase_16 AC 112 ms
78,208 KB
testcase_17 RE -
testcase_18 WA -
testcase_19 AC 47 ms
54,400 KB
testcase_20 RE -
testcase_21 AC 51 ms
55,936 KB
testcase_22 WA -
testcase_23 AC 53 ms
56,320 KB
testcase_24 WA -
testcase_25 AC 49 ms
55,552 KB
testcase_26 AC 97 ms
77,312 KB
testcase_27 AC 48 ms
54,912 KB
testcase_28 WA -
testcase_29 RE -
testcase_30 WA -
testcase_31 AC 52 ms
56,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
H, W, K, L, R = map(int, input().split())
S = [input() for _ in range(H)] 
if (R-L+1)%2==1:
    print("No")
    exit()
if ((H-1)+(W-1))%2!=K%2:
    print("No")
    exit()

ok = set()
for i in range(H):
    for j in range(W):
        ud = 0
        if 0<i<H-1 and S[i-1][j]==S[i][j]==S[i+1][j]==".":
            ud = 1
        lr = 0
        if 0<j<W-1 and S[i][j-1]==S[i][j]==S[i][j+1]==".":
            lr = 1
        if ud or lr:
            ok.add((i, j, ud, lr))

dir = [(1, 0), (0, 1), (-1, 0), (0, -1)]
INF = 10**9
deq = deque()
dist_s = [[INF for _ in range(W)] for _ in range(H)]
pre_s = [[(-1, -1) for _ in range(W)] for _ in range(H)]
deq.append((0, 0))
dist_s[0][0] = 0
while deq:
    i, j = deq.popleft()
    for di, dj in dir:
        ni = i+di
        nj = j+dj
        if 0<=ni<H and 0<=nj<W and S[ni][nj]==".":
            if dist_s[ni][nj] > dist_s[i][j]+1:
                dist_s[ni][nj] = dist_s[i][j]+1
                pre_s[ni][nj] = (i, j)
                deq.append((ni, nj))
if dist_s[H-1][W-1]>K:
    print("No")
    exit()

deq = deque()
dist_t = [[INF for _ in range(W)] for _ in range(H)]
pre_t = [[(-1, -1) for _ in range(W)] for _ in range(H)]
deq.append((H-1, W-1))
dist_t[H-1][W-1] = 0
while deq:
    i, j = deq.popleft()
    for di, dj in dir:
        ni = i+di
        nj = j+dj
        if 0<=ni<H and 0<=nj<W and S[ni][nj]==".":
            if dist_t[ni][nj] > dist_t[i][j]+1:
                dist_t[ni][nj] = dist_t[i][j]+1
                pre_t[ni][nj] = (i, j)
                deq.append((ni, nj))

dir_rev = dict()
dir_rev[(1, 0)] = "D"
dir_rev[(0, 1)] = "R"
dir_rev[(-1, 0)] = "U"
dir_rev[(0, -1)] = "L"
for i, j, ud, lr in ok:
    d = dist_s[i][j]
    if d==INF:
        continue
    if L<=d:
        continue
    if (L-1)%2!=d%2:
        continue
    d = dist_t[i][j]
    if d==INF:
        continue
    if (K-R)<d:
        continue
    if (K-R)%2!=d%2:
        continue

    order = []
    now = (i, j)
    while now != (0, 0):
        ii, jj = now
        pi, pj = pre_s[ii][jj]
        v = dir_rev[(ii-pi, jj-pj)]
        order.append(v)
        now = pre_s[ii][jj]
    order.reverse()

    a = L-1-dist_s[i][j]
    for _ in range(0, a, 2):
        order.append(order[-2])
        order.append(order[-2])

    if ud:
        for _ in range(0, R-L+1, 2):
            order.append("UD")
    else:
        for _ in range(0, R-L+1, 2):
            order.append("LR")


    now = (i, j)
    while now != (H-1, W-1):
        ii, jj = now
        pi, pj = pre_t[ii][jj]
        v = dir_rev[(pi-ii, pj-jj)]
        order.append(v)
        now = pre_t[ii][jj]

    a = K-len("".join(order))
    for _ in range(0, a, 2):
        order.append(order[-2])
        order.append(order[-2])

    print("Yes")
    print("".join(order))
    exit()

print("No")
0