結果

問題 No.2411 Reverse Directions
ユーザー AngrySadEightAngrySadEight
提出日時 2023-07-20 00:08:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 4,380 bytes
コンパイル時間 566 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 150,236 KB
最終ジャッジ日時 2024-04-18 01:01:46
合計ジャッジ時間 4,957 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,656 KB
testcase_01 AC 38 ms
54,656 KB
testcase_02 AC 42 ms
54,784 KB
testcase_03 AC 41 ms
54,784 KB
testcase_04 AC 81 ms
99,200 KB
testcase_05 AC 45 ms
54,656 KB
testcase_06 AC 38 ms
54,400 KB
testcase_07 AC 95 ms
81,024 KB
testcase_08 AC 40 ms
54,784 KB
testcase_09 AC 40 ms
54,656 KB
testcase_10 AC 252 ms
150,236 KB
testcase_11 AC 38 ms
55,552 KB
testcase_12 AC 216 ms
113,724 KB
testcase_13 AC 45 ms
55,296 KB
testcase_14 AC 154 ms
95,744 KB
testcase_15 AC 90 ms
92,300 KB
testcase_16 AC 88 ms
77,440 KB
testcase_17 AC 141 ms
86,392 KB
testcase_18 AC 125 ms
103,592 KB
testcase_19 AC 37 ms
54,912 KB
testcase_20 AC 136 ms
79,624 KB
testcase_21 AC 45 ms
56,704 KB
testcase_22 AC 182 ms
101,376 KB
testcase_23 AC 46 ms
56,704 KB
testcase_24 AC 193 ms
86,656 KB
testcase_25 AC 42 ms
55,424 KB
testcase_26 AC 81 ms
77,056 KB
testcase_27 AC 39 ms
55,168 KB
testcase_28 AC 182 ms
117,632 KB
testcase_29 AC 228 ms
116,832 KB
testcase_30 AC 199 ms
131,480 KB
testcase_31 AC 45 ms
57,088 KB
権限があれば一括ダウンロードができます

ソースコード

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.append(c[(i + 2) % 4])
    while True:
        if len(ss) == L - 1:
            break
        if ss[-1] == 'L':
            ss.append('R')
            ss.append('L')
        if ss[-1] == 'R':
            ss.append('L')
            ss.append('R')
        if ss[-1] == 'U':
            ss.append('D')
            ss.append('U')
        if ss[-1] == 'D':
            ss.append('U')
            ss.append('D')
    for i in reversed(range(len(ss))):
        ans_str.append(ss[i])

    if dir[idx_x][idx_y] == 1:
        for i in range((R - L + 1) // 2):
            ans_str.append('U')
            ans_str.append('D')
    else:
        for i in range((R - L + 1) // 2):
            ans_str.append('L')
            ans_str.append('R')

    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.append(c[i])
    while True:
        if len(st) == K - R:
            break
        if st[-1] == 'L':
            st.append('R')
            st.append('L')
        if st[-1] == 'R':
            st.append('L')
            st.append('R')
        if st[-1] == 'U':
            st.append('D')
            st.append('U')
        if st[-1] == 'D':
            st.append('U')
            st.append('D')
    for i in range(len(st)):
        ans_str.append(st[i])
    print(''.join(ans_str))
0