20pts求助

P2704 [NOI2001] 炮兵阵地

asas111 @ 2023-10-31 22:52:31

#include<bits/extc++.h>
#define ll long long
#define _for(i,a,b) for(ll i=(a);i<=(b);++i)
#define _rep(i,a,b) for(ll i=(a);i>=(b);--i)
#define INF 1e18
using namespace std;
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(ll x){if(x<0){putchar('-');x=-x;}if(x>9)write(x/10);putchar(x%10+'0');}
int n,m,k[109],p,b[1<<15],f[4][1<<11][1<<11],o,a,x[1<<15];
char u;
bool g(int j){return !((j&(j>>1))&&(j&(j>>2)));}
bool g(int i,int j){return !(k[i]&j);}
int main(){
    n=read(),m=read();
    p=1<<m;
    _for(i,1,n)
        _for(j,1,m){
            cin>>u;
            k[i]=(k[i]<<1)+(u=='H');
        }
    _for(i,1,p-1)
        b[i]=b[i&(i-1)]+1;
    _for(i,0,p-1)
        if(g(i))
            x[o++]=i;
    _for(z,0,o-1){
        int i=x[z];
        if(g(1,i))
            f[1][i][0]=b[i];
    }
    _for(z,0,o-1){
        int i=x[z];
        if(g(2,i))
            _for(y,0,o-1){
                int j=x[y];
                if(g(1,j)&&(!(i&j)))
                    f[2][i][j]=f[1][j][0]+b[i];
            }
    }
    _for(i,3,n)
        _for(z,0,o-1){
            int j=x[z];
            if(g(i,j))
                _for(y,0,o-1){
                    int k=x[y];
                    if(g(i-1,k)&&(!(j&k)))
                        _for(r,0,o-1){
                            int l=x[r];
                            if(g(i-2,l)&&(!((j&l)||(k&l))))
                                f[i%3][j][k]=max(f[i%3][j][k],f[(i-1)%3][k][l]+b[j]);
                        }
                }
        }
    _for(i,0,o-1)
        _for(j,0,o-1)
            a=max(a,f[n%3][x[i]][x[j]]);
    cout<<a;
    return 0;
}

|