結果

問題 No.3504 Insert Maze
コンテスト
ユーザー YuukunA
提出日時 2026-04-18 18:07:17
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
TLE  
実行時間 -
コード長 925 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 550 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 55,260 KB
最終ジャッジ日時 2026-04-18 18:07:50
合計ジャッジ時間 9,514 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17 TLE * 1 -- * 67
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
import sys

input = sys.stdin.readline
H, W = map(int, input().split())
C = [input().strip() for _ in range(H)]

HH, WW = 2 * H - 1, 2 * W - 1

def ok(x, y):
    return 0 <= x < HH and 0 <= y < WW and not (
        x % 2 == 0 and y % 2 == 0 and C[x // 2][y // 2] == "#"
    )

dist = [[-1] * WW for _ in range(HH)]
dist[0][0] = 0
q = deque([(0, 0)])

while q:
    x, y = q.popleft()

    ns = []
    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        ns.append((x + dx, y + dy))

    if x % 2 == 0 and y % 2 == 0:
        ns += [(x + 2, y), (x - 2, y), (x, y + 2), (x, y - 2)]
    elif x % 2 == 1 and y % 2 == 0:
        ns += [(x, y + 2), (x, y - 2)]
    elif x % 2 == 0 and y % 2 == 1:
        ns += [(x + 2, y), (x - 2, y)]

    for nx, ny in ns:
        if ok(nx, ny) and dist[nx][ny] < 0:
            dist[nx][ny] = dist[x][y] + 1
            q.append((nx, ny))

print(dist[-1][-1])
0