結果

問題 No.2411 Reverse Directions
ユーザー flygon
提出日時 2023-08-11 22:34:33
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,891 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 931,336 KB
最終ジャッジ日時 2024-11-18 17:24:10
合計ジャッジ時間 13,909 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 TLE * 3 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

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,y)])
    depth = [[-1 for i in range(w)] for i in range(h)]
    depth[x][y] = 0
    pre = [[-1]*w for i in range(h)]
    while que:
        x,y= que.popleft()
        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]
            if not(0<=nx<h and 0<=ny<w): continue
            if s[nx][ny] == "#": continue
            if depth[nx][ny] == -1:
                depth[nx][ny] = depth[x][y] + 1
                pre[nx][ny] = x*w+y
                que.append((nx,ny))
    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):
        ok = 1
        ok &= d1[i][j] % 2 == (l-1)%2 
        ok &= d2[i][j] % 2 == (k-r)%2
        ok &= d1[i][j] <= (l-1) 
        ok &= d2[i][j] <= (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,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,j]
        if mid:break
    if mid:break
if not mid:
    print("No")
    exit()

pre = []
nowx,nowy = mpos
while [nowx,nowy] != [0,0]:
    ppos = p1[nowx][nowy]
    nowx, nowy = ppos//w, ppos%w
    pre.append([nowx,nowy])
pre = pre[::-1]
pre.append(mpos)
suf = [mpos]
nowx,nowy = mpos
while [nowx,nowy] != [h-1,w-1]:
    ppos = p2[nowx][nowy]
    nowx, nowy = ppos//w, ppos%w
    suf.append([nowx,nowy])

anspre = []
anssuf = []

for i in range(1,len(pre)):
    cx,cy = pre[i]
    px,py = pre[i-1]
    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]
    px,py = suf[i-1]
    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