Man_CCNU @ 2022-10-24 11:12:31
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N = 1e2 + 10;
int f[N][1<<11],g[N],cnt[N], n, m,res;
vector<int>state;
vector<int>h[1 << 10];
bool check(int x) //判断两个1的间隔>=2,
{
if (x & (x>>1)) return false;
if (x & (x >> 2)) return false;
return true;
}
int get_sum(int x)
{
int res = 0;
while (x) {
if (x & 1) {
res++;
}
x = x >> 1;
}
return res;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < 1 << m; i++) {
if (check(i)) {
state.push_back(i);
cnt[i] = get_sum(i);
}
}
for (int i = 0; i < state.size(); i++) {
for (int j = 0; j < state.size(); j++) {
int a = state[i];
int b = state[j];
if ((a & b) == 0) {
h[a].push_back(b);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == 'H') {
g[i] += (1 << (j - 1));
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < state.size(); j++) {
int a = state[j];
if (a & g[i]) continue;
for (int k = 0; k < h[a].size(); k++) {
int b = h[a][k];
if (b & g[i - 1]) continue;
int s = cnt[a];
if (i >= 2) {
for (int p = 0; p < h[b].size(); p++) {
int c = h[b][p];
if (g[i - 2] & c) continue;
if (a & c) continue;
f[i][a] = max(f[i][a], f[i-2][c] + cnt[b] + cnt[a]);
}
}
else {
f[i][a] = max(f[i][a], f[i - 1][b] + cnt[a]);
}
}
}
}
for (int i = 0; i < state.size(); i++) {
res = max(res, f[n][state[i]]);
}
cout << res << endl;
return 0;
}
by Man_CCNU @ 2022-10-24 11:29:18
突然发现状态转移有问题