結果

問題 No.13 囲みたい!
ユーザー OKCH3COOHOKCH3COOH
提出日時 2019-10-24 23:53:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 870 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 10,756 KB
実行使用メモリ 11,284 KB
最終ジャッジ日時 2023-09-25 10:31:59
合計ジャッジ時間 1,614 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,480 KB
testcase_01 AC 19 ms
8,476 KB
testcase_02 AC 20 ms
8,568 KB
testcase_03 AC 34 ms
9,328 KB
testcase_04 AC 51 ms
11,284 KB
testcase_05 AC 45 ms
9,208 KB
testcase_06 AC 23 ms
8,684 KB
testcase_07 AC 42 ms
8,660 KB
testcase_08 AC 42 ms
9,032 KB
testcase_09 AC 43 ms
8,976 KB
testcase_10 AC 24 ms
8,500 KB
testcase_11 AC 40 ms
8,752 KB
testcase_12 AC 22 ms
8,660 KB
testcase_13 AC 27 ms
8,684 KB
testcase_14 AC 27 ms
8,696 KB
testcase_15 AC 20 ms
8,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

W, H = map(int, input().split())
M = [list(map(int, input().split())) for _ in range(H)]

depth = [[-1] * W for _ in range(H)]

for stH in range(H):
    for stW in range(W):
        if depth[stH][stW] >= 0:
            continue

        now = M[stH][stW]
        st = deque([(stH, stW, 0)])
        while st:
            nh, nw, d = st.pop()

            if depth[nh][nw] >= 0:
                if abs(depth[nh][nw] - d) >= 3:
                    print('possible')
                    exit()
                continue
            depth[nh][nw] = d

            for h in [nh - 1, nh + 1]:
                if 0 <= h < H and M[h][nw] == now:
                    st.append((h, nw, d + 1))
            for w in [nw - 1, nw + 1]:
                if 0 <= w < W and M[nh][w] == now:
                    st.append((nh, w, d + 1))

print('impossible')
0