結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2018-09-08 01:23:04 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,946 ms / 6,000 ms | 
| コード長 | 1,049 bytes | 
| コンパイル時間 | 1,816 ms | 
| コンパイル使用メモリ | 172,088 KB | 
| 実行使用メモリ | 111,992 KB | 
| 最終ジャッジ日時 | 2024-11-08 08:04:46 | 
| 合計ジャッジ時間 | 15,343 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 32 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long ll;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
    int h,w;
    cin >> h >> w;
    int arr[h][w];
    int ans = 0;
    rep(i,0,h){
        rep(j,0,w){
            cin >> arr[i][j];
        }
    }
    rep(i,0,h){
        rep(j,0,w){
        
            if(arr[i][j] == 1){
                stack<pair<int,int>> stk;
                stk.push(make_pair(i,j));
                
                while(!stk.empty()){
                    pair<int,int> tmp = stk.top();
                    arr[tmp.first][tmp.second] = 0;
                    stk.pop();
                    rep(k,0,4){
                        int y = tmp.first + dy[k];
                        int x = tmp.second + dx[k];
                        if(-1 < y && y < h && -1 < x && x < w && arr[y][x] == 1) stk.push(make_pair(y,x));
                    }
                }
                
                ans++;
            }
        }
    }
    cout << ans << endl;
}