結果

問題 No.367 ナイトの転身
ユーザー convexineqconvexineq
提出日時 2021-01-02 16:09:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 1,139 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 82,192 KB
実行使用メモリ 86,704 KB
最終ジャッジ日時 2024-04-20 09:33:24
合計ジャッジ時間 4,323 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,556 KB
testcase_01 AC 45 ms
55,408 KB
testcase_02 AC 44 ms
56,028 KB
testcase_03 AC 45 ms
54,180 KB
testcase_04 AC 44 ms
55,728 KB
testcase_05 AC 45 ms
54,192 KB
testcase_06 AC 50 ms
56,712 KB
testcase_07 AC 44 ms
54,796 KB
testcase_08 AC 44 ms
56,028 KB
testcase_09 AC 44 ms
54,412 KB
testcase_10 AC 151 ms
82,996 KB
testcase_11 AC 242 ms
86,704 KB
testcase_12 AC 206 ms
80,668 KB
testcase_13 AC 178 ms
80,504 KB
testcase_14 AC 183 ms
80,308 KB
testcase_15 AC 113 ms
76,912 KB
testcase_16 AC 189 ms
80,316 KB
testcase_17 AC 109 ms
76,956 KB
testcase_18 AC 117 ms
76,848 KB
testcase_19 AC 127 ms
77,824 KB
testcase_20 AC 113 ms
77,084 KB
testcase_21 AC 138 ms
77,708 KB
testcase_22 AC 54 ms
64,108 KB
testcase_23 AC 93 ms
76,712 KB
testcase_24 AC 92 ms
76,744 KB
testcase_25 AC 90 ms
76,656 KB
testcase_26 AC 60 ms
66,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

h,w = map(int,input().split())
b = [input() for _ in range(h)]
for i in range(h):
    for j in range(w):
        if b[i][j] == "S":
            si,sj = i,j
        elif b[i][j] == "G":
            gi,gj = i,j
from collections import deque
INF = 10**9
dist = [[[INF]*w for _ in range(h)] for _ in range(2)]
dist[0][si][sj] = 0
q = deque([(0,si,sj)])
while q:
    kind,i,j = q.popleft()
    d = dist[kind][i][j]
    if kind == 0:
        for ni,nj in [(i-1,j-2),(i-1,j+2),(i+1,j-2),(i+1,j+2),(i-2,j-1),(i-2,j+1),(i+2,j-1),(i+2,j+1)]:
            if 0 <= ni < h and 0 <= nj < w:
                nkind = kind^(b[ni][nj] == "R")
                if dist[nkind][ni][nj] == INF:
                    dist[nkind][ni][nj] = d+1
                    q.append((nkind,ni,nj))
    else:
        for ni,nj in [(i-1,j-1),(i+1,j-1),(i-1,j+1),(i+1,j+1)]:
            if 0 <= ni < h and 0 <= nj < w:
                nkind = kind^(b[ni][nj] == "R")
                if dist[nkind][ni][nj] == INF:
                    dist[nkind][ni][nj] = d+1
                    q.append((nkind,ni,nj))
ans = min(dist[0][gi][gj],dist[1][gi][gj])
print(ans if ans != INF else -1)
0