結果

問題 No.697 池の数はいくつか
ユーザー fumiphys
提出日時 2019-05-15 23:57:03
言語 C++14
(gcc 8.2.0)
結果
AC  
実行時間 753 ms
コード長 3,937 Byte
コンパイル時間 1,304 ms
使用メモリ 36,716 KB
最終ジャッジ日時 2019-08-12 06:35:31

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
1_sample1.txt AC 2 ms
6,868 KB
1_sample2.txt AC 3 ms
6,872 KB
1_sample3.txt AC 2 ms
6,868 KB
2_case1.txt AC 3 ms
6,872 KB
5_gen_case1.txt AC 2 ms
6,872 KB
5_gen_case2.txt AC 3 ms
8,916 KB
5_gen_case3.txt AC 2 ms
6,868 KB
5_gen_case4.txt AC 2 ms
6,872 KB
5_gen_case5.txt AC 3 ms
6,872 KB
5_gen_case6.txt AC 2 ms
6,872 KB
5_gen_case7.txt AC 2 ms
6,872 KB
5_gen_case8.txt AC 3 ms
6,872 KB
5_gen_case9.txt AC 2 ms
6,868 KB
5_gen_case10.txt AC 2 ms
6,872 KB
5_gen_case11.txt AC 3 ms
6,868 KB
5_gen_case12.txt AC 3 ms
6,872 KB
5_gen_case13.txt AC 2 ms
8,920 KB
5_gen_case14.txt AC 3 ms
6,872 KB
5_gen_case15.txt AC 2 ms
6,872 KB
5_gen_case16.txt AC 2 ms
6,868 KB
5_gen_case17.txt AC 3 ms
6,872 KB
5_gen_case18.txt AC 2 ms
6,872 KB
5_gen_case19.txt AC 3 ms
6,872 KB
5_gen_case20.txt AC 2 ms
6,868 KB
9_gen_case1.txt AC 87 ms
13,140 KB
9_gen_case2.txt AC 87 ms
13,144 KB
9_gen_case3.txt AC 87 ms
13,140 KB
9_gen_case4.txt AC 86 ms
13,140 KB
9_gen_case5.txt AC 89 ms
13,144 KB
10_gen_case.txt AC 700 ms
36,716 KB
11_gen_case.txt AC 753 ms
36,696 KB
テストケース一括ダウンロード

ソースコード

diff #
// includes
#include <bits/stdc++.h>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define vrep(v, i) for(int i = 0; i < (v).size(); i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define FI first
#define SE second
#define dump(a, n) for(int i = 0; i < n; i++)cout << a[i] << "\n "[i + 1 != n];
#define dump2(a, n, m) for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)cout << a[i][j] << "\n "[j + 1 != m];
#define bit(n) (1LL<<(n))
using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
typedef complex<double> cd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << p.first << " " << p.second; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << ":" << itr->second; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << ":" << itr->second; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}

int a[3001][3001];
int h, w;


int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w;
  rep(i, h){
    rep(j, w){
      cin >> a[i][j];
    }
  }
  int res = 0;
  rep(i, h){
    rep(j, w){
      if(a[i][j]){
        res++;
        queue<P> q;
        q.push(mk(i, j));
        while(!q.empty()){
          auto p = q.front(); q.pop();
          int i1 = p.FI;
          int j1 = p.SE;
          a[i1][j1] = 0;
          rep(k, 4){
            int nx = i1 + dx[k];
            int ny = j1 + dy[k];
            if(nx >= 0 && nx < h && ny >= 0 && ny < w && a[nx][ny]){
              a[nx][ny] = 0;
              q.push(mk(nx, ny));
            }
          }
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}

0