【Luogu P2704】[NOI2001] 炮兵阵地

炮兵阵地:

链接:

洛谷

题目大意:

在一个棋盘上,一个棋子上下左右相邻两格内不能有别的棋,且棋子不能放在一些格子内。求最多的棋子数。

正文:

\(f_{i,j,k}\) 表示第 \(i\) 行的状态是 \(j\) 和前一行的状态是 \(k\) 的方案数。显然有:

\[f_{i,j,k}=\max_l\{f_{i-1,k,l}+\mathrm{num}(j)\}\quad(j\land k=0,j\land l=0,k\land 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;
}
posted @ 2021-07-15 07:45  Jayun  阅读(45)  评论(0编辑  收藏  举报