結果

問題 No.2411 Reverse Directions
ユーザー titiatitia
提出日時 2023-08-12 03:27:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 2,149 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 116,096 KB
最終ジャッジ日時 2024-04-29 17:59:49
合計ジャッジ時間 4,328 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,144 KB
testcase_01 AC 44 ms
54,272 KB
testcase_02 AC 44 ms
54,400 KB
testcase_03 AC 40 ms
54,528 KB
testcase_04 AC 58 ms
71,936 KB
testcase_05 AC 39 ms
54,016 KB
testcase_06 AC 38 ms
54,400 KB
testcase_07 AC 78 ms
83,200 KB
testcase_08 AC 38 ms
54,272 KB
testcase_09 AC 38 ms
54,144 KB
testcase_10 AC 168 ms
116,096 KB
testcase_11 AC 39 ms
54,912 KB
testcase_12 AC 185 ms
101,632 KB
testcase_13 AC 40 ms
54,016 KB
testcase_14 AC 116 ms
86,016 KB
testcase_15 AC 65 ms
74,368 KB
testcase_16 AC 66 ms
73,984 KB
testcase_17 AC 112 ms
83,584 KB
testcase_18 AC 91 ms
84,336 KB
testcase_19 AC 38 ms
54,400 KB
testcase_20 AC 110 ms
81,272 KB
testcase_21 AC 87 ms
78,688 KB
testcase_22 AC 154 ms
93,028 KB
testcase_23 AC 39 ms
54,016 KB
testcase_24 AC 181 ms
95,360 KB
testcase_25 AC 38 ms
54,656 KB
testcase_26 AC 54 ms
68,992 KB
testcase_27 AC 94 ms
77,824 KB
testcase_28 AC 139 ms
95,744 KB
testcase_29 AC 190 ms
106,808 KB
testcase_30 AC 156 ms
99,908 KB
testcase_31 AC 134 ms
85,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from collections import deque

H,W,K,L,R=map(int,input().split())

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

MAP=[input().strip() for i in range(H)]

F=[[1<<30]*W for i in range(H)]
BACK=[[-1]*W for i in range(H)]

F[0][0]=0

Q=deque()
Q.append((0,0))

while Q:
    x,y=Q.popleft()

    for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
        if 0<=z<H and 0<=w<W and MAP[z][w]=="." and F[z][w]==1<<30:
            F[z][w]=F[x][y]+1
            Q.append((z,w))
            BACK[z][w]=(x,y)

F2=[[1<<30]*W for i in range(H)]
BACK2=[[-1]*W for i in range(H)]

F2[H-1][W-1]=0

Q=deque()
Q.append((H-1,W-1))

while Q:
    x,y=Q.popleft()

    for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
        if 0<=z<H and 0<=w<W and MAP[z][w]=="." and F2[z][w]==1<<30:
            F2[z][w]=F2[x][y]+1
            Q.append((z,w))
            BACK2[z][w]=(x,y)


gx=-1
gy=-1
for i in range(H):
    for j in range(W):
        if F[i][j]<=L-1 and F[i][j]%2==(L-1)%2 and F2[i][j]<=K-R and F2[i][j]%2==(K-R)%2 and i+1<H and MAP[i+1][j]=="." and i>=1 and MAP[i-1][j]==".":
            gx,gy=i,j
        if F[i][j]<=L-1 and F[i][j]%2==(L-1)%2 and F2[i][j]<=K-R and F2[i][j]%2==(K-R)%2 and j+1<W and MAP[i][j+1]=="." and j>=1 and MAP[i][j-1]==".":
            gx,gy=i,j

if gx==-1:
    print("No")
    exit()

ANS1=[]
ANS2=[]

x=gx
y=gy

while x!=0 or y!=0:
    z,w=BACK[x][y]

    if z==x+1:
        ANS1.append("U")
    elif z==x-1:
        ANS1.append("D")
    elif w==y+1:
        ANS1.append("L")
    else:
        ANS1.append("R")

    x,y=z,w

x=gx
y=gy

while x!=H-1 or y!=W-1:
    z,w=BACK2[x][y]

    if z==x+1:
        ANS2.append("D")
    elif z==x-1:
        ANS2.append("U")
    elif w==y+1:
        ANS2.append("R")
    else:
        ANS2.append("L")

    x,y=z,w

ANS1.reverse()

if gx+1<H and MAP[gx+1][gy]=="." and gx>=1 and MAP[gx-1][gy]==".":
    ANS3=["D","U"]*((K-len(ANS1)-len(ANS2))//2)

elif gy+1<W and MAP[gx][gy+1]=="." and gy>=1 and MAP[gx][gy-1]==".":
    ANS3=["R","L"]*((K-len(ANS1)-len(ANS2))//2)

else:
    print("No")
    exit()

ANS=ANS1+ANS3+ANS2

print("Yes")
print("".join(ANS))
        
    


0