結果

問題 No.697 池の数はいくつか
ユーザー simansiman
提出日時 2021-01-10 20:26:09
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 1,444 ms / 6,000 ms
コード長 1,280 bytes
コンパイル時間 1,602 ms
コンパイル使用メモリ 139,756 KB
実行使用メモリ 47,732 KB
最終ジャッジ日時 2024-04-25 20:57:59
合計ジャッジ時間 12,774 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
12,752 KB
testcase_01 AC 5 ms
13,516 KB
testcase_02 AC 3 ms
13,132 KB
testcase_03 AC 3 ms
13,900 KB
testcase_04 AC 4 ms
12,876 KB
testcase_05 AC 4 ms
13,516 KB
testcase_06 AC 4 ms
12,876 KB
testcase_07 AC 3 ms
13,648 KB
testcase_08 AC 4 ms
13,644 KB
testcase_09 AC 3 ms
12,880 KB
testcase_10 AC 3 ms
13,256 KB
testcase_11 AC 4 ms
13,000 KB
testcase_12 AC 3 ms
13,648 KB
testcase_13 AC 4 ms
13,004 KB
testcase_14 AC 4 ms
13,776 KB
testcase_15 AC 3 ms
14,028 KB
testcase_16 AC 3 ms
13,008 KB
testcase_17 AC 4 ms
13,260 KB
testcase_18 AC 3 ms
13,004 KB
testcase_19 AC 3 ms
13,388 KB
testcase_20 AC 3 ms
12,876 KB
testcase_21 AC 4 ms
13,900 KB
testcase_22 AC 4 ms
12,368 KB
testcase_23 AC 3 ms
13,132 KB
testcase_24 AC 162 ms
26,660 KB
testcase_25 AC 165 ms
24,712 KB
testcase_26 AC 164 ms
26,884 KB
testcase_27 AC 165 ms
24,884 KB
testcase_28 AC 162 ms
25,108 KB
testcase_29 AC 1,358 ms
47,604 KB
testcase_30 AC 1,409 ms
47,476 KB
testcase_31 AC 1,317 ms
47,480 KB
testcase_32 AC 1,435 ms
47,480 KB
testcase_33 AC 1,444 ms
47,732 KB
testcase_34 AC 1,416 ms
47,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_H = 3000;
const int MAX_W = 3000;
const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};

int H, W;
int A[MAX_H][MAX_W];
bool visited[MAX_W][MAX_W];

void f(int y, int x) {
  queue<int> que;
  que.push(y * W + x);
  A[y][x] = 0;

  while (!que.empty()) {
    int z = que.front();
    int y = z / W;
    int x = z % W;
    que.pop();

    for (int i = 0; i < 4; ++i) {
      int ny = y + DY[i];
      int nx = x + DX[i];
      int nz = ny * W + nx;
      if (ny < 0 || nx < 0 || H <= ny || W <= nx) continue;
      if (A[ny][nx] == 0) continue;

      A[ny][nx] = 0;
      que.push(nz);
    }
  }
}

int main() {
  cin >> H >> W;
  fprintf(stderr, "H: %d, W: %d\n", H, W);
  memset(visited, false, sizeof(visited));

  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      cin >> A[y][x];
    }
  }

  int ans = 0;

  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      if (A[y][x] == 0) continue;

      ++ans;
      f(y, x);
    }
  }

  cout << ans << endl;

  return 0;
}
0