結果
| 問題 | No.124 門松列(3) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-07-14 22:11:19 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 38 ms / 5,000 ms |
| コード長 | 1,381 bytes |
| コンパイル時間 | 73 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 12,032 KB |
| 最終ジャッジ日時 | 2024-07-08 07:19:37 |
| 合計ジャッジ時間 | 1,783 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
def read_data():
W, H = map(int, input().split())
M = [0] * (W + 3)
for row in range(H):
M.extend(list(map(int, input().split())))
M.extend([0, 0])
M.extend([0] * (W + 1))
return W, H, M
def solve(W, H, M):
H2 = H + 2
W2 = W + 2
H1 = H + 1
W1 = W + 1
goal = H1 * W2 - 2
start = W2 + 1
frontiers = []
if M[start + 1] != M[start]: frontiers.append((start + 1, start))
if M[start + W2] != M[start]: frontiers.append((start + W2, start))
visited = set(frontiers)
dist = 1
while frontiers:
dist += 1
new_frontiers = []
for pos1, pos2 in frontiers:
val1 = M[pos1]
val2 = M[pos2]
dif = val1 - val2
for new_pos in [pos1-1, pos1+1, pos1-W2, pos1+W2]:
h, w = divmod(new_pos, W2)
if w == 0 or h == 0 or w == W1 or h == H1 or new_pos == pos2: continue
if (new_pos, pos1) in visited: continue
new_val = M[new_pos]
if new_val == val1 or new_val == val2: continue
if dif * (new_val - val1) > 0: continue
new_frontiers.append((new_pos, pos1))
visited.add((new_pos, pos1))
if new_pos == goal: return dist
frontiers = new_frontiers
return -1
W, H, M = read_data()
print(solve(W, H, M))