Submission #531343

#TimeUsernameProblemLanguageResultExecution timeMemory
531343zxcvbnmDango Maker (JOI18_dango_maker)C++17
33 / 100
2033 ms165864 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr int nax = 3005;
char mat[nax][nax];
int thrus[nax][nax][2];
const string str = "RGW";
stack<array<int, 3>> s[4];
int n, m;

bool check_ok(int i, int j, int p) {
    if (i < 0 || j < 0 || i >= n || j >= m) return false;

    string curr;
    if (p == 0) {
        for(int k = 0; k < 3; k++) {
            curr += mat[i][j+k];
        }
    }
    else {
        for(int k = 0; k < 3; k++) {
            curr += mat[i+k][j];
        }
    }
    return (curr == str);
}

int thru(int i, int j, int p) {
    int cnt = 0;
    if (p == 0) {
        for(int k = 0; k < 3; k++) {
            cnt += check_ok(i-k, j+k, 1);
        }
    }
    else {
        for(int k = 0; k < 3; k++) {
            cnt += check_ok(i+k, j-k, 0);
        }
    }
    return cnt;
}

void rem(int i, int j, int p) {
    if (p == 0) {
        for(int k = 0; k < 3; k++) {
            mat[i][j+k] = 'X';
        }
    }
    else {
        for(int k = 0; k < 3; k++) {
            mat[i+k][j] = 'X';
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> mat[i][j];
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if (check_ok(i, j, 0)) {
                int cnt = thru(i, j, 0);
                thrus[i][j][0] = cnt;
//                cout << i << " " << j << " 0 " << cnt << "\n";
                s[cnt].push({i, j, 0});
            }
            if (check_ok(i, j, 1)) {
                int cnt = thru(i, j, 1);
//                cout << i << " " << j << " 1 " << cnt << "\n";
                thrus[i][j][1] = cnt;
                s[cnt].push({i, j, 1});
            }
        }
    }

    int ans = 0;

    for(int rep = 0; rep < 4; rep++) {
        while(!s[rep].empty()) {
            auto v = s[rep].top();
            s[rep].pop();

            int x = v[0], y = v[1], p = v[2];
            if (!check_ok(x, y, p)) continue;
            ans++;
            rem(x, y, p);

            for(int i = max(0, x-3); i < min(n, x+3); i++) {
                for(int j = max(0, y-3); j < min(m, y+3); j++) {
                    if (check_ok(i, j, 0)) {
                        int cnt = thru(i, j, 0);
                        if (cnt < thrus[i][j][0]) {
                            thrus[i][j][0] = cnt;
                            if (cnt < rep) {
                                rep = cnt;
                            }
                            s[cnt].push({i, j, 0});
                        }
                    }
                    if (check_ok(i, j, 1)) {
                        int cnt = thru(i, j, 1);
                        if (cnt < thrus[i][j][1]) {
                            thrus[i][j][1] = cnt;
                            if (cnt < rep) {
                                rep = cnt;
                            }
                            s[cnt].push({i, j, 1});
                        }
                    }
                }
            }
        }
    }

    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...