結果
| 問題 | No.13 囲みたい! | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-04-18 18:28:12 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 48 ms / 5,000 ms | 
| コード長 | 989 bytes | 
| コンパイル時間 | 151 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 12,288 KB | 
| 最終ジャッジ日時 | 2024-10-04 00:07:49 | 
| 合計ジャッジ時間 | 1,463 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 16 | 
ソースコード
from collections import deque
from sys import exit
W, H = map(int, input().split())
M = [tuple(map(int, input().split())) for _ in range(H)]
table = [[None] * W for _ in range(H)]
nvisited = {(h, w) for h in range(H) for w in range(W)}
def dfs(h0, w0):
    D = deque()
    n = M[h0][w0]
    table[h0][w0] = n
    D.append((h0, w0, 0, 0))
    while D:
        h, w, dhb, dwb = D.pop()
        for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            if dhb == -dh and dwb == -dw:
                continue
            if 0 <= h + dh < H and 0 <= w + dw < W:
                if table[h + dh][w + dw] is None and M[h + dh][w + dw] == n:
                    table[h + dh][w + dw] = n
                    nvisited.remove((h + dh, w + dw))
                    D.append((h + dh, w + dw, dh, dw))
                elif table[h + dh][w + dw] == n:
                    print('possible')
                    exit()
while nvisited:
    h, w = nvisited.pop()
    dfs(h, w)
print('impossible')
            
            
            
        