結果

問題 No.2411 Reverse Directions
ユーザー 👑 AngrySadEight
提出日時 2023-07-19 23:52:06
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 4,403 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 755,704 KB
最終ジャッジ日時 2024-10-09 18:45:45
合計ジャッジ時間 5,548 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 MLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

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')
st += "UD"
for i in range(len(st)):
ans_str.append(st[i])
print(''.join(ans_str))
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0