結果

問題 No.367 ナイトの転身
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-09-02 10:37:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 1,892 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,852 KB
実行使用メモリ 100,016 KB
最終ジャッジ日時 2024-04-27 13:39:15
合計ジャッジ時間 3,874 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,768 KB
testcase_01 AC 41 ms
55,592 KB
testcase_02 AC 40 ms
54,888 KB
testcase_03 AC 37 ms
55,712 KB
testcase_04 AC 38 ms
56,040 KB
testcase_05 AC 37 ms
55,208 KB
testcase_06 AC 41 ms
58,276 KB
testcase_07 AC 35 ms
55,500 KB
testcase_08 AC 39 ms
54,704 KB
testcase_09 AC 39 ms
55,256 KB
testcase_10 AC 227 ms
94,324 KB
testcase_11 AC 358 ms
100,016 KB
testcase_12 AC 247 ms
86,736 KB
testcase_13 AC 218 ms
85,528 KB
testcase_14 AC 215 ms
85,500 KB
testcase_15 AC 98 ms
77,508 KB
testcase_16 AC 218 ms
84,356 KB
testcase_17 AC 108 ms
77,704 KB
testcase_18 AC 117 ms
78,324 KB
testcase_19 AC 134 ms
79,696 KB
testcase_20 AC 114 ms
78,044 KB
testcase_21 AC 136 ms
79,056 KB
testcase_22 AC 48 ms
64,588 KB
testcase_23 AC 86 ms
77,180 KB
testcase_24 AC 93 ms
77,092 KB
testcase_25 AC 81 ms
76,908 KB
testcase_26 AC 57 ms
68,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == 'S':
            s = (i,j)
        if S[i][j] == 'G':
            g = (i,j)
            

from collections import deque
v = deque()

dist = [[[-1]*W for _ in range(H)] for j in range(2)]

v.append((0,s[0],s[1]))
dist[0][s[0]][s[1]]=0


def nb1(i,j):
    dx = [1,1,2,2,-1,-1,-2,-2]
    dy = [2,-2,1,-1,2,-2,1,-1]
    tmp = []
    for t in range(8):
        tx = dx[t]+i
        ty = dy[t]+j
        if (0<=tx<H)&(0<=ty<W):
            tmp.append((tx,ty))
    return tmp
 
 
def nb2(i,j):
    dx = [1,1,-1,-1]
    dy = [1,-1,1,-1]
    tmp = []
    for t in range(4):
        tx = dx[t]+i
        ty = dy[t]+j
        if (0<=tx<H)&(0<=ty<W):
            tmp.append((tx,ty))
    return tmp   


while v:
    
    ic,ix,iy = v.popleft()
    td = dist[ic][ix][iy]
    
    if ic==0:
        for jx,jy in nb1(ix,iy):
            
            if S[jx][jy]=='R':
                
                if dist[1][jx][jy]==-1:
                    dist[1][jx][jy] = td+1
                    v.append((1,jx,jy))
            else:
                if dist[0][jx][jy]==-1:
                    dist[0][jx][jy] = td+1
                    v.append((0,jx,jy))        
    else:
        
        for jx,jy in nb2(ix,iy):
            
            if S[jx][jy]=='R':
                
                if dist[0][jx][jy]==-1:
                    dist[0][jx][jy] = td+1
                    v.append((0,jx,jy))
            else:
                if dist[1][jx][jy]==-1:
                    dist[1][jx][jy] = td+1
                    v.append((1,jx,jy))            
                
a = dist[0][g[0]][g[1]]
b = dist[1][g[0]][g[1]]

if (a==-1)&(b==-1):
    print(-1)
elif (a!=-1)&(b!=-1):
    print(min(a,b))
else:
    print(max(a,b))
0