結果

問題 No.697 池の数はいくつか
ユーザー hanyuhanyu
提出日時 2020-04-03 18:44:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 719 ms / 6,000 ms
コード長 1,141 bytes
コンパイル時間 1,768 ms
コンパイル使用メモリ 174,608 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-25 20:45:50
合計ジャッジ時間 8,174 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,948 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 80 ms
6,944 KB
testcase_25 AC 83 ms
6,944 KB
testcase_26 AC 80 ms
6,940 KB
testcase_27 AC 80 ms
6,944 KB
testcase_28 AC 80 ms
6,940 KB
testcase_29 AC 692 ms
6,944 KB
testcase_30 AC 708 ms
6,944 KB
testcase_31 AC 695 ms
6,940 KB
testcase_32 AC 716 ms
6,944 KB
testcase_33 AC 706 ms
6,940 KB
testcase_34 AC 719 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};

int h, w;

void bfs(int i, int j, vector<vector<bool>> &a) {
  queue<pair<int, int>> q;
  q.push(make_pair(i, j));
  a.at(i).at(j) = false;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      if (p.first + dx[k] < 0 || p.first + dx[k] >= h || 
          p.second + dy[k] < 0 || p.second + dy[k] >= w) continue;
      if (a.at(p.first + dx[k]).at(p.second + dy[k])) {
        a.at(p.first + dx[k]).at(p.second + dy[k]) = false;
        q.push(make_pair(p.first + dx[k], p.second + dy[k]));
      }
    }
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  cin >> h >> w;
  
  vector<vector<bool>> a(h, vector<bool>(w));
  int x;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> x;
      a.at(i).at(j) = (x ? true : false);
    }
  }
  
  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a.at(i).at(j)) {
        bfs(i, j, a);
        ans++;
      }
    }
  }
  
  cout << ans << '\n';
}
0