結果

問題 No.697 池の数はいくつか
ユーザー ryusukeryusuke
提出日時 2022-02-04 21:04:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,987 ms / 6,000 ms
コード長 1,186 bytes
コンパイル時間 868 ms
コンパイル使用メモリ 82,216 KB
実行使用メモリ 281,400 KB
最終ジャッジ日時 2024-04-25 21:08:21
合計ジャッジ時間 15,401 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,228 KB
testcase_01 AC 41 ms
54,588 KB
testcase_02 AC 40 ms
55,832 KB
testcase_03 AC 42 ms
54,776 KB
testcase_04 AC 41 ms
54,260 KB
testcase_05 AC 41 ms
54,780 KB
testcase_06 AC 41 ms
54,064 KB
testcase_07 AC 42 ms
54,672 KB
testcase_08 AC 43 ms
55,000 KB
testcase_09 AC 40 ms
55,260 KB
testcase_10 AC 43 ms
55,028 KB
testcase_11 AC 40 ms
55,176 KB
testcase_12 AC 41 ms
54,472 KB
testcase_13 AC 41 ms
55,316 KB
testcase_14 AC 40 ms
53,936 KB
testcase_15 AC 40 ms
53,944 KB
testcase_16 AC 41 ms
55,556 KB
testcase_17 AC 42 ms
54,116 KB
testcase_18 AC 42 ms
54,932 KB
testcase_19 AC 41 ms
54,856 KB
testcase_20 AC 40 ms
54,484 KB
testcase_21 AC 40 ms
54,480 KB
testcase_22 AC 41 ms
54,052 KB
testcase_23 AC 42 ms
54,548 KB
testcase_24 AC 259 ms
96,052 KB
testcase_25 AC 253 ms
95,972 KB
testcase_26 AC 257 ms
96,032 KB
testcase_27 AC 254 ms
96,204 KB
testcase_28 AC 256 ms
96,244 KB
testcase_29 AC 1,966 ms
281,400 KB
testcase_30 AC 1,601 ms
269,368 KB
testcase_31 AC 1,987 ms
281,296 KB
testcase_32 AC 1,583 ms
269,336 KB
testcase_33 AC 1,591 ms
269,276 KB
testcase_34 AC 1,640 ms
269,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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