Submission #529747

#TimeUsernameProblemLanguageResultExecution timeMemory
529747zxcvbnmDango Maker (JOI18_dango_maker)C++14
13 / 100
1 ms332 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const string str = "RGW";
constexpr int nax = 3005;
int thru[nax][nax][2];
char mat[nax][nax];

priority_queue<array<int, 4>, vector<array<int, 4>>, greater<array<int, 4>>> q;

int thrus(int x, int y, int p) {
    return thru[x][y][p];
}

void push_in(int x, int y, int p) {
    q.push({thrus(x, y, p), x, y, p});
}

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

bool check_ok(int i, int j, int p) {
    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);
}

void go(int i, int j, int p, bool rec, bool rem=false) {
    if (!rem)
        if (!check_ok(i, j, p)) return;

//    cout << "go: " << i << " " << j << " " << p << "\n";

    if (p == 0) {
        int cnt = 0;
        if (check_ok(i, j, 1^rem)) {
            cnt++;
            if (rec) {
                go(i, j, 1^rem, false);
            }
        }
        if (i-1 >= 0 && check_ok(i-1, j+1, 1^rem)) {
            cnt++;
            if (rec) {
                go(i-1, j+1, 1^rem, false);
            }
        }
        if (i-2 >= 0 && check_ok(i-2, j+2, 1^rem)) {
            cnt++;
            if (rec) {
                go(i-2, j+2, 1^rem, false);
            }
        }

        if (!rem) {
            thru[i][j][0] = cnt;
            q.push({cnt, i, j, 0});
        }
//                cout << i << " " << j << " " << 0 << " " << cnt << "\n";
    }
    if (p == 1) {
        int cnt = 0;
        if (check_ok(i, j, 0^rem)) {
            cnt++;
            if (rec) {
                go(i, j, 0^rem, false);
            }
        }
        if (j-1 >= 0 && check_ok(i+1, j-1, 0^rem)) {
            cnt++;
            if (rec) {
                go(i+1, j-1, 0^rem, false);
            }
        }
        if (j-2 >= 0 && check_ok(i+2, j-2, 0^rem)) {
            cnt++;
            if (rec) {
                go(i+2, j-2, 0^rem, false);
            }
        }

        if (!rem) {
            thru[i][j][1] = cnt;
            q.push({cnt, i, j, 1});
        }
//                cout << i << " " << j << " " << 1 << " " << cnt << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    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++) {
            go(i, j, 0, false);
            go(i, j, 1, false);
        }
    }

    int ans = 0;

    while(!q.empty()) {
        auto v = q.top();
        q.pop();

        if (!check_ok(v[1], v[2], v[3])) continue;
        ans++;

        rem(v[1], v[2], v[3]);
        go(v[1], v[2], v[3], true, true);
//        cout << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << "\n";
    }

    cout << ans << "\n";
//    for(int i = 0; i < n; i++) {
//        for(int j = 0; j < m-2; j++) {
//            string curr;
//            for(int k = 0; k < 3; k++) {
//                curr += mat[i][j+k];
//            }
//
//            if (curr == str) {
//                ok[i][j][0] = true;
//            }
//        }
//    }
//
//    for(int j = 0; j < m; j++) {
//        for(int i = 0; i < n-2; i++) {
//            string curr;
//            for(int k = 0; k < 3; k++) {
//                curr += mat[i+k][j];
//            }
//
//            if (curr == str) {
//                ok[i][j][1] = true;
//            }
//        }
//    }
//
//    for(int i = 0; i < n; i++) {
//        for(int j = 0; j < m; j++) {
//            if (ok[i][j][0]) {
//                int cnt = 0;
//                cnt += ok[i][j][1];
//                if (i-1 >= 0) {
//                    cnt += ok[i-1][j+1][1];
//                }
//                if (i-2 >= 0) {
//                    cnt += ok[i-2][j+2][1];
//                }
//
//                thru[i][j][0] = cnt;
//                q.push({cnt, i, j, 0});
////                cout << i << " " << j << " " << 0 << " " << cnt << "\n";
//            }
//            if (ok[i][j][1]) {
//                int cnt = 0;
//                cnt += ok[i][j][0];
//                if (j-1 >= 0) {
//                    cnt += ok[i+1][j-1][0];
//                }
//                if (j-2 >= 0) {
//                    cnt += ok[i+2][j-2][0];
//                }
//
//                thru[i][j][1] = cnt;
//                q.push({cnt, i, j, 1});
////                cout << i << " " << j << " " << 1 << " " << cnt << "\n";
//            }
//        }
//    }
//
//    int ans = 0;
//    while(!q.empty()) {
//        auto v = q.top();
//        q.pop();
//        cout << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << "\n";
//        ok[v[1]][v[2]][v[3]] = false;
//        int i = v[1], j = v[2];
//
//        if (v[3] == 0) {
//            string curr;
//            for(int k = 0; k < 3; k++) {
//                curr += mat[v[1]][v[2]+k];
//            }
//            if (curr != str) continue;
//            ans++;
//            for(int k = 0; k < 3; k++) {
//                mat[v[1]][v[2]+k] = 'X';
//            }
//
//            if (ok[i][j][1]) {
//                thru[i][j][1]--;
//                push_in(i, j, 1);
//            }
//            if (i-1 >= 0 && ok[i-1][j+1][1]) {
//                thru[i-1][j+1][1]--;
//                push_in(i-1, j+1, 1);
//            }
//            if (i-2 >= 0 && ok[i-2][j+2][1]) {
//                thru[i-2][j+2][1]--;
//                push_in(i-2, j+2, 1);
//            }
//        }
//        else {
//            string curr;
//            for(int k = 0; k < 3; k++) {
//                curr += mat[v[1]+k][v[2]];
//            }
//            if (curr != str) continue;
//            ans++;
//            for(int k = 0; k < 3; k++) {
//                mat[v[1]+k][v[2]] = 'X';
//            }
//
//            if (ok[i][j][0]) {
//                thru[i][j][0]--;
//                push_in(i, j, 0);
//            }
//            if (j-1 >= 0 && ok[i+1][j-1][0]) {
//                thru[i+1][j-1][0]--;
//                push_in(i+1, j-1, 0);
//            }
//            if (j-2 >= 0 && ok[i+2][j-2][0]) {
//                thru[i+2][j-2][0]--;
//                push_in(i+2, j-2, 0);
//            }
//        }
////        cout << v[0] << "\n";
//    }
//
//    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...