# 11086: 【原1086】炮兵阵地

### 题目描述

author: duruofei 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1086

## Sample Input

5 4
PHPP
PPHH
PPPP
PHPP
PHHP


## Sample Output

6


## FineArtz's solution

/* 炮兵阵地 */
#include <iostream>
using namespace std;

int mapping[1025];
long long f[105][65][65];
long long sum[65], sta[65], a[105];
int cnt = 0, n, m;

inline bool check(int x){
return !((x & (x << 1)) || (x & (x << 2)));
}

int calcSum(int x){
int ret = 0;
while (x){
++ret;
x = x & (x - 1);
}
return ret;
}

void init(){
for (int i = 0; i < (1 << m); ++i){
if (check(i)){
sta[++cnt] = i;
mapping[i] = cnt;
sum[cnt] = calcSum(i);
}
}
}

int main(){
cin >> n >> m;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
char ch;
cin >> ch;
a[i] <<= 1;
if (ch == 'H')
a[i] = a[i] | 1;
}
}
init();
long long ans = 0;
for (int i = 1; i <= cnt; ++i){
if (!(sta[i] & a[1])){
f[1][i][0] = max(f[1][i][0], sum[i]);
ans = max(ans, f[1][i][0]);
}
}
if (n == 1){
cout << ans << endl;
return 0;
}
for (int i = 1; i <= cnt; ++i){
if (!(sta[i] & a[2])){
for (int j = 1; j <= cnt; ++j){
if (!(sta[j] & a[1])){
if (!(sta[i] & sta[j])){
f[2][i][j] = max(f[2][i][j], f[1][j][0] + sum[i]);
ans = max(ans, f[1][i][0]);
}
}
}
}
}
if (n == 2){
cout << ans << endl;
return 0;
}
for (int l = 3; l <= n; ++l){
for (int i = 1; i <= cnt; ++i){
if (!(sta[i] & a[l])){
for (int j = 1; j <= cnt; ++j){
if (!(sta[j] & a[l - 1])){
for (int k = 1; k <= cnt; ++k){
if (!(sta[k] & a[l - 2])){
if (!((sta[i] & sta[j]) | (sta[i] & sta[k]) | (sta[j] & sta[k]))){
f[l][i][j] = max(f[l][i][j], f[l - 1][j][k] + sum[i]);
ans = max(ans, f[l][i][j]);
}
}
}
}
}
}
}
}
cout << ans << endl;
return 0;
}


## yyong119's solution

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAXN = 105;
const int MAXM = 15;
const int MAXS = 65;

char matrix[MAXN][MAXM];
int f[MAXN][MAXS][MAXS], vst[MAXS], num[MAXS], originState[MAXN];

int main() {

int n, m; scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
originState[i] = 0;
scanf("%s", &matrix[i]);
for (int j = 0; j < m; ++j)
if (matrix[i][j] == 'P')
originState[i] += 1 << (m - j - 1);
}

memset(f, sizeof(f), 0);
int vnum = 0;
for (int i = 0; i < (1 << m); ++i)
if (!(i & (i << 1)) && !(i & (i << 2))) {
vst[vnum] = i;
int tmp = 0, r = i;
for (; r; r &= r - 1, ++tmp);
num[vnum++] = tmp;
}

for (int i = 0; i < vnum; ++i)
if (!(vst[i] & (~originState[0])))
f[0][i][0] = num[i];

for (int i = 1; i < n; ++i)
for (int j = 0; j < vnum; ++j) { //第i行
if ((~originState[i]) & vst[j]) continue;
for (int k = 0; k < vnum; ++k) { //第i - 1行
if ((~originState[i - 1]) & vst[k]) continue;
if (vst[j] & vst[k]) continue;
for (int t = 0; t < vnum; ++t) { //第i - 2行
if (vst[j] & vst[t]) continue;
if (f[i - 1][k][t] == -1) continue;
f[i][j][k] = max(f[i][j][k], f[i - 1][k][t] + num[j]);
}
}
}

int ret = 0;
for (int i = 0; i < vnum; ++i)
for (int j = 0; j < vnum; ++j)
ret = max(f[n - 1][i][j], ret);
printf("%d\n", ret);
return 0;
}