結果

問題 No.697 池の数はいくつか
ユーザー mag
提出日時 2020-06-24 07:15:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 806 ms / 6,000 ms
コード長 1,037 bytes
コンパイル時間 1,930 ms
コンパイル使用メモリ 175,644 KB
実行使用メモリ 38,656 KB
最終ジャッジ日時 2024-11-08 08:40:05
合計ジャッジ時間 9,274 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
https://yukicoder.me/problems/no/697

*/
#include<bits/stdc++.h>

using namespace std;
using ll=long long;
#define rep2(i, a, n) for(int i = (a); i < (n); i++)
#define rep(i, n) rep2(i,0,n)

int main(){
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  int h,w;
  cin>>h>>w;
  vector<vector<int>> hoge(h,vector<int>(w));
  rep(i,h)rep(j,w)cin>>hoge[i][j];
  //$B0\F0(B4$BJ}8~$N%Y%/%H%k(B
  int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
  int ans=0,x,y,xx,yy;
  rep(sy,h){
    rep(sx,w){
      if(hoge[sy][sx]==1){
        ans++;
        queue<pair<int,int>> que;
        que.push({sx,sy});
        hoge[sy][sx]=0;
        while(que.size()){
          x=que.front().first;
          y=que.front().second;
          que.pop();
          rep(i,4){
            xx=x+dx[i];
            yy=y+dy[i];
            if (0 <= xx and xx < w and 0 <= yy and yy < h) if (hoge[yy][xx] == 1) {
              que.push({xx,yy});
              hoge[yy][xx] = 0;
            }
          }
        }
      }
    }
  }
  cout<<ans<<endl;
}
0