結果

問題 No.697 池の数はいくつか
ユーザー beet
提出日時 2018-11-15 17:28:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,672 ms / 6,000 ms
コード長 1,109 bytes
コンパイル時間 1,909 ms
コンパイル使用メモリ 199,628 KB
最終ジャッジ日時 2025-01-06 16:42:11
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
const int MAX = 3030;
int st[MAX][MAX],dp[MAX][MAX];
signed main(){
  int h,w;
  cin>>h>>w;
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      cin>>st[i][j];
  memset(dp,0,sizeof(dp));
  int ans=0;

  auto in=[&](int y,int x){return 0<=y&&y<h&&0<=x&&x<w;};
  int dy[]={0,0,1,-1};
  int dx[]={1,-1,0,0};
  
  auto bfs=
    [&](int y,int x){
      using P = pair<int, int>;
      queue<P> q;
      q.emplace(y,x);
      dp[y][x]=1;
      while(!q.empty()){
        tie(y,x)=q.front();q.pop();
        for(int k=0;k<4;k++){
          int ny=y+dy[k],nx=x+dx[k];
          if(!in(ny,nx)||!st[ny][nx]) continue;
          if(dp[ny][nx]) continue;
          q.emplace(ny,nx);
          dp[ny][nx]=1;
        }
      }
      ans++;      
    };
  
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      if(st[i][j]&&!dp[i][j]) bfs(i,j);
  cout<<ans<<endl;
  return 0;
}
0