結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-10-24 23:53:25 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 62 ms / 5,000 ms | 
| コード長 | 870 bytes | 
| コンパイル時間 | 81 ms | 
| コンパイル使用メモリ | 12,928 KB | 
| 実行使用メモリ | 13,696 KB | 
| 最終ジャッジ日時 | 2024-07-18 08:33:17 | 
| 合計ジャッジ時間 | 1,621 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
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')
            
            
            
        