洛璟 @ 2021-04-25 15:27:05
除了#1其他全WA了,也不知道哪里错了,调了好久了没看出来呜呜
#include<bits/stdc++.h>
using namespace std;
int n, m, f[1030][1030][3], a[110], sum[1030];
inline int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c>'9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
x = (x << 3) + (x << 1) + (c ^ '0');
c = getchar();
}
return x * f;
}
inline int getone(int x)
{
int ans = 0;
while (x > 0)
{
if (x & 1 == 1)ans++;
x = x >> 1;
}
return ans;
}
signed main()
{
n = read();
m = read();
for (int i = 1;i <= n;++i)
{
for (int j = 1;j <= m;++j)
{
char x;
cin >> x;
a[i] <<= 1;
a[i] += (x == 'H' ? 1 : 0);
}
}
for (int i = 0;i < (1 << m);++i)
{
sum[i] = getone(i);
}
for (int i = 0;i < (1 << m);++i)
{
if ((i & a[1]) != 0 || (i & (i << 1)) != 0 || (i & (i << 2)) != 0) continue;
f[0][i][1] = sum[i];
}
for (int i = 0;i <= (1 << m);++i)
{
for (int j = 0;j < (1 << m);++j)
{
if ((i & j) != 0 || (i & a[1]) != 0 || (j & a[2]) != 0 || (i & (i << 1)) != 0 || (i & (i << 2)) != 0 || (j & (j << 1)) != 0 || (j & (j << 2)) != 0) continue;
f[i][j][2] = sum[i] + sum[j];
}
}
for (int i = 3;i <= n;++i)
{
for (int j = 0;j < (1 << m);++j)//上一行
{
if ((j & a[i - 1]) != 0 || (j & (j << 1)) != 0 || (j & (j << 2)) != 0) continue;
for (int k = 0;k < (1 << m);++k)//当前
{
if ((k & a[i]) != 0 || k & j != 0 || (k & (k << 1)) != 0 || (k & (k << 2)) != 0) continue;
for (int l = 0;l < (1 << m);++l)//上面的上面
{
if ((l & a[i - 2]) != 0 || (l & j) != 0 || (l & k) != 0 || (l & (l << 1)) != 0 || (l & (l << 2)) != 0) continue;
f[j][k][i % 3] = max(f[j][k][i % 3], f[l][j][(i - 1) % 3] + sum[k]);
}
}
}
}
int ans = -114514;
for (int i = 0;i < (1 << m);++i)
{
for (int j = 0;j < (1 << m);++j)
{
ans = max(f[i][j][n % 3], ans);
}
}
printf("%d", ans);
return 0;
}