結果

問題 No.2411 Reverse Directions
ユーザー flygonflygon
提出日時 2023-08-11 22:48:59
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,877 bytes
コンパイル時間 662 ms
コンパイル使用メモリ 82,420 KB
実行使用メモリ 102,996 KB
最終ジャッジ日時 2024-11-18 17:55:32
合計ジャッジ時間 4,992 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
57,044 KB
testcase_01 AC 40 ms
55,184 KB
testcase_02 AC 40 ms
56,204 KB
testcase_03 AC 41 ms
57,000 KB
testcase_04 AC 55 ms
66,572 KB
testcase_05 AC 41 ms
56,420 KB
testcase_06 AC 42 ms
56,840 KB
testcase_07 AC 76 ms
82,112 KB
testcase_08 AC 43 ms
55,540 KB
testcase_09 AC 41 ms
55,504 KB
testcase_10 AC 151 ms
102,996 KB
testcase_11 AC 41 ms
57,252 KB
testcase_12 AC 129 ms
87,724 KB
testcase_13 AC 41 ms
56,704 KB
testcase_14 AC 86 ms
81,052 KB
testcase_15 AC 63 ms
72,356 KB
testcase_16 AC 62 ms
74,280 KB
testcase_17 AC 90 ms
79,628 KB
testcase_18 AC 72 ms
79,808 KB
testcase_19 AC 41 ms
56,336 KB
testcase_20 AC 92 ms
78,236 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 40 ms
55,720 KB
testcase_24 AC 127 ms
84,764 KB
testcase_25 AC 42 ms
56,880 KB
testcase_26 AC 61 ms
72,364 KB
testcase_27 RE -
testcase_28 AC 99 ms
87,844 KB
testcase_29 AC 128 ms
94,096 KB
testcase_30 AC 117 ms
89,224 KB
testcase_31 AC 121 ms
81,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
# sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd

h,w,k,l,r = map(int,input().split())

if (r-l + 1) % 2 != 0:
    print("No")
    exit()
s = [input().rstrip() for i in range(h)]
dx = [0,0,1,-1]
dy = [1,-1,0,0]
def bfs(x,y):
    que = deque([x*w+y])
    depth = [-1]*(w*h)
    depth[x*w+y] = 0
    pre = [-1]*(h*w)
    while que:
        crr= que.popleft()
        x,y = crr//w, crr%w
        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]
            nxt = nx*w+ny
            if not(0<=nx<h and 0<=ny<w): continue
            if s[nx][ny] == "#": continue
            if depth[nxt] == -1:
                depth[nxt] = depth[crr] + 1
                pre[nxt] = crr
                que.append(nxt)
    return depth,pre

cnt = l-1 + k-r
d1,p1 = bfs(0,0)
d2,p2 = bfs(h-1,w-1)


mid = []
mpos = ()
for i in range(h):
    for j in range(w):
        pos = i*w+j
        ok = 1
        ok &= d1[pos] % 2 == (l-1)%2 
        ok &= d2[pos] % 2 == (k-r)%2
        ok &= d1[pos] <= (l-1) 
        ok &= d2[pos] <= (k-r)
        if not ok: continue
        if i != 0 and i != h-1 and s[i+1][j] == "." and s[i-1][j] == ".":
            mid = ["UD"]*((r-l+1)//2)
            mpos = i*w+j
        if j != 0 and j != w-1 and s[i][j-1] == "." and s[i][j+1] == ".":
            mid = ["LR"]*((r-l+1)//2)
            mpos = i*w+j
        if mid:break
    if mid:break
if not mid:
    print("No")
    exit()

pre = []
now = mpos
while now != 0:
    now = p1[now]
    if now == -1: break
    pre.append(now)
pre = pre[::-1]
pre.append(mpos)

suf = [mpos]
now = mpos
while now != (h-1)*w+w-1:
    now = p2[now]
    if now == -1: break
    suf.append(now)

anspre = []
anssuf = []

for i in range(1,len(pre)):
    cx,cy = pre[i]//w, pre[i]%w
    px,py = pre[i-1]//w, pre[i-1]%w
    if cx == px + 1:
        anspre.append("D")
    if cx == px - 1:
        anspre.append("U")
    if cy == py + 1:
        anspre.append("R")
    if cy == py - 1:
        anspre.append("L")

        
for i in range(1,len(suf)):
    cx,cy = suf[i]//w, suf[i]%w
    px,py = suf[i-1]//w, suf[i-1]%w
    if cx == px + 1:
        anssuf.append("D")
    if cx == px - 1:
        anssuf.append("U")
    if cy == py + 1:
        anssuf.append("R")
    if cy == py - 1:
        anssuf.append("L")

if len(anspre) <= l - 1:
    dif = l-1-len(anspre)
    if anspre[0] == "R":
        tmp = ["RL"]*(dif // 2)
    if anspre[0] == "D":
        tmp = ["DU"]*(dif // 2)
    anspre = tmp + anspre

if len(anssuf) <= k-r:
    dif = k-r-len(anssuf)
    if anssuf[-1] == "D":
        tmp = ["UD"]*(dif // 2)
    else:
        tmp = ["LR"]*(dif // 2)
    anssuf = anssuf + tmp

ans = anspre + mid + anssuf
print('Yes')
print("".join(ans))    
0