이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*input
3 4
RGWR
GRGG
RGWW
5 5
RGRGW
GRRGW
WGGWR
RWRGW
RGWGW
4 4
RGWR
GRRG
WGGW
WWWR
3 4
RGWR
GRGG
RGWW
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
using vc = vector<char>;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, M;
cin >> N >> M;
vector<vc> sk(N, vc(M));
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
cin >> sk[i][j];
}
}
vii idesine(N, vi(M, 0));
vii iapacia(N, vi(M, 0));
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
if (j + 2 < M and sk[i][j] == 'R' and sk[i][j + 1] == 'G' and sk[i][j + 2] == 'W')
idesine[i][j]++;
if (i + 2 < N and sk[i][j] == 'R' and sk[i + 1][j] == 'G' and sk[i + 2][j] == 'W')
iapacia[i][j]++;
}
}
auto get = [&](vii vec, int i, int j) -> int {
if (i >= 0 and j >= 0 and i < (int)vec.size() and j < (int)vec[i].size())
return vec[i][j];
else
return 0;
};
vii sumUp(N, vi(M, 0));
vii sumLeft(N, vi(M, 0));
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
sumUp[i][j] = get(sumUp, i - 1, j) + get(iapacia, i - 2, j);
sumLeft[i][j] = get(sumLeft, i, j - 1) + get(idesine, i, j - 2);
}
}
// for (auto u : idesine) {
// for (auto e : u)
// printf("%d ", e);
// printf("\n");
// }
// printf("\n");
// for (auto u : iapacia) {
// for (auto e : u)
// printf("%d ", e);
// printf("\n");
// }
// printf("\n");
vii trysIdesine(N, vi(M, 0));
vii trysIapacia(N, vi(M, 0));
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
trysIdesine[i][j] = max(
get(iapacia, i, j) + get(trysIdesine, i, j - 1),
get(idesine, i, j - 2) + get(idesine, i + 1, j - 2)
+ get(idesine, i + 2, j - 2) + get(trysIdesine, i, j - 3)
);
trysIapacia[i][j] = max(
get(idesine, i, j - 2) + get(trysIapacia, i - 1, j),
get(iapacia, i - 2, j) + get(iapacia, i - 2, j - 1)
+ get(iapacia, i - 2, j - 2) + get(trysIapacia, i - 3, j)
);
}
}
// for (auto u : trysIdesine) {
// for (auto e : u)
// printf("%d ", e);
// printf("\n");
// }
// printf("\n");
// for (auto u : trysIapacia) {
// for (auto e : u)
// printf("%d ", e);
// printf("\n");
// }
// printf("\n");
vii dp(N, vi(M, 0));
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
dp[i][j] = max({
get(dp, i, j - 3) + get(trysIapacia, i, j),
get(dp, i - 3, j) + get(trysIdesine, i - 2, j),
get(dp, i - 3, j - 3) + get(trysIapacia, i, j) + get(trysIdesine, i - 2, j - 3),
get(dp, i - 3, j - 3) + get(trysIdesine, i - 2, j) + get(trysIapacia, i - 3, j),
get(dp, i, j - 1) + get(sumUp, i, j),
get(dp, i, j - 2) + get(sumUp, i, j) + get(sumUp, i, j - 1),
get(dp, i - 1, j) + get(sumLeft, i, j),
get(dp, i - 2, j) + get(sumLeft, i, j) + get(sumLeft, i - 1, j),
get(dp, i - 1, j - 1) + get(sumLeft, i, j) + get(sumUp, i - 1, j),
get(dp, i - 1, j - 1) + get(sumLeft, i, j - 1) + get(sumUp, i, j)
});
}
}
// for (auto u : dp) {
// for (auto e : u)
// printf("%d ", e);
// printf("\n");
// }
// printf("\n");
printf("%d\n", dp[N - 1][M - 1]);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |