結果

問題 No.2411 Reverse Directions
ユーザー AngrySadEightAngrySadEight
提出日時 2023-07-20 00:04:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 3,995 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 13,440 KB
実行使用メモリ 19,148 KB
最終ジャッジ日時 2024-04-18 01:01:50
合計ジャッジ時間 4,089 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
16,640 KB
testcase_01 AC 34 ms
11,264 KB
testcase_02 AC 33 ms
11,264 KB
testcase_03 AC 33 ms
11,264 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

H, W, K, L, R = map(int, input().split())
S = []
for _ in range(H):
    s = str(input())
    S.append(s)

if (R - L + 1) % 2 == 1:
    print("No")
    exit()

if (H + W - 2) % 2 != K % 2:
    print("No")
    exit()

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
c = ['D', 'R', 'U', 'L']

INF = 10000002

dist_s = [[INF for _ in range(W)] for _ in range(H)]
dist_t = [[INF for _ in range(W)] for _ in range(H)]

dq = deque()
dist_s[0][0] = 0
dq.append([0, 0])

while len(dq):
    px, py = dq.popleft()
    for i in range(4):
        if px + dx[i] >= 0 and px + dx[i] < H and py + dy[i] >= 0 and py + dy[i] < W:
            if S[px + dx[i]][py + dy[i]] == '#':
                continue
            if dist_s[px + dx[i]][py + dy[i]] >= INF:
                dist_s[px + dx[i]][py + dy[i]] = dist_s[px][py] + 1
                dq.append([px + dx[i], py + dy[i]])

dist_t[H - 1][W - 1] = 0
dq.append([H - 1, W - 1])

while len(dq):
    px, py = dq.popleft()
    for i in range(4):
        if px + dx[i] >= 0 and px + dx[i] < H and py + dy[i] >= 0 and py + dy[i] < W:
            if S[px + dx[i]][py + dy[i]] == '#':
                continue
            if dist_t[px + dx[i]][py + dy[i]] >= INF:
                dist_t[px + dx[i]][py + dy[i]] = dist_t[px][py] + 1
                dq.append([px + dx[i], py + dy[i]])

dir = [[-1 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if i > 0 and i < H - 1:
            if S[i][j] == '.' and S[i - 1][j] == '.' and S[i + 1][j] == '.':
                dir[i][j] = 1
        if j > 0 and j < W - 1:
            if S[i][j] == '.' and S[i][j - 1] == '.' and S[i][j + 1] == '.':
                dir[i][j] = 2

idx_x = -1
idx_y = -1

for i in range(H):
    for j in range(W):
        if dir[i][j] >= 1:
            if dist_s[i][j] > L - 1:
                continue
            if abs(dist_s[i][j] - (L - 1)) % 2 == 1:
                continue
            if dist_t[i][j] > K - R:
                continue
            if abs(dist_t[i][j] - (K - R)) % 2 == 1:
                continue
            idx_x = i
            idx_y = j

if idx_x == -1:
    print("No")
else:
    print("Yes")
    ans_str = ""
    now_idx_x = idx_x
    now_idx_y = idx_y

    ss = ""
    while True:
        if now_idx_x == 0 and now_idx_y == 0:
            break
        for i in range(4):
            if now_idx_x + dx[i] >= 0 and now_idx_x + dx[i] < H and now_idx_y + dy[i] >= 0 and now_idx_y + dy[i] < W:
                if dist_s[now_idx_x + dx[i]][now_idx_y + dy[i]] == dist_s[now_idx_x][now_idx_y] - 1:
                    now_idx_x += dx[i]
                    now_idx_y += dy[i]
                    ss += c[(i + 2) % 4]
    while True:
        if len(ss) == L - 1:
            break
        if ss[-1] == 'L':
            ss += "RL"
        if ss[-1] == 'R':
            ss += "LR"
        if ss[-1] == 'U':
            ss += "DU"
        if ss[-1] == 'D':
            ss += "UD"
    for i in reversed(range(len(ss))):
        ans_str += ss[i]

    if dir[idx_x][idx_y] == 1:
        for i in range((R - L + 1) // 2):
            ans_str += "UD"
    else:
        for i in range((R - L + 1) // 2):
            ans_str += "LR"

    now_idx_x = idx_x
    now_idx_y = idx_y
    st = ""
    while True:
        if now_idx_x == H - 1 and now_idx_y == W - 1:
            break
        for i in range(4):
            if now_idx_x + dx[i] >= 0 and now_idx_x + dx[i] < H and now_idx_y + dy[i] >= 0 and now_idx_y + dy[i] < W:
                if dist_t[now_idx_x + dx[i]][now_idx_y + dy[i]] == dist_t[now_idx_x][now_idx_y] - 1:
                    now_idx_x += dx[i]
                    now_idx_y += dy[i]
                    st += c[i]
    while True:
        if len(st) == K - R:
            break
        if st[-1] == 'L':
            st += "RL"
        if st[-1] == 'R':
            st += "LR"
        if st[-1] == 'U':
            st += "DU"
        if st[-1] == 'D':
            st += "UD"
    ans_str += st
    print(ans_str)
0