結果

問題 No.3504 Insert Maze
コンテスト
ユーザー カプモカ
提出日時 2026-04-18 19:08:13
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 1,551 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 323 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 78,056 KB
最終ジャッジ日時 2026-04-18 19:09:34
合計ジャッジ時間 16,480 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other AC * 29 WA * 9 TLE * 2 -- * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
from collections import deque

readline = sys.stdin.readline
input_value = readline().split()
H = int(input_value[0])
W = int(input_value[1])

grid = [None] * H
for h in range(H):
    fragment_grid = readline().rstrip()
    grid[h] = fragment_grid

right_clear = [False] * H
bottom_clear = [False] * W

if grid[H-1][W-1] == '.':
    right_clear[H-1] = True
    for i in range(H-2, -1, -1):
        if grid[i][W-1] == '.':
            right_clear[i] = True
        else:
            break
            
    bottom_clear[W-1] = True
    for i in range(W-2, -1, -1):
        if grid[H-1][i] == '.':
            bottom_clear[i] = True
        else:
            break

dist = [[-1] * W for _ in range(H)]
queue = deque()
dist[0][0] = 0
queue.append((0,0))

dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
Magic = H + W
ans = -1
check_point = False

while queue:
    y, x = queue.popleft()
    if y == H - 1 and x == W - 1:
        ans = dist[y][x]
        break
    
    if dist[y][x] == y + x:
        if y + 1 < H and right_clear[y + 1]:
            check_point = True
        if x + 1 < W and bottom_clear[x + 1]:
            check_point = True
    
    for i in range(4):
        ny = y + dy[i]
        nx = x + dx[i]

        if 0 <= ny < H and 0 <= nx < W:
            if grid[ny][nx] == '.' and dist[ny][nx] == -1:
                dist[ny][nx] = dist[y][x] + 1
                queue.append((ny, nx))

if check_point:
    if ans == -1 or ans > Magic - 1:
        ans = Magic - 1
else:
    if ans == -1 or ans > Magic:
        ans = Magic

print(ans) 
0