結果

問題 No.697 池の数はいくつか
ユーザー qibqib
提出日時 2022-11-04 00:31:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,824 ms / 6,000 ms
コード長 766 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 285,720 KB
最終ジャッジ日時 2023-09-25 07:19:14
合計ジャッジ時間 21,931 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,784 KB
testcase_01 AC 91 ms
71,376 KB
testcase_02 AC 94 ms
71,820 KB
testcase_03 AC 93 ms
71,772 KB
testcase_04 AC 93 ms
71,608 KB
testcase_05 AC 92 ms
71,684 KB
testcase_06 AC 93 ms
71,704 KB
testcase_07 AC 93 ms
71,768 KB
testcase_08 AC 90 ms
71,652 KB
testcase_09 AC 94 ms
71,404 KB
testcase_10 AC 95 ms
71,864 KB
testcase_11 AC 93 ms
71,772 KB
testcase_12 AC 98 ms
71,632 KB
testcase_13 AC 99 ms
71,720 KB
testcase_14 AC 91 ms
71,720 KB
testcase_15 AC 94 ms
71,532 KB
testcase_16 AC 93 ms
71,468 KB
testcase_17 AC 92 ms
71,360 KB
testcase_18 AC 97 ms
71,672 KB
testcase_19 AC 96 ms
71,684 KB
testcase_20 AC 95 ms
71,520 KB
testcase_21 AC 104 ms
71,492 KB
testcase_22 AC 103 ms
71,616 KB
testcase_23 AC 102 ms
71,764 KB
testcase_24 AC 350 ms
95,624 KB
testcase_25 AC 342 ms
95,808 KB
testcase_26 AC 334 ms
96,080 KB
testcase_27 AC 347 ms
95,948 KB
testcase_28 AC 361 ms
96,136 KB
testcase_29 AC 2,824 ms
284,444 KB
testcase_30 AC 1,922 ms
238,708 KB
testcase_31 AC 2,735 ms
285,720 KB
testcase_32 AC 1,743 ms
238,704 KB
testcase_33 AC 1,755 ms
238,712 KB
testcase_34 AC 1,802 ms
238,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

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

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

par = [[-1 for _ in range(w)] for _ in range(h)]
ans = 0
for x in range(h):
  for y in range(w):
    if a[x][y] == 0 or par[x][y] != -1:
      continue

    dq = deque()
    sx, sy = x, y
    dq.appendleft((sx, sy))
    par[sx][sy] = ans
    while len(dq) > 0:
      cx, cy = dq.pop()
      for i in range(4):
        nx = cx + dx[i]
        if nx < 0 or nx >= h:
          continue
        ny = cy + dy[i]
        if ny < 0 or ny >= w:
          continue
        if a[nx][ny] == 0 or par[nx][ny] != -1:
          continue

        par[nx][ny] = ans
        dq.appendleft((nx, ny))
    ans += 1

print(ans)
0