Submission #3989986


Source Code Expand

Copy
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <utility>

#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; i++)
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; }

typedef long long ll;
using namespace std;

bool is_in(int i, int j, int h, int w) {
    return 0 <= i && i < h && 0 <= j && j < w;
}

int main() {
    
    int h, w;
    cin >> h >> w;
    vector<string> sorg(h, "");
    vector<string> s   (h, "");
    REP(i, h) {
        cin >> s[i];
        sorg[i] = s[i];
    }

    REP(i, h) REP (j, w) {
        if ((i + j) % 2) {
            s[i][j] = s[i][j] == '.' ? '#' : '.';
        }
    }

    vector<vector<bool>> visited(h, vector<bool>(w, false));
    ll ans = 0;

    REP(i, h) REP(j, w) {

        if (visited[i][j]) continue;

        deque<ll> q;
        ll black = 0, white = 0;

        q.push_back(j + i * w);
        visited[i][j] = true;
        char rootc = s[i][j];

        while (!q.empty()) {

            ll v = q.back();
            int k = v / w, l = v % w;
            q.pop_back();

            if (sorg[k][l] == '.') white++; else black++;
            
            if (is_in(k-1, l, h, w)) if (rootc == s[k-1][l] && !visited[k-1][l]) {
                q.push_back((k - 1) * w + l);
                visited[k-1][l] = true;
            }
            if (is_in(k+1, l, h, w)) if (rootc == s[k+1][l] && !visited[k+1][l]) {
                q.push_back((k + 1) * w + l);
                visited[k+1][l] = true;
            }
            if (is_in(k, l-1, h, w)) if (rootc == s[k][l-1] && !visited[k][l-1]) {
                q.push_back(k * w + l - 1);
                visited[k][l-1] = true;
            }
            if (is_in(k, l+1, h, w)) if (rootc == s[k][l+1] && !visited[k][l+1]) {
                q.push_back(k * w + l + 1);
                visited[k][l+1] = true;
            }

        }

        ans += black * white;

    }

    cout << ans << endl;

}

Submission Info

Submission Time
Task C - Alternating Path
User ganmodokix
Language C++14 (GCC 5.4.1)
Score 300
Code Size 2282 Byte
Status AC
Exec Time 28 ms
Memory 1280 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 15
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01.txt AC 8 ms 384 KB
02.txt AC 22 ms 640 KB
03.txt AC 3 ms 256 KB
04.txt AC 28 ms 640 KB
05.txt AC 13 ms 1280 KB
06.txt AC 16 ms 1024 KB
07.txt AC 15 ms 896 KB
08.txt AC 11 ms 640 KB
09.txt AC 13 ms 640 KB
10.txt AC 1 ms 256 KB
11.txt AC 1 ms 256 KB
12.txt AC 1 ms 256 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB
sample-03.txt AC 1 ms 256 KB