結果

問題 No.5015 Escape from Labyrinth
ユーザー nikoro256
提出日時 2023-04-15 20:09:42
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,111 bytes
コンパイル時間 1,075 ms
コンパイル使用メモリ 86,532 KB
実行使用メモリ 81,980 KB
スコア 500
最終ジャッジ日時 2023-04-15 20:10:05
合計ジャッジ時間 21,799 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 6 RE * 69 TLE * 1 -- * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

def firstsolve():
    re=[]
    point=BFS(St,K)
    for b in BFS_Restoration(K,point)[::-1]:
        re.append(b)
    point=BFS(K,G)
    for b in BFS_Restoration(G,point)[::-1]:
        re.append(b)
    return re
    
def BFS_Restoration(end,point):
    thispoint=point[end[0]*N+end[1]]
    pos=end
    re=[pos]
    while thispoint!=0:
        for d in [[1,0],[-1,0],[0,1],[0,-1]]:
            x=pos[0]+d[0]
            y=pos[1]+d[1]
            if 0<x<=N and 0<=y<N and point[x*N+y]==thispoint-1:
                re.append((x,y))
                thispoint=point[x*N+y]
                pos=(x,y)
                break
    return re

def BFS(pos,end):
    point=[-1 for _ in range(N*N)]
    dq=deque()
    dq.append(pos)
    while len(dq)!=0:
        p=dq.popleft()
        if p==end:
            break
        for d in [[1,0],[-1,0],[0,1],[0,-1]]:
            x=p[0]+d[0]
            y=p[1]+d[1]
            if 0<x<=N and 0<=y<N and S[x][y]!='#' and S[x][y]!='E' and point[x*N+y]==-1:
                dq.append((x,y))
                point[x*N+y]=point[p[0]*N+p[1]]+1
    return point

def changetoans(way):
    pos=St
    ans=[]
    for w in way:
        d=[[1,0],[-1,0],[0,1],[0,-1]]
        change=[w[0]-pos[0],w[1]-pos[1]]
        for i in range(4):
            if change==d[i]:
                break
        ans_d=[]
        if 0<=i<=3:
            ans_d.append('M')
        if i==0:
            ans_d.append('D')
        elif i==1:
            ans_d.append('U')
        elif i==2:
            ans_d.append('R')
        elif i==3:
            ans_d.append('L')
        ans.append(ans_d)
        pos=w
    return ans

import sys
from collections import deque
input=sys.stdin.readline
N,D,H=map(int,input().split())
S=[]
St,G,K=(),(),()
for i in range(N):
    S.append(input())
    for j in range(N):
        if S[i][j]=='S':
            St=(i,j)
        elif S[i][j]=='G':
            G=(i,j)
        elif S[i][j]=='K':
            K=(i,j)
M=int(input())
dic={}
for _ in range(M):
    y,x,d=map(int,input().split())
    dic[(y,x)]=d
way=firstsolve()
ans=changetoans(way)
for a in ans:
    print(' '.join(a))
0