結果

問題 No.3504 Insert Maze
コンテスト
ユーザー Koi
提出日時 2026-04-18 00:48:56
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 2,673 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 372 ms
コンパイル使用メモリ 85,632 KB
実行使用メモリ 146,432 KB
最終ジャッジ日時 2026-04-18 00:49:16
合計ジャッジ時間 10,718 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code


def solve1(H, W, S):
    L = [(0,0)]
    Seen = [[False] * W for _ in range(H)]
    add_one_possible = False
    add_zero_possible = False
    dx = [1, 0]
    dy = [0, 1]
    while len(L):
        (x, y) = L.pop()
        if(x == H - 1 and y == W - 1):
            add_zero_possible = True
            break
        if(x >= H - 2 or y >= W - 2):
            add_one_possible = True
        for k in range(2):
            x2 = x + dx[k]
            y2 = y + dy[k]
            if(x2 < H and y2 < W and  S[x2][y2] != "#" and not Seen[x2][y2]):
                Seen[x2][y2] = True
                L.append((x2, y2))
    if(add_zero_possible):
        return (H + W - 2)
    elif(add_one_possible):
        return (H + W - 1)
    else:
        return (H + W)

def solve2(H, W, S):
    L = [(0,0)]
    Seen = [[False] * W for _ in range(H)]
    dx = [1, 0]
    dy = [0, 1]
    S_height = 0
    S_width = 0
    while len(L):
        (x, y) = L.pop()
        if(x == H - 1 and y == W - 1):
            return (H + W - 2)
        S_height = max(x, S_height)
        S_width = max(y, S_width)
        for k in range(2):
            x2 = x + dx[k]
            y2 = y + dy[k]
            if(x2 < H and y2 < W and  S[x2][y2] != "#" and not Seen[x2][y2]):
                Seen[x2][y2] = True
                L.append((x2, y2))
    L = [(H - 1,W - 1)]
    Seen = [[False] * W for _ in range(H)]
    dx = [-1, 0]
    dy = [0, -1]
    G_height = H - 1
    G_width = W - 1
    while len(L):
        (x, y) = L.pop()
        G_height = min(x, G_height)
        G_width = min(y, G_width)
        for k in range(2):
            x2 = x + dx[k]
            y2 = y + dy[k]
            if(0 <= x2  and 0 <= y2 and  S[x2][y2] != "#" and not Seen[x2][y2]):
                Seen[x2][y2] = True
                L.append((x2, y2))
    # print(S_height, S_width, G_height, G_width)
    if(G_height - S_height <= 1 or G_width - S_width <= 1):
        return (H + W -1)
    else:
        return (H + W)


H, W = map(int, input().split())
S = [input() for _ in range(H)]
print(solve2(H, W, S))
# import random
# random.seed(41)
# while True:
#     H = random.randint(2, 3)
#     W = random.randint(2, 3)
#     S = [["." for _ in range(W)] for _ in range(H)]
#     S[0][0] = "S"
#     S[H - 1][W - 1] = "G"
#     for i in range(H):
#         for j in range(W):
#             if(S[i][j] != "."):
#                 continue
#             if(random.random() < 0.6):
#                 S[i][j] = "#"
#     ans1 = solve1(H, W, S)
#     ans2 = solve2(H, W, S)
#     if(ans1 != ans2):
#         print(H, W)
#         for i in range(H):
#             print("".join(S[i]))
#         print(ans1, ans2)
#         break
0