Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
2
Indexable
Never
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using db = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<db, db>;
using tiii = tuple<int, int, int>;
using str = string;

#define vt vector
#define pb push_back
#define eb emplace_back
#define ins insert
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second



void solve() {
    int n, m;
    cin >> n >> m;
    vt<vt<char>> matrix(n);
    int used[n][m];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char a;
            cin >> a;
            matrix[i].pb(a);
        }
    }
    
    int cnt = 0;
    
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (matrix[i][j] == '.') {
                if (!used[i][j]) {
                    cnt++;
                    set<pii> q = {mp(i, j)};
                    while (!q.empty()) {
                        int x = q.begin()->fi, y = q.begin()->se;
                        q.erase(q.begin());
                        vt<pii> ch = {mp(x + 1, y), mp(x - 1, y), mp(x, y + 1), mp(x, y - 1)};
                        for (auto [u, v] : ch) {
                            if (0 <= u <= n && 0 <= v <= m) {
                                used[u][v] = cnt;
                                q.ins(mp(u, v));
                            }
                        }
                    }
                }
            }
        }
    }
    cout << cnt;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
//     freopen("data.in", "r", stdin);
//     freopen("data.out", "w", stdout);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}