炮兵阵地:
链接:
题目大意:
在一个棋盘上,一个棋子上下左右相邻两格内不能有别的棋,且棋子不能放在一些格子内。求最多的棋子数。
正文:
设 (f_{i,j,k}) 表示第 (i) 行的状态是 (j) 和前一行的状态是 (k) 的方案数。显然有:
[f_{i,j,k}=max_l{f_{i-1,k,l}+mathrm{num}(j)}quad(jland k=0,jland l=0,kland l=0)
]
代码:
const int N = 110, M = 1030;
inline ll Read()
{
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x * f;
}
int n, m;
int a[N], f[5][M][M];
int able[N], num[N], cnt;
void Prework()
{
for (int i = 0; i < (1 << m + 1); i++)
{
if ((i & (i << 1)) || (i & (i << 2)) || (i & (i >> 1)) || (i & (i >> 2))) continue;
able[++cnt] = i;
for (int j = i; j; j >>= 1)
if (j & 1) num[cnt]++;
if ((a[1] & i) == i) f[1][cnt][0] = num[cnt];
}
for (int j = 1; j <= cnt; j++)
{
if((able[j] & a[2]) != able[j]) continue;
for (int k = 1; k <= cnt; k++)
{
if((able[k] & a[1]) != able[k]) continue;
if (able[j] & able[k]) continue;
f[0][j][k] = max(f[0][j][k], f[1][k][0] + num[j]);
}
}
}
int ans;
int main()
{
n = Read(), m = Read();
for (int i = 1; i <= n; i++)
{
char c[N]; scanf ("%s", c + 1);
for (int j = 1; j <= m; j++)
a[i] = a[i] * 2 + (c[j] == 'P'? 1: 0);
}
Prework();
for (int i = 3; i <= n; i++)
{
for (int j = 1; j <= cnt; j++)
{
if((able[j] & a[i]) != able[j]) continue;
for (int k = 1; k <= cnt; k++)
{
if((able[k] & a[i - 1]) != able[k]) continue;
if (able[j] & able[k]) continue;
for (int l = 1; l <= cnt; l++)
{
if((able[l] & a[i - 2]) != able[l]) continue;
if ((able[j] & able[l]) || (able[k] & able[l])) continue;
f[i % 2][j][k] = max(f[i % 2][j][k], f[(i - 1) % 2][k][l] + num[j]);
}
if (i == n) ans = max(ans, f[i % 2][j][k]);
}
}
}
printf ("%d", ans);
return 0;
}