This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int nax = 105;
char mat[nax][nax];
const string str = "RGW";
/*
0 - desine
1 - virsus
*/
bool gali[nax][nax][2];
int n, m;
int dp[nax][3];
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);
}
bool in(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
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 (mat[i][j] == 'G') {
if (j-1 >= 0) {
gali[i][j][0] = check_ok(i, j-1, 0);
}
if (i-1 >= 0) {
gali[i][j][1] = check_ok(i-1, j, 1);
}
}
}
}
vector<pair<int, int>> s[nax+nax];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
s[i+j].push_back({i, j});
}
}
int ans = 0;
for(int sz = 0; sz < m+n; sz++) {
for(const auto& p : s[sz]) {
int i = p.first, j = p.second;
// cout << "curr: " << i << " " << j << "\n";
if (i) {
dp[i][0] = max({dp[i-1][0], dp[i-1][1], dp[i-1][2]});
if (gali[i][j][0]) {
dp[i][1] = max(dp[i-1][0], dp[i-1][1]) + 1;
}
if (gali[i][j][1]) {
dp[i][2] = max(dp[i-1][0], dp[i-1][2]) + 1;
}
}
else {
if (gali[i][j][0]) {
dp[i][1] = ans+1;
}
if (gali[i][j][1]) {
dp[i][2] = ans+1;
}
}
// cout << i << " " << j << ":\n";
for(int k = 0; k < 3; k++) {
ans = max(ans, dp[i][k]);
// cout << dp[i][k] << " ";
}
// cout << "\n";
}
for(int i = 0; i < n; i++) {
dp[i][0] = ans;
}
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |