結果
問題 | No.2411 Reverse Directions |
ユーザー |
|
提出日時 | 2023-08-11 23:14:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 169 ms / 2,000 ms |
コード長 | 3,322 bytes |
コンパイル時間 | 182 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 103,212 KB |
最終ジャッジ日時 | 2024-11-18 18:33:28 |
合計ジャッジ時間 | 4,308 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
import sys# sys.setrecursionlimit(5*10**5)input = sys.stdin.readlinefrom collections import defaultdict, deque, Counterfrom heapq import heappop, heappushfrom bisect import bisect_left, bisect_rightfrom math import gcdh,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] = 0pre = [-1]*(h*w)while que:crr= que.popleft()x,y = crr//w, crr%wfor i in range(4):nx = x+dx[i]ny = y+dy[i]nxt = nx*w+nyif not(0<=nx<h and 0<=ny<w): continueif s[nx][ny] == "#": continueif depth[nxt] == -1:depth[nxt] = depth[crr] + 1pre[nxt] = crrque.append(nxt)return depth,precnt = l-1 + k-rd1,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+jok = 1if d1[pos] == -1 or d2[pos] == -1: continueok &= d1[pos] % 2 == (l-1)%2ok &= d2[pos] % 2 == (k-r)%2ok &= d1[pos] <= (l-1)ok &= d2[pos] <= (k-r)if not ok: continueif 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+jif 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+jif mid:breakif mid:breakif not mid:print("No")exit()pre = []now = mposwhile now != 0:now = p1[now]if now == -1: breakpre.append(now)pre = pre[::-1]pre.append(mpos)suf = [mpos]now = mposwhile now != (h-1)*w+w-1:now = p2[now]if now == -1: breaksuf.append(now)anspre = []anssuf = []for i in range(1,len(pre)):cx,cy = pre[i]//w, pre[i]%wpx,py = pre[i-1]//w, pre[i-1]%wif 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]%wpx,py = suf[i-1]//w, suf[i-1]%wif 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:if anspre:dif = l-1-len(anspre)if anspre[0] == "R":tmp = ["RL"]*(dif // 2)if anspre[0] == "D":tmp = ["DU"]*(dif // 2)anspre = tmp + anspreelse:dif = l-1if s[0][1]:tmp = ["RL"]*(dif // 2)else:tmp = ["DU"]*(dif // 2)anssuf = tmpif len(anssuf) <= k-r:if anssuf:dif = k-r-len(anssuf)if anssuf[-1] == "D":tmp = ["UD"]*(dif // 2)else:tmp = ["LR"]*(dif // 2)anssuf = anssuf + tmpelse:dif = k-rif s[-1][-2]:tmp = ["LR"]*(dif // 2)else:tmp = ["UD"]*(dif // 2)anssuf = tmpans = anspre + mid + anssufprint('Yes')print("".join(ans))