結果
問題 | No.367 ナイトの転身 |
ユーザー | ayaoni |
提出日時 | 2020-12-10 16:30:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 200 ms / 2,000 ms |
コード長 | 2,024 bytes |
コンパイル時間 | 275 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 85,504 KB |
最終ジャッジ日時 | 2024-09-19 20:37:44 |
合計ジャッジ時間 | 3,609 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,400 KB |
testcase_01 | AC | 43 ms
54,272 KB |
testcase_02 | AC | 43 ms
54,272 KB |
testcase_03 | AC | 42 ms
54,656 KB |
testcase_04 | AC | 43 ms
54,144 KB |
testcase_05 | AC | 40 ms
54,400 KB |
testcase_06 | AC | 43 ms
55,040 KB |
testcase_07 | AC | 43 ms
54,016 KB |
testcase_08 | AC | 43 ms
54,272 KB |
testcase_09 | AC | 47 ms
53,888 KB |
testcase_10 | AC | 157 ms
82,432 KB |
testcase_11 | AC | 200 ms
85,504 KB |
testcase_12 | AC | 174 ms
79,872 KB |
testcase_13 | AC | 150 ms
79,872 KB |
testcase_14 | AC | 160 ms
78,976 KB |
testcase_15 | AC | 103 ms
76,928 KB |
testcase_16 | AC | 157 ms
79,804 KB |
testcase_17 | AC | 105 ms
77,440 KB |
testcase_18 | AC | 108 ms
77,312 KB |
testcase_19 | AC | 120 ms
77,824 KB |
testcase_20 | AC | 97 ms
76,928 KB |
testcase_21 | AC | 123 ms
77,956 KB |
testcase_22 | AC | 52 ms
62,848 KB |
testcase_23 | AC | 86 ms
76,544 KB |
testcase_24 | AC | 92 ms
77,056 KB |
testcase_25 | AC | 77 ms
73,344 KB |
testcase_26 | AC | 61 ms
66,688 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))