結果

問題 No.2412 YOU Grow Bigger!
ユーザー hari64hari64
提出日時 2023-07-19 22:42:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,225 ms / 6,000 ms
コード長 2,393 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 82,428 KB
実行使用メモリ 265,856 KB
最終ジャッジ日時 2024-05-01 06:34:57
合計ジャッジ時間 35,354 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
69,032 KB
testcase_01 AC 43 ms
56,268 KB
testcase_02 AC 57 ms
65,952 KB
testcase_03 AC 4,225 ms
265,552 KB
testcase_04 AC 3,893 ms
265,856 KB
testcase_05 AC 2,597 ms
185,656 KB
testcase_06 AC 3,056 ms
219,752 KB
testcase_07 AC 3,933 ms
265,020 KB
testcase_08 AC 2,979 ms
216,504 KB
testcase_09 AC 93 ms
77,472 KB
testcase_10 AC 1,139 ms
100,172 KB
testcase_11 AC 66 ms
68,492 KB
testcase_12 AC 50 ms
60,636 KB
testcase_13 AC 1,477 ms
120,004 KB
testcase_14 AC 1,005 ms
98,072 KB
testcase_15 AC 82 ms
76,676 KB
testcase_16 AC 88 ms
77,132 KB
testcase_17 AC 90 ms
77,028 KB
testcase_18 AC 1,119 ms
101,792 KB
testcase_19 AC 79 ms
74,244 KB
testcase_20 AC 69 ms
71,476 KB
testcase_21 AC 522 ms
82,688 KB
testcase_22 AC 1,414 ms
106,928 KB
testcase_23 AC 795 ms
92,032 KB
testcase_24 AC 1,611 ms
127,480 KB
testcase_25 AC 707 ms
88,552 KB
testcase_26 AC 803 ms
91,392 KB
testcase_27 AC 681 ms
84,456 KB
testcase_28 AC 1,172 ms
103,340 KB
testcase_29 AC 87 ms
76,916 KB
testcase_30 AC 57 ms
65,396 KB
権限があれば一括ダウンロードができます

ソースコード

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