結果

問題 No.697 池の数はいくつか
ユーザー parsee1053
提出日時 2021-01-01 18:06:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,684 ms / 6,000 ms
コード長 1,302 bytes
コンパイル時間 1,340 ms
コンパイル使用メモリ 92,060 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-11-08 08:49:55
合計ジャッジ時間 14,258 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int> > a(h, vector<int>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> a[i][j];
    }
  }
  queue<pair<int, int> > que;
  int ans = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (a[i][j] == 1) {
        ans++;
        que.push(make_pair(i, j));
        a[i][j] = 0;
        while (!que.empty()) {
          int y = que.front().first;
          int x = que.front().second;
          que.pop();
          for (int c = -1; c <= 1; ++c) {
            for (int d = -1; d <= 1; ++d) {
              if (c * c + d * d == 1) {
                int ny = y + c, nx = x + d;
                if (0 <= ny && ny < h && 0 <= nx && nx < w) {
                  if (a[ny][nx] == 1) {
                    que.push(make_pair(ny, nx));
                    a[ny][nx] = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
0