結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,392 KB
testcase_01 AC 40 ms
54,848 KB
testcase_02 AC 40 ms
55,196 KB
testcase_03 AC 39 ms
54,684 KB
testcase_04 AC 40 ms
54,736 KB
testcase_05 AC 38 ms
54,584 KB
testcase_06 AC 40 ms
54,396 KB
testcase_07 AC 41 ms
54,184 KB
testcase_08 AC 39 ms
55,108 KB
testcase_09 AC 40 ms
54,608 KB
testcase_10 AC 39 ms
54,788 KB
testcase_11 AC 39 ms
54,644 KB
testcase_12 AC 41 ms
54,312 KB
testcase_13 AC 40 ms
55,904 KB
testcase_14 AC 40 ms
55,228 KB
testcase_15 AC 40 ms
55,384 KB
testcase_16 AC 39 ms
54,624 KB
testcase_17 AC 38 ms
55,608 KB
testcase_18 AC 39 ms
55,532 KB
testcase_19 AC 38 ms
54,852 KB
testcase_20 AC 40 ms
54,760 KB
testcase_21 AC 39 ms
55,168 KB
testcase_22 AC 40 ms
54,224 KB
testcase_23 AC 40 ms
54,056 KB
testcase_24 AC 245 ms
96,084 KB
testcase_25 AC 239 ms
95,904 KB
testcase_26 AC 238 ms
96,024 KB
testcase_27 AC 232 ms
95,956 KB
testcase_28 AC 236 ms
96,024 KB
testcase_29 AC 1,834 ms
281,676 KB
testcase_30 AC 1,449 ms
269,404 KB
testcase_31 AC 1,818 ms
281,664 KB
testcase_32 AC 1,450 ms
269,400 KB
testcase_33 AC 1,497 ms
269,264 KB
testcase_34 AC 1,497 ms
269,556 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
#print(*dist, sep='\n')
#print(q)
#print()
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))
                #print(q)
        cnt += 1

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

#print(*dist, sep='\n')
print(ans)
0