結果

問題 No.697 池の数はいくつか
ユーザー neko_the_shadowneko_the_shadow
提出日時 2018-06-09 23:44:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 779 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 10,804 KB
実行使用メモリ 93,196 KB
最終ジャッジ日時 2023-08-16 22:16:49
合計ジャッジ時間 21,929 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
93,196 KB
testcase_01 AC 15 ms
8,252 KB
testcase_02 AC 15 ms
8,308 KB
testcase_03 AC 15 ms
8,172 KB
testcase_04 WA -
testcase_05 AC 15 ms
8,252 KB
testcase_06 AC 15 ms
8,168 KB
testcase_07 AC 15 ms
8,208 KB
testcase_08 AC 15 ms
8,200 KB
testcase_09 AC 15 ms
8,152 KB
testcase_10 WA -
testcase_11 AC 16 ms
8,184 KB
testcase_12 AC 16 ms
8,304 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 15 ms
8,320 KB
testcase_16 AC 15 ms
8,260 KB
testcase_17 AC 15 ms
8,196 KB
testcase_18 AC 15 ms
8,288 KB
testcase_19 AC 15 ms
8,248 KB
testcase_20 AC 16 ms
8,252 KB
testcase_21 AC 15 ms
8,252 KB
testcase_22 AC 15 ms
8,308 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools

if __name__ == '__main__':
    h, w = map(int, input().split())
    matrix = [list(map(int, input().split())) for _ in range(h)]
    
    diffs = ((1, 0), (-1, 0), (0, 1), (0, -1))
    counter = 2
    for x, y in itertools.product(range(h), range(w)):
        if matrix[x][y] == 0: continue

        ponds = [(x, y)]
        for dx, dy in diffs:
            i, j = x + dx, y + dy
            if 0 <= i < h and 0 <= j < w and matrix[i][j] != 0: ponds.append((i, j))
        
        coordinate = min((matrix[i][j] for i, j in ponds if matrix[i][j] != 1), default=counter)
        if coordinate == counter: counter += 1

        for i, j in ponds: matrix[i][j] = coordinate
    
    answer = max(map(max, matrix))
    if answer != 0: answer -= 1
    print(answer)
0