比赛 状态压缩DP练习 评测结果 EEEEEEEEEE
题目名称 炮兵阵地 最终得分 0
用户昵称 氢氦 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2019-05-31 18:19:45
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

inline int read()
{
    register int x = 0,w = 1,ch = getchar();
    while (ch < '0' || ch > '9'){if (ch == '-') w = -1;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - 48;ch = getchar();}
    return x * w;
}

int boom[105]; char ch;
int n,m,top,ans = 0;
int f[105][2000][2000];

int main()
{
    freopen ("cannon.in", "r", stdin);
    freopen ("cannon.out", "w", stdout);
    n = read(), m = read();
    top = pow(2,m);
    for (int i = 1; i <= n; i++){
            int now = 0;
        for (int j = 1; j <= m; j++){
            cin >> ch;
            now <<= 1;
            if (ch == 'H') ++now;
        }
        boom[i+1] = now;
    }
    for (int i = 1; i <= n; i++){
        for(int j = 0; j < top; j++){
            if ((j & (j << 1)) || (j & (j << 2)))continue;
            if (j & boom[i+1])continue;
            int num = __builtin_popcount(j);
            for (int k = 0; k < top; k++){
                if ((k & (k << 1)) || (k & (k << 2)))continue;
                if (k & j)continue;
                if (k & boom[i])continue;
                for (int l = 0; l < top; l++){
                    if ((l & (l << 1)) || (l & (l << 2)))continue;
                    if (k & l || l & j)continue;
                    if (l & boom[i-1])continue;
                    f[i][j][k] = max (f[i][j][k], f[i-1][k][l] + num);
                    ans = max (f[i][j][k], ans);
                }
            }
        }
    }
    printf ("%d", ans);
    return 0;
}