結果

問題 No.697 池の数はいくつか
ユーザー ryusukeryusuke
提出日時 2023-04-21 14:27:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,853 ms / 6,000 ms
コード長 1,224 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 281,420 KB
最終ジャッジ日時 2024-04-24 02:56:46
合計ジャッジ時間 15,623 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
54,016 KB
testcase_01 AC 43 ms
53,760 KB
testcase_02 AC 40 ms
53,888 KB
testcase_03 AC 39 ms
53,632 KB
testcase_04 AC 41 ms
53,888 KB
testcase_05 AC 41 ms
53,760 KB
testcase_06 AC 39 ms
54,016 KB
testcase_07 AC 41 ms
53,888 KB
testcase_08 AC 40 ms
53,888 KB
testcase_09 AC 40 ms
53,632 KB
testcase_10 AC 49 ms
53,760 KB
testcase_11 AC 45 ms
53,888 KB
testcase_12 AC 44 ms
53,888 KB
testcase_13 AC 40 ms
53,888 KB
testcase_14 AC 39 ms
54,016 KB
testcase_15 AC 40 ms
53,888 KB
testcase_16 AC 39 ms
53,888 KB
testcase_17 AC 40 ms
53,632 KB
testcase_18 AC 39 ms
53,888 KB
testcase_19 AC 38 ms
53,888 KB
testcase_20 AC 39 ms
54,144 KB
testcase_21 AC 39 ms
53,760 KB
testcase_22 AC 40 ms
53,888 KB
testcase_23 AC 39 ms
53,888 KB
testcase_24 AC 238 ms
96,256 KB
testcase_25 AC 229 ms
96,000 KB
testcase_26 AC 229 ms
96,128 KB
testcase_27 AC 227 ms
96,128 KB
testcase_28 AC 227 ms
96,256 KB
testcase_29 AC 1,853 ms
281,416 KB
testcase_30 AC 1,406 ms
269,312 KB
testcase_31 AC 1,812 ms
281,420 KB
testcase_32 AC 1,444 ms
269,440 KB
testcase_33 AC 1,676 ms
269,184 KB
testcase_34 AC 1,394 ms
269,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.path.append("../../")

from collections import deque

h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]

# dist[i][j] := INF -> 池でまだ見てない, -1 -> 地面で通れない
dist = [[-1] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j]:
            dist[i][j] = 0

cnt = 1 # 今見ている池の番号
q = deque()
for i in range(h):
    for j in range(w):
        if a[i][j]:
            q.append((i, j))
            dist[i][j] = cnt
            break
    break

d = ((1, 0), (-1, 0), (0, 1), (0, -1))
for i in range(h):
    for j in range(w):
        if not (dist[i][j] == 0 or dist[i][j] == cnt): continue
        if dist[i][j] == 0:
            q.append((i, j))
        while q:
            vy, vx = q.popleft()
            dist[vy][vx] = cnt
            for dy, dx in d:
                y = vy + dy
                x = vx + dx
                if not (0 <= x < w and 0 <= y < h): continue
                if dist[y][x] != 0: continue
                dist[y][x] = dist[vy][vx]
                q.append((y, x))
        cnt += 1

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, dist[i][j])

print(ans)
0