結果

問題 No.2412 YOU Grow Bigger!
ユーザー hari64hari64
提出日時 2023-07-19 22:41:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,393 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 12,100 KB
実行使用メモリ 16,904 KB
最終ジャッジ日時 2023-10-20 02:18:22
合計ジャッジ時間 7,762 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,344 KB
testcase_01 AC 29 ms
10,344 KB
testcase_02 AC 31 ms
10,344 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def canReach(H, W, Ss):
    grid = [[False for _ in range(W - 2)] for _ in range(H - 2)]
    for h in range(1, H - 1):
        for w in range(1, W - 1):
            grid[h - 1][w - 1] = True
            for dh in range(-1, 2):
                for dw in range(-1, 2):
                    if Ss[h + dh][w + dw] == "#":
                        grid[h - 1][w - 1] = False

    # 0,0からH-3,W-3までの経路があるか
    adj = [[[] for _ in range(W - 2)] for _ in range(H - 2)]
    for h in range(H - 2):
        for w in range(W - 2):
            if not grid[h][w]:
                continue
            for dh in range(-1, 2):
                for dw in range(-1, 2):
                    if dh == 0 and dw == 0:
                        continue
                    if (
                        0 <= h + dh < H - 2
                        and 0 <= w + dw < W - 2
                        and grid[h + dh][w + dw]
                    ):
                        adj[h][w].append((h + dh, w + dw))

    q = deque([(0, 0)])
    visited = [[False for _ in range(W - 2)] for _ in range(H - 2)]
    visited[0][0] = True
    while q:
        h, w = q.popleft()
        for nh, nw in adj[h][w]:
            if not visited[nh][nw]:
                visited[nh][nw] = True
                q.append((nh, nw))

    return visited[H - 3][W - 3]


def main():
    H, W = map(int, input().split())
    assert 6 <= H <= 500
    assert 6 <= W <= 500
    Ss = []
    for _ in range(H):
        S = input().strip()
        assert len(S) == W
        for c in S:
            assert c == "." or c == "#"
        Ss.append(S)

    for i in range(3):
        for j in range(3):
            assert Ss[i][j] == "."
    for i in range(H - 3, H):
        for j in range(W - 3, W):
            assert Ss[i][j] == "."

    ans = 2
    if not canReach(H, W, Ss):
        ans = 0
    else:
        for h in range(H):
            for w in range(W):
                if h < 3 and w < 3:
                    continue
                if h >= H - 3 and w >= W - 3:
                    continue
                if Ss[h][w] == "#":
                    continue
                Ss[h] = Ss[h][:w] + "#" + Ss[h][w + 1 :]
                if not canReach(H, W, Ss):
                    ans = 1
                Ss[h] = Ss[h][:w] + "." + Ss[h][w + 1 :]

    print(ans)


if __name__ == "__main__":
    main()
0