結果

問題 No.2411 Reverse Directions
ユーザー miya145592miya145592
提出日時 2023-08-11 23:57:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 311 ms / 2,000 ms
コード長 3,004 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 82,280 KB
実行使用メモリ 133,308 KB
最終ジャッジ日時 2024-11-18 19:44:44
合計ジャッジ時間 5,975 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
54,784 KB
testcase_01 AC 43 ms
54,400 KB
testcase_02 AC 45 ms
54,656 KB
testcase_03 AC 44 ms
54,144 KB
testcase_04 AC 72 ms
80,384 KB
testcase_05 AC 44 ms
54,656 KB
testcase_06 AC 44 ms
54,528 KB
testcase_07 AC 123 ms
84,992 KB
testcase_08 AC 45 ms
54,656 KB
testcase_09 AC 45 ms
54,400 KB
testcase_10 AC 279 ms
133,308 KB
testcase_11 AC 46 ms
54,656 KB
testcase_12 AC 311 ms
120,416 KB
testcase_13 AC 45 ms
55,552 KB
testcase_14 AC 172 ms
88,892 KB
testcase_15 AC 92 ms
80,556 KB
testcase_16 AC 100 ms
78,464 KB
testcase_17 AC 175 ms
87,864 KB
testcase_18 AC 123 ms
85,172 KB
testcase_19 AC 45 ms
54,528 KB
testcase_20 AC 169 ms
86,528 KB
testcase_21 AC 48 ms
56,320 KB
testcase_22 AC 258 ms
105,360 KB
testcase_23 AC 49 ms
56,320 KB
testcase_24 AC 308 ms
110,708 KB
testcase_25 AC 46 ms
55,168 KB
testcase_26 AC 84 ms
77,312 KB
testcase_27 AC 46 ms
55,168 KB
testcase_28 AC 214 ms
105,656 KB
testcase_29 AC 299 ms
122,868 KB
testcase_30 AC 233 ms
110,272 KB
testcase_31 AC 49 ms
56,192 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]
    if ud:
        for _ in range(0, a, 2):
            order.append("UD")
        for _ in range(0, R-L+1, 2):
            order.append("UD")
    else:
        for _ in range(0, a, 2):
            order.append("LR")
        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))
    z = order[-1]
    for _ in range(0, a, 2):
        if z=="R":
            order.append("LR")
        elif z=="L":
            order.append("RL")
        elif z=="U":
            order.append("DU")
        else:
            order.append("UD")

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

print("No")
0