結果

問題 No.13 囲みたい!
ユーザー H3PO4H3PO4
提出日時 2020-04-18 18:28:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 989 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-04-14 21:07:11
合計ジャッジ時間 1,786 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,008 KB
testcase_01 AC 33 ms
11,008 KB
testcase_02 AC 30 ms
10,880 KB
testcase_03 AC 44 ms
12,160 KB
testcase_04 AC 37 ms
12,288 KB
testcase_05 AC 53 ms
12,288 KB
testcase_06 AC 38 ms
12,160 KB
testcase_07 AC 53 ms
12,288 KB
testcase_08 AC 57 ms
12,288 KB
testcase_09 AC 40 ms
12,288 KB
testcase_10 AC 39 ms
11,136 KB
testcase_11 AC 51 ms
12,032 KB
testcase_12 AC 32 ms
10,880 KB
testcase_13 AC 39 ms
11,264 KB
testcase_14 AC 39 ms
11,264 KB
testcase_15 AC 31 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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')
0