結果

問題 No.3504 Insert Maze
コンテスト
ユーザー tyuyu_62
提出日時 2026-04-18 10:40:24
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,756 ms / 2,000 ms
コード長 2,152 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 289 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 418,688 KB
最終ジャッジ日時 2026-04-18 10:41:32
合計ジャッジ時間 54,244 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# snippet: templete_
# Last Modified: 2026-02-13

from collections import deque, defaultdict
from itertools import permutations, product
from bisect import bisect_left, bisect_right
from heapq import heappush, heappop
from random import randint, shuffle
from time import perf_counter as pc
def II(): return int(input())
def LI(): return list(map(int,input().split()))
def LI_1(): return [int(x) - 1 for x in input().split()]
def SI(): return input()
def LS(): return list(input().split())
mod = 998244353
inf = 1001001001001001001

import sys
sys.setrecursionlimit(10 ** 6)
input = lambda: sys.stdin.readline().rstrip()
_buf = []
def print(*a, **k): _buf.append(" ".join(map(str, a)))

D = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def solve():
    H, W = LI()
    S = [SI() for _ in range(H)]
    dp = [[inf] * W for _ in range(H)]
    dp[0][0] = 0
    ptr = 0
    g = [[] for _ in range(2 * H + 2 * W + 1)]
    g[0].append((0, 0))
    while ptr < H + W + 1:
        for i, j in g[ptr]:
            if dp[i][j] < ptr:
                continue
            for di, dj in D:
                ni, nj = i + di, j + dj
                if 0 <= ni < H and 0 <= nj < W:
                    if S[ni][nj] == "#":
                        nc = ptr + 2
                        if dp[ni][nj] > nc:
                            dp[ni][nj] = nc
                            g[nc].append((ni, nj))
                            ni += di; nj += dj
                            while 0 <= ni < H and 0 <= nj < W:
                                nc += 1
                                if dp[ni][nj] > nc:
                                    dp[ni][nj] = nc
                                    g[nc].append((ni, nj))
                                ni += di; nj += dj
                    else:
                        nc = ptr + 1
                        if dp[ni][nj] > nc:
                            dp[ni][nj] = nc
                            g[nc].append((ni, nj))
        ptr += 1
    assert dp[-1][-1] != inf
    print(dp[-1][-1])
    return

if __name__ == "__main__":
    T = 1
    # T = II()
    for _ in range(T):
        solve()
    sys.stdout.write("\n".join(_buf) + "\n")
0