poj 1185 炮兵阵地

题目描述

http://poj.org/problem?id=1185

状压dp经典题

Solution

能够发现一行的状态远不止1024,所以可以把这东西扔到状态里即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdio>
#define maxn 110
#define maxm 11
using namespace std;

int n, m, g[maxn];

int st[100], c1, cnt[110];

int f[maxn][100][100];

int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
char s[maxm]; scanf("%s", s + 1);
for (int j = 1; j <= m; ++j)
if (s[j] == 'H') g[i] |= 1 << m - j;
}
for (int i = 0; i < (1 << m); ++i) {
if ((i << 1 & i) || (i << 2 & i)) continue;
st[++c1] = i;
for (int j = 1; j <= m; ++j) cnt[c1] += i >> j - 1 & 1;
}
for (int i = 1; i <= c1; ++i) {
if (g[1] & st[i]) continue;
f[1][i][i] = cnt[i];
}
for (int i = 1; i < n; ++i)
for (int j = 1; j <= c1; ++j)
for (int k = 1; k <= c1; ++k) {
if (!f[i][j][k]) continue;
for (int o = 1; o <= c1; ++o) {
if ((st[j] & st[o]) || (st[k] & st[o]) || (g[i + 1] & st[o])) continue;
f[i + 1][o][j] = max(f[i + 1][o][j], f[i][j][k] + cnt[o]);
}
}
int ans = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= c1; ++j)
for (int k = 1; k <= c1; ++k) ans = max(ans, f[i][j][k]);
cout << ans << endl;
return 0;
}