結果
問題 | No.367 ナイトの転身 |
ユーザー | はむ吉🐹 |
提出日時 | 2016-04-29 23:32:56 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,792 bytes |
コンパイル時間 | 220 ms |
コンパイル使用メモリ | 82,008 KB |
実行使用メモリ | 276,356 KB |
最終ジャッジ日時 | 2024-10-04 19:08:44 |
合計ジャッジ時間 | 4,265 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
61,256 KB |
testcase_01 | AC | 50 ms
54,856 KB |
testcase_02 | AC | 44 ms
55,124 KB |
testcase_03 | AC | 43 ms
56,164 KB |
testcase_04 | AC | 73 ms
63,104 KB |
testcase_05 | AC | 43 ms
55,688 KB |
testcase_06 | AC | 49 ms
56,808 KB |
testcase_07 | TLE | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
ソースコード
#!/usr/bin/env pypy3 import collections import itertools DS_KNIGHT = [(1, 2), (-1, -2), (1, -2), (-1, 2), (2, 1), (-2, -1), (2, -1), (-2, 1)] DS_MB = [(1, 1), (-1, -1), (1, -1), (-1, 1)] INF = 10 ** 8 Position = collections.namedtuple("Position", "r c") Status = collections.namedtuple("Status", "is_knight position") def min_num_of_operations(height, width, board, start, goal): s_start = Status(True, start) dist = collections.defaultdict(lambda: INF) dist[s_start] = 0 q = collections.deque() q.append(s_start) while q: s0 = q.popleft() if s0.position == goal: break for dr, dc in (DS_KNIGHT if s0.is_knight else DS_MB): p = Position(s0.position.r + dr, s0.position.c + dc) if p.r < 0 or p.r >= height: continue elif p.c < 0 or p.c >= width: continue elif dist[Status(True, p)] >= INF or dist[Status(False, p)] >= INF: new_class = not s0.is_knight if board[p.r][p.c] == "R" else s0.is_knight s = Status(new_class, p) dist[s] = dist[s0] + 1 q.append(s) return min(dist[Status(True, goal)], dist[Status(False, goal)]) def main(): height, width = map(int, input().split()) board = [input() for _ in range(height)] start = None goal = None for r, c in itertools.product(range(height), range(width)): if board[r][c] == "S": start = Position(r, c) elif board[r][c] == "G": goal = Position(r, c) if (start is not None) and (goal is not None): break answer = min_num_of_operations(height, width, board, start, goal) print(answer if answer < INF else -1) if __name__ == '__main__': main()