Submission #1143992

#TimeUsernameProblemLanguageResultExecution timeMemory
1143992SmuggingSpunDango Maker (JOI18_dango_maker)C++20
13 / 100
203 ms327680 KiB
#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
template<class T>void maximize(T& a, T b){
	if(a < b){
		a = b;
	}
}
int n, m;
namespace sub1{
	void solve(){
		vector<int>dp(1 << (n * m), -1);
		vector<vector<char>>a(n, vector<char>(m));
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				cin >> a[i][j];
			}
		}
		auto solve = [&] (auto&& self, vector<vector<bool>>state) -> int{
			int mask = 0;
			for(int i = 0, p = 0; i < n; i++){
				for(int j = 0; j < m; j++, p++){
					if(state[i][j]){
						mask |= 1 << p;
					}
				}
			}				
			int& ans = dp[mask];
			if(ans != -1){
				return ans;
			}
			for(int i = ans = 0; i < n; i++){
				for(int j = 0; j + 2 < m; j++){
					if(state[i][j] && state[i][j + 1] && state[i][j + 2]){
						if(a[i][j] == 'R' && a[i][j + 1] == 'G' && a[i][j + 2] == 'W'){
							state[i][j] = state[i][j + 1] = state[i][j + 2] = false;
							maximize(ans, self(self, state) + 1);
							state[i][j] = state[i][j + 1] = state[i][j + 2] = true;
						}
					}
				}
			}
			for(int i = 0; i + 2 < n; i++){
				for(int j = 0; j < m; j++){
					if(state[i][j] && state[i + 1][j] && state[i + 2][j]){
						if(a[i][j] == 'R' && a[i + 1][j] == 'G' && a[i + 2][j] == 'W'){
							state[i][j] = state[i + 1][j] = state[i + 2][j] = false;
							maximize(ans, self(self, state) + 1);
							state[i][j] = state[i + 1][j] = state[i + 2][j] = true;
						}
					}
				}
			}
			return ans;
		};
		cout << solve(solve, vector<vector<bool>>(n, vector<bool>(m, true)));
	}
}
namespace sub23{
	void solve(){
		vector<vector<char>>a(n + 1, vector<char>(m + 1));
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				cin >> a[i][j];
			}
		}
		vector<vector<int>>g, row(n + 1, vector<int>(m + 1, -1));
		for(int i = 1, id = 0; i <= n; i++){
			for(int j = 1; j + 1 < m; j++){
				if(a[i][j] == 'R' && a[i][j + 1] == 'G' && a[i][j + 2] == 'W'){
					row[i][j] = g.size();
					g.emplace_back(vector<int>());
				}
			}
		}
		auto add_edge = [&] (int u, int v){
			g[u].emplace_back(v);
			g[v].emplace_back(u);
		};
		for(int i = 1; i + 1 < n; i++){
			for(int j = 1; j <= m; j++){
				if(a[i][j] == 'R' && a[i + 1][j] == 'G' && a[i + 2][j] == 'W'){
					int id = g.size();
					g.emplace_back(vector<int>());
					if(row[i][j] != -1){
						add_edge(row[i][j], id);
					}
					if(row[i + 1][j - 1] != -1){
						add_edge(row[i + 1][j - 1], id);
					}
					if(j > 2 && row[i + 2][j - 2] > -1){
						add_edge(row[i + 2][j - 2], id);
					}
				}
			}
		}
		vector<vector<int>>dp(g.size(), vector<int>(2, -1));
		auto dfs = [&] (auto&& self, int s, int p) -> void{
			dp[s][0] = 0;
			dp[s][1] = 1;
			for(int& d : g[s]){
				if(d != p){
					self(self, d, s);
					dp[s][0] += max(dp[d][0], dp[d][1]);
					dp[s][1] += dp[d][0];
				}
			}
		};
		int ans = 0;
		for(int i = 0; i < g.size(); i++){
			if(dp[i][0] == -1){
				dfs(dfs, i, -1);
				ans += max(dp[i][0], dp[i][1]);
			}
		}
		cout << ans;
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n >> m;
	if(n <= 4 && m <= 4){
		sub1::solve();
	}
	else{
		sub23::solve();
	}
}

Compilation message (stderr)

dango_maker.cpp: In function 'int main()':
dango_maker.cpp:122:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...