結果
問題 | No.367 ナイトの転身 |
ユーザー | ayaoni |
提出日時 | 2020-12-10 16:30:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 214 ms / 2,000 ms |
コード長 | 2,024 bytes |
コンパイル時間 | 2,096 ms |
コンパイル使用メモリ | 81,216 KB |
実行使用メモリ | 85,068 KB |
最終ジャッジ日時 | 2023-10-20 00:48:10 |
合計ジャッジ時間 | 3,951 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
55,444 KB |
testcase_01 | AC | 39 ms
55,444 KB |
testcase_02 | AC | 40 ms
55,444 KB |
testcase_03 | AC | 40 ms
55,444 KB |
testcase_04 | AC | 39 ms
55,444 KB |
testcase_05 | AC | 39 ms
55,444 KB |
testcase_06 | AC | 40 ms
55,444 KB |
testcase_07 | AC | 40 ms
55,444 KB |
testcase_08 | AC | 39 ms
55,444 KB |
testcase_09 | AC | 39 ms
55,444 KB |
testcase_10 | AC | 150 ms
81,744 KB |
testcase_11 | AC | 214 ms
85,068 KB |
testcase_12 | AC | 183 ms
79,104 KB |
testcase_13 | AC | 158 ms
79,120 KB |
testcase_14 | AC | 165 ms
78,576 KB |
testcase_15 | AC | 98 ms
76,500 KB |
testcase_16 | AC | 161 ms
78,976 KB |
testcase_17 | AC | 105 ms
76,596 KB |
testcase_18 | AC | 110 ms
76,580 KB |
testcase_19 | AC | 121 ms
76,828 KB |
testcase_20 | AC | 95 ms
76,428 KB |
testcase_21 | AC | 127 ms
77,344 KB |
testcase_22 | AC | 51 ms
63,848 KB |
testcase_23 | AC | 81 ms
76,140 KB |
testcase_24 | AC | 88 ms
76,348 KB |
testcase_25 | AC | 72 ms
72,944 KB |
testcase_26 | AC | 56 ms
66,012 KB |
ソースコード
import sys from collections import deque sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) H,W = MI() A = [S() for _ in range(H)] for i in range(H): for j in range(W): if A[i][j] == 'S': si,sj = i,j if A[i][j] == 'G': gi,gj = i,j directions_K = [(-2,-1),(-2,1),(-1,-2),(-1,2),(2,-1),(2,1),(1,-2),(1,2)] directions_B = [(-1,-1),(-1,1),(1,-1),(1,1)] dist = [[[-1]*W for _ in range(H)] for _ in range(2)] dist[0][si][sj] = 0 deq = deque([(0,si,sj)]) while deq: k,i,j = deq.pop() if k == 0: for di,dj in directions_K: ni,nj = i+di,j+dj if 0 <= ni < H and 0 <= nj < W: if A[ni][nj] == 'R' and dist[1][ni][nj] == -1: dist[1][ni][nj] = dist[k][i][j]+1 deq.appendleft((1,ni,nj)) if A[ni][nj] != 'R' and dist[0][ni][nj] == -1: dist[0][ni][nj] = dist[k][i][j]+1 deq.appendleft((0,ni,nj)) else: for di,dj in directions_B: ni,nj = i+di,j+dj if 0 <= ni < H and 0 <= nj < W: if A[ni][nj] == 'R' and dist[0][ni][nj] == -1: dist[0][ni][nj] = dist[k][i][j]+1 deq.appendleft((0,ni,nj)) if A[ni][nj] != 'R' and dist[1][ni][nj] == -1: dist[1][ni][nj] = dist[k][i][j]+1 deq.appendleft((1,ni,nj)) ans0,ans1 = dist[0][gi][gj],dist[1][gi][gj] if ans0 == -1: if ans1 == -1: print(-1) else: print(ans1) else: if ans1 == -1: print(ans0) else: print(min(ans0,ans1))