結果
| 問題 | No.124 門松列(3) |
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-31 17:58:10 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 135 ms / 5,000 ms |
| コード長 | 1,963 bytes |
| コンパイル時間 | 166 ms |
| コンパイル使用メモリ | 82,568 KB |
| 実行使用メモリ | 92,620 KB |
| 最終ジャッジ日時 | 2025-03-31 17:58:47 |
| 合計ジャッジ時間 | 3,081 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
from collections import deque
def is_kadomatsu(a, b, c):
if a == b or b == c or a == c:
return False
sorted_vals = sorted([a, b, c], reverse=True)
mid = sorted_vals[1]
return mid == a or mid == c
def main():
import sys
input = sys.stdin.read().split()
ptr = 0
W = int(input[ptr])
ptr += 1
H = int(input[ptr])
ptr += 1
grid = []
for y in range(H):
row = list(map(int, input[ptr:ptr+W]))
ptr += W
grid.append(row)
start_x, start_y = 0, 0
goal_x, goal_y = W-1, H-1
if start_x == goal_x and start_y == goal_y:
print(0)
return
visited = [[[[False for _ in range(10)] for __ in range(10)] for ___ in range(W)] for ____ in range(H)]
directions = [ (0, 1), (1, 0), (0, -1), (-1, 0) ]
queue = deque()
initial_val = grid[start_y][start_x]
for dx, dy in directions:
new_x = start_x + dx
new_y = start_y + dy
if 0 <= new_x < W and 0 <= new_y < H:
next_val = grid[new_y][new_x]
if not visited[new_y][new_x][initial_val][next_val]:
visited[new_y][new_x][initial_val][next_val] = True
queue.append( (new_x, new_y, initial_val, next_val, 1) )
found = -1
while queue:
x, y, pv, cv, steps = queue.popleft()
if x == goal_x and y == goal_y:
found = steps
break
for dx, dy in directions:
new_x = x + dx
new_y = y + dy
if 0 <= new_x < W and 0 <= new_y < H:
new_val = grid[new_y][new_x]
if not is_kadomatsu(pv, cv, new_val):
continue
if not visited[new_y][new_x][cv][new_val]:
visited[new_y][new_x][cv][new_val] = True
queue.append( (new_x, new_y, cv, new_val, steps + 1) )
print(found if found != -1 else -1)
if __name__ == '__main__':
main()
lam6er