結果
問題 | No.367 ナイトの転身 |
ユーザー | maspy |
提出日時 | 2020-03-19 13:29:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 400 ms / 2,000 ms |
コード長 | 1,427 bytes |
コンパイル時間 | 440 ms |
コンパイル使用メモリ | 82,264 KB |
実行使用メモリ | 147,768 KB |
最終ジャッジ日時 | 2024-12-14 03:08:31 |
合計ジャッジ時間 | 4,258 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
54,188 KB |
testcase_01 | AC | 43 ms
54,668 KB |
testcase_02 | AC | 42 ms
54,184 KB |
testcase_03 | AC | 43 ms
55,708 KB |
testcase_04 | AC | 44 ms
56,148 KB |
testcase_05 | AC | 43 ms
54,916 KB |
testcase_06 | AC | 57 ms
63,560 KB |
testcase_07 | AC | 44 ms
55,184 KB |
testcase_08 | AC | 42 ms
54,608 KB |
testcase_09 | AC | 45 ms
55,048 KB |
testcase_10 | AC | 329 ms
144,476 KB |
testcase_11 | AC | 400 ms
147,768 KB |
testcase_12 | AC | 228 ms
102,744 KB |
testcase_13 | AC | 195 ms
102,828 KB |
testcase_14 | AC | 195 ms
101,468 KB |
testcase_15 | AC | 85 ms
77,072 KB |
testcase_16 | AC | 182 ms
95,672 KB |
testcase_17 | AC | 96 ms
79,096 KB |
testcase_18 | AC | 101 ms
79,808 KB |
testcase_19 | AC | 114 ms
80,652 KB |
testcase_20 | AC | 99 ms
79,780 KB |
testcase_21 | AC | 122 ms
80,388 KB |
testcase_22 | AC | 56 ms
63,264 KB |
testcase_23 | AC | 76 ms
72,728 KB |
testcase_24 | AC | 75 ms
72,024 KB |
testcase_25 | AC | 76 ms
71,892 KB |
testcase_26 | AC | 65 ms
67,648 KB |
ソースコード
#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import itertools from collections import deque H, W = map(int, readline().split()) S = ''.join(read().decode().split()) start = S.index('S') goal = S.index('G') N = H * W Knight = ((2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2), (1, -2), (2, -1)) Bishop = ((1, 1), (-1, 1), (-1, -1), (1, -1)) # 0 <= i < N:knightで出発 # N <= i < 2N:bishopで出発 graph = [[] for _ in range(N + N)] for x, y in itertools.product(range(H), range(W)): for dx, dy in Knight: x1 = x + dx y1 = y + dy if not ((0 <= x1 < H) and (0 <= y1 < W)): continue i = x * W + y j = x1 * W + y1 if S[j] == 'R': j += N graph[i].append(j) for dx, dy in Bishop: x1 = x + dx y1 = y + dy if not ((0 <= x1 < H) and (0 <= y1 < W)): continue i = x * W + y + N j = x1 * W + y1 if S[j] != 'R': j += N graph[i].append(j) INF = 10 ** 6 dist = [INF] * (N + N) dist[start] = 0 q = deque([start]) while q: v = q.popleft() dw = dist[v] + 1 for w in graph[v]: if dist[w] <= dw: continue dist[w] = dw q.append(w) answer = min(dist[goal], dist[goal + N]) if answer == INF: answer = -1 print(answer)