Submission #442041

#TimeUsernameProblemLanguageResultExecution timeMemory
442041aryan12Dango Maker (JOI18_dango_maker)C++17
100 / 100
741 ms63860 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
 
const int N = 6e3 + 5;
int n, m, diagonals;
char a[N][N];
int dp[N][3];
bool canTake[N][N][3];
 
/*
1 -> up to down
2 -> left to right
*/
 
bool up(int i, int j) {
    if(i <= 1 || i >= n) {
        return false;
    }
    if(a[i - 1][j] != 'R' || a[i][j] != 'G' || a[i + 1][j] != 'W') {
        return false;
    }
    return true;
}
 
bool left(int i, int j) {
    if(j <= 1 || j >= m) {
        return false;
    }
    if(a[i][j - 1] != 'R' || a[i][j] != 'G' || a[i][j + 1] != 'W') {
        return false;
    }
    return true;
}
 
int Recur(int row, int prev) {
    //cout << "Recur(" << row << ", " << prev << ")\n";
    if(row > n) {
        //cout << "Row already exceeded\n";
        return 0;
    }
    if(dp[row][prev] != -1) {
        //cout << "Dp already initialized = " << dp[row][prev] << "\n";
        return dp[row][prev];
    }
    int col = diagonals - row;
    int ans = Recur(row + 1, 0); //not choosing anything here :sadge:
    if(prev == 0) {
        //can choose both
        if(canTake[row][col][1]) {
            ans = max(ans, Recur(row + 1, 1) + 1);
        }
        if(canTake[row][col][2]) {
            ans = max(ans, Recur(row + 1, 2) + 1);
        }
    }
    else if(prev == 1) {
        //can't choose left to right
        if(canTake[row][col][1]) {
            ans = max(ans, Recur(row + 1, 1) + 1);
        }
    }
    else {
        //can't choose up to down
        if(canTake[row][col][2]) {
            ans = max(ans, Recur(row + 1, 2) + 1);
        }
    }
    return dp[row][prev] = ans;
}
 
void Solve() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            canTake[i][j][0] = true;
            canTake[i][j][1] = up(i, j);
            canTake[i][j][2] = left(i, j);
        }
    }
    int ans = 0;
    for(diagonals = 2; diagonals <= n + m; diagonals++) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < 3; j++) {
                dp[i][j] = -1;
            }
        }
        ans += Recur(1, 0); //row, previous taken...if any
        /*for(int i = 1; i <= n; i++) {
            for(int j = 0; j < 3; j++) {
                cout << dp[i][j] << " ";
            }
            cout << "\n";
        }*/
    }
    cout << ans << "\n";
}
 
int32_t main() {
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--) {
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...