Submission #927199

#TimeUsernameProblemLanguageResultExecution timeMemory
927199Gromp15Dango Maker (JOI18_dango_maker)C++17
100 / 100
909 ms20732 KiB
#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
void test_case() {
	int n, m;
	cin >> n >> m;
	vector<string> a(n);
	for (auto &x : a) cin >> x;
	vector<vector<bool>> vis(n, vector<bool>(m));
	const int dx[][2] = {{1, 2}, {-1, 1}, {-1, -2}};
	const string match = "RGW";
	auto in = [&](int x, int y) {
		return x>=0 && y>=0 && x<n && y<m;
	};
	auto check = [&](int x, int y, queue<ar<int, 2>> &v) {
		int idx = a[x][y] == 'R' ? 0 : a[x][y] == 'G' ? 1 : 2;
		bool ok = 1;
		for (int d = 0; d < 2; d++) {
			int nx = x + dx[idx][d];
			if (!in(nx, y) || a[nx][y] != match[idx + dx[idx][d]]) {
				ok = 0;
				break;
			}
		}
		if (ok) {
			if (!vis[x][y]) v.push({x, y}), vis[x][y] = 1;
			for (int d = 0; d < 2; d++) { 
				int nx = x + dx[idx][d];
				if (!vis[nx][y]) {
					vis[nx][y] = 1;
					v.push({nx, y});
				}
			}
		}
		bool ok2 = 1;
		for (int d = 0; d < 2; d++) {
			int ny = y + dx[idx][d];
			if (!in(x, ny) || a[x][ny] != match[idx + dx[idx][d]]) {
				ok2 = 0;
				break;
			}
		}
		if (ok2) {
			if (!vis[x][y]) v.push({x, y}), vis[x][y] = 1;
			for (int d = 0; d < 2; d++) { 
				int ny = y + dx[idx][d];
				if (!vis[x][ny]) {
					v.push({x, ny});
					vis[x][ny] = 1;
				}
			}
		}
	};
	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) if (!vis[i][j]) {
			queue<ar<int, 2>> q;
			check(i, j, q);
			vector<ar<int, 2>> cur;
			while (q.size()) {
				auto [x, y] = q.front();
				cur.push_back(q.front());
				q.pop();
				check(x, y, q);
			}
			auto good = [&](int idx) {
				auto [x, y] = cur[idx];
				return x+2 < n && a[x][y] == 'R' && a[x+1][y] == 'G' && a[x+2][y] == 'W';
			};
			if (cur.size()) {
				sort(all(cur));
				vector<int> dp(8);
				int L = -1, R = -1;
				for (int k = 0; k < sz(cur); k++) {
					int r = k;
					while (r+1 < sz(cur) && cur[r+1][0] == cur[r][0]) r++;
					int curl = cur[k][1], curr = cur[r][1];
					if (L == -1) {
						if (r-k+1 == 3) dp[0] = 1;
						if (good(k)) dp[1] = 1;
					}
					else {
						vector<int> dp2(8);
						for (int mask = 0; mask < 8; mask++) {
							int nw = 0;
							for (int z = L; z <= R; z++) {
								int idx = z - L;
								if ((mask >> idx & 1) && (curl <= z && z <= curr)) {
									nw |= 1 << (z - curl);
								}
							}
							ckmax(dp2[nw], dp[mask]);
							if (!(nw & 1) && good(k)) {
								ckmax(dp2[nw | 1], dp[mask] + 1);
							}
							if (!nw && r-k+1 == 3) ckmax(dp2[nw], dp[mask] + 1);
						}
						swap(dp, dp2);
					}
					//for (int i = 0; i < 8; i++) cout << dp[i] << " \n"[i==7];
					assert(r-k+1 <= 3);
					L = curl, R = curr;
					k = r;
				}
				/*
				cout << "--------\n";
				for (auto [x, y] : cur) cout << x << " " << y << '\n';
				cout << *max_element(all(dp)) << "\n\n";
				*/
				ans += *max_element(all(dp));
			}
		}
	}
	cout << ans << '\n';



}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
//    cin >> t;
    while (t--) test_case();
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...