結果

問題 No.2411 Reverse Directions
ユーザー timi
提出日時 2024-02-17 15:54:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 2,946 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 156,948 KB
最終ジャッジ日時 2024-09-28 23:44:28
合計ジャッジ時間 5,615 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

H,W,K,L,R=map(int, input().split())
A=[]
for i in range(H):
  S=input()
  A.append(S)
  
from collections import deque
d=deque()
B=[[10**10]*W for i in range(H)]
B[0][0]=0;BB={}
d.append((0,0))
dy,dx=[0,0,1,-1,],[1,-1,0,0]
for i in range(L-1):
  nd=deque()
  while d:
    y,x=d.popleft()
    for i in range(4):
      ny,nx=y+dy[i],x+dx[i]
      if 0<=ny<H and 0<=nx<W and A[ny][nx]=='.':
        if B[ny][nx]>B[y][x]+1:
          nd.append((ny,nx))
          B[ny][nx]=B[y][x]+1
          BB[(ny,nx)]=(y,x)
  d=nd

d=deque()
C=[[10**10]*W for i in range(H)]
C[H-1][W-1]=0;CC={}
d.append((H-1,W-1))
dy,dx=[0,0,1,-1],[1,-1,0,0]
for i in range(K-R):
  nd=deque()
  while d:
    y,x=d.popleft()
    for i in range(4):
      ny,nx=y+dy[i],x+dx[i]
      if 0<=ny<H and 0<=nx<W and A[ny][nx]=='.':
        if C[ny][nx]>C[y][x]+1:
          nd.append((ny,nx))
          C[ny][nx]=C[y][x]+1
          CC[(ny,nx)]=(y,x)
  d=nd

if (R-L)%2==0:
  print('No')
  exit()
hh,ww=-1,-1;d=0
for h in range(H):
  for w in range(W):
    if B[h][w]!=10**10 and C[h][w]!=10**10:
      if B[h][w]%2==(L-1)%2 and C[h][w]%2==(K-R)%2:
        if 0<=h-1<H and 0<=h+1<H and A[h-1][w]==A[h+1][w]=='.':
          hh,ww=h,w;d=1
        if 0<=w-1<W and 0<=w+1<W and A[h][w-1]==A[h][w+1]=='.':
          hh,ww=h,w;d=-1

if hh==-1:
  print('No')
  exit()
  
X,Y=[(hh+1,ww+1)],[]
nowy,nowx=hh,ww 
for i in range(B[hh][ww]):
  nowy,nowx=BB[(nowy,nowx)] 
  X.append((nowy+1,nowx+1))
nowy,nowx=hh,ww 
for i in range(C[hh][ww]):
  nowy,nowx=CC[(nowy,nowx)] 
  Y.append((nowy+1,nowx+1))
X=X[::-1]

c=len(X)
if L-c>0:
  if c<=1:
    X.append(Y[0])
    X.append(X[-2])
    for i in range(L-c-2):
      X.append(X[-2])
  else:
    for i in range(L-c):
      X.append(X[-2])
c=len(Y)
if K-R-c>0:
  if c<=1:
    Y.append(X[-1])
    Y.append(Y[-2])
    for i in range(K-R-c-2):
      Y.append(Y[-2])
  else:
    for i in range(K-R-c):
      Y.append(Y[-2])

p,q=X[-1]
for i in range((R-L+1)//2):
  if d==1:
    X.append((p-1,q))
    X.append((p,q))
  else:
    X.append((p,q-1))
    X.append((p,q))

ans=[];XX=X+Y
for i in range(len(XX)-1):
  p,q=XX[i];r,s=XX[i+1]
  if q+1==s:
    ans.append('R')
  elif q-1==s:
    ans.append('L')
  elif p+1==r:
    ans.append('D')
  else:
    ans.append('U')
print('Yes')
#print(len(ans))
print(''.join(ans))
# X,Y,Z=ans[:L-1],ans[L-1:R],ans[R:]
# P=''.join(X+Y+Z)
# YY=[]
# for y in Y:
#   if y=='U':
#     YY.append('D')
#   if y=='D':
#     YY.append('U')
#   if y=='L':
#     YY.append('R')
#   if y=='R':
#     YY.append('L')
# Q=''.join(X+YY+Z)
# print(P)
# print(Q)
# print(d)
# y,x,yy,xx=0,0,0,0 
# now=0
# for i in range(len(P)):
#   if P[i]=='U':
#     y-=1
#   if P[i]=='D':
#     y+=1
#   if P[i]=='L':
#     x-=1
#   if P[i]=='R':
#     x+=1
#   if Q[i]=='U':
#     yy-=1
#   if Q[i]=='D':
#     yy+=1
#   if Q[i]=='L':
#     xx-=1
#   if Q[i]=='R':
#     xx+=1
#   if A[y][x]=='#':
#     print('!')
#   if A[yy][xx]=='#':
#     print('!!')


0