結果
| 問題 | No.2411 Reverse Directions | 
| コンテスト | |
| ユーザー |  AngrySadEight | 
| 提出日時 | 2023-07-20 00:04:07 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 3,995 bytes | 
| コンパイル時間 | 379 ms | 
| コンパイル使用メモリ | 13,312 KB | 
| 実行使用メモリ | 19,120 KB | 
| 最終ジャッジ日時 | 2024-10-09 18:45:56 | 
| 合計ジャッジ時間 | 4,168 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 1 TLE * 1 -- * 27 | 
ソースコード
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)
            
            
            
        