結果

問題 No.367 ナイトの転身
ユーザー lloyzlloyz
提出日時 2023-02-13 22:34:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 387 ms / 2,000 ms
コード長 1,070 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 86,956 KB
実行使用メモリ 113,088 KB
最終ジャッジ日時 2023-09-23 14:02:08
合計ジャッジ時間 6,043 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
71,640 KB
testcase_01 AC 86 ms
71,664 KB
testcase_02 AC 84 ms
71,288 KB
testcase_03 AC 87 ms
71,584 KB
testcase_04 AC 86 ms
71,660 KB
testcase_05 AC 85 ms
71,588 KB
testcase_06 AC 93 ms
72,248 KB
testcase_07 AC 84 ms
71,388 KB
testcase_08 AC 84 ms
71,612 KB
testcase_09 AC 84 ms
71,664 KB
testcase_10 AC 323 ms
109,292 KB
testcase_11 AC 387 ms
113,088 KB
testcase_12 AC 161 ms
88,984 KB
testcase_13 AC 172 ms
90,268 KB
testcase_14 AC 212 ms
89,676 KB
testcase_15 AC 123 ms
78,160 KB
testcase_16 AC 213 ms
89,056 KB
testcase_17 AC 134 ms
78,988 KB
testcase_18 AC 140 ms
79,620 KB
testcase_19 AC 139 ms
80,772 KB
testcase_20 AC 122 ms
78,700 KB
testcase_21 AC 146 ms
81,684 KB
testcase_22 AC 97 ms
77,012 KB
testcase_23 AC 106 ms
77,876 KB
testcase_24 AC 116 ms
77,772 KB
testcase_25 AC 115 ms
77,356 KB
testcase_26 AC 104 ms
77,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

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':
            si, sj = i, j
        elif S[i][j] == 'G':
            gi, gj = i, j
INF = 10**18
DP = [[[INF for _ in range(2)] for _ in range(w)] for _ in range(h)]
DP[si][sj][0] = 0
Directions = [[(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)],
              [(1, 1), (1, -1), (-1, -1), (-1, 1)]]
Que = deque([(si, sj, 0)])
while Que:
    ci, cj, idx = Que.popleft()
    if ci == gi and cj == gj:
        break
    cc = DP[ci][cj][idx]
    for di, dj in Directions[idx]:
        ni, nj = ci + di, cj + dj
        if 0 <= ni < h and 0 <= nj < w:
            if S[ni][nj] == 'R':
                nidx = idx ^ 1
            else:
                nidx = idx
            if cc + 1 >= DP[ni][nj][nidx]:
                continue
            DP[ni][nj][nidx] = cc + 1
            Que.append((ni, nj, nidx))

if min(DP[gi][gj]) == INF:
    print(-1)
else:
    print(min(DP[gi][gj]))
0