結果

問題 No.697 池の数はいくつか
ユーザー playrollerplayroller
提出日時 2018-11-12 23:40:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 774 ms / 6,000 ms
コード長 1,227 bytes
コンパイル時間 1,630 ms
コンパイル使用メモリ 173,032 KB
実行使用メモリ 38,732 KB
最終ジャッジ日時 2023-08-08 02:21:54
合計ジャッジ時間 8,815 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 85 ms
6,928 KB
testcase_25 AC 85 ms
6,988 KB
testcase_26 AC 85 ms
7,060 KB
testcase_27 AC 85 ms
6,992 KB
testcase_28 AC 85 ms
6,988 KB
testcase_29 AC 727 ms
38,568 KB
testcase_30 AC 768 ms
38,336 KB
testcase_31 AC 725 ms
38,732 KB
testcase_32 AC 772 ms
38,320 KB
testcase_33 AC 774 ms
38,456 KB
testcase_34 AC 768 ms
38,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, j, n) for(int i=j;i<n;++i)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(),i.rend()
#define INF 1e9
const int mod = 1e9 + 7;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));}
i64 lcm(i64 n, i64 m) {return (n / gcd(n, m) * m);}
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  vvt<int> a(h, vt<int>(w));
  rep(i, 0, h) rep(j, 0, w) cin >> a[i][j];

  int ans = 0;
  rep(sy, 0, h) {
    rep(sx, 0, w) {
      if(!a[sy][sx]) continue;

      ans++;
      queue<pi> que;
      que.push({sy, sx});
      a[sy][sx] = 0;

      while(!que.empty()) {
        int y = que.front().first, x = que.front().second;
        que.pop();

        rep(i, 0, 4) {
          int ny = y + dy[i], nx = x + dx[i];
          if(nx < 0 || nx >= w || ny < 0 || ny >= h) continue;

          if(a[ny][nx]) {
            que.push({ny, nx});
            a[ny][nx] = 0;
          }
        }
      }
    }
  }
  cout << ans << endl;
}
0