結果
問題 |
No.367 ナイトの転身
|
ユーザー |
![]() |
提出日時 | 2025-06-23 22:15:40 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 664 ms / 2,000 ms |
コード長 | 1,092 bytes |
コンパイル時間 | 407 ms |
コンパイル使用メモリ | 82,040 KB |
実行使用メモリ | 125,108 KB |
最終ジャッジ日時 | 2025-06-23 22:15:44 |
合計ジャッジ時間 | 4,393 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
ソースコード
H, W = map(int, input().split()) S = [input() for _ in range(H)] RC = set() dir = [[(1, 2), (2, 1), (1, -2), (2, -1), (-1, 2), (-2, 1), (-1, -2), (-2, -1)], [(1, 1), (1, -1), (-1, 1), (-1, -1)]] for i in range(H): for j in range(W): if S[i][j] == "S": ST = (i, j, 0) elif S[i][j] == "G": GL = (i, j) elif S[i][j] == "R": RC.add((i, j)) D = [[[-1] * W for _ in range(H)] for _ in range(2)] D[0][ST[0]][ST[1]] = 0 Q = set([ST]) Q2 = set() cnt = 1 while Q: while Q: x, y, z = Q.pop() for dx, dy in dir[z]: x2 = x + dx y2 = y + dy if (x2, y2) == GL: print(cnt) exit() elif (x2, y2) in RC: if D[z ^ 1][x2][y2] == -1: D[z ^ 1][x2][y2] = cnt Q2.add((x2, y2, z ^ 1)) elif 0 <= x2 < H and 0 <= y2 < W: if D[z][x2][y2] == -1: D[z][x2][y2] = cnt Q2.add((x2, y2, z)) cnt += 1 Q2, Q = Q, Q2 print(-1)