292 400多的方法
作者:
wuxikui
,
2022-02-09 22:03:14
,
所有人可见
,
阅读 155
#include <bits/stdc++.h>
using namespace std;
const int M = 12, N = 110;
int f[2][1 << M][1 << M];
int n, m;
int g[N], cnt[1 << M];
vector<int> legal;
int count(int x)
{
int res = 0;
for (int i = 0; i < m; i ++ ) res += (x >> i & 1);
return res;
}
bool check(int x)
{
for (int i = 0; i < m; i ++ )
if ((x >> i & 1) && ((x >> i + 1 & 1) || (x >> i + 2 & 1))) return false;
return true;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
{
string s;
cin >> s;
for (int j = 0; j < m; j ++ ) g[i] += (s[j] == 'H') << j;
}
for (int i = 0; i < 1 << m; i ++ ) if (check(i)) legal.push_back(i), cnt[i] = count(i);
for (int i = 1; i <= n; i ++ )
for (int j = 0; j < legal.size(); j ++ )
if ((g[i] & legal[j]) == 0)
for (int k = 0; k < legal.size(); k ++ )
if ((g[i - 1] & legal[k]) == 0)
for (int l = 0; l < legal.size(); l ++ )
if (!(legal[j] & legal[k]) && !(legal[j] & legal[l]) && !(legal[l] & legal[k]))
f[i & 1][k][j] = max(f[i & 1][k][j], f[i - 1 & 1][l][k] + cnt[legal[j]]);
int res = 0;
for (int i = 0; i < legal.size(); i ++ )
for (int j = 0; j < legal.size(); j ++ )
res = max(res, f[n & 1][i][j]);
printf("%d\n", res);
return 0;
}
没加什么优化