状压dp10pts求助

P2704 [NOI2001] 炮兵阵地

qnqfff @ 2022-01-24 14:39:32

RT
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
    char ch=getchar();int s=0,f=1;
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') ch=getchar(),f=-1;
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return f*s;
}
int n,m,a[100010],dp[1001][101][101];
signed main(){
    n=read();m=read();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            char x;
            cin>>x;
            x=(x=='P'?0:1);
            a[i]+=pow(2,m-j)*x;
        }
    for(int i=0;i<(1<<m);i++)
        if((i&(i<<1))==0&&(i&(i>>1))==0&&(i&(i<<2))==0&&(i&(i>>2))==0&&(i&a[1])==0)
            dp[1][i][0]+=__builtin_popcount(i);
    for(int i=0;i<(1<<m);i++)
        if((i&(i<<1))==0&&(i&(i>>1))==0&&(i&(i<<2))==0&&(i&(i>>2))==0&&(i&a[1])==0)
            for(int j=0;j<(1<<m);j++)
                if((j&(j<<1))==0&&(j&(j>>1))==0&&(j&(j<<2))==0&&(j&(j>>2))==0&&(j&a[2])==0&&(i&j)==0)
                    dp[2][i][j]+=__builtin_popcount(i)+__builtin_popcount(j);
    for(int i=3;i<=n;i++)
        for(int j=0;j<(1<<m);j++){
            if((j&(j<<1))!=0||(j&(j>>1))!=0||(j&(j<<2))!=0||(j&(j>>2))!=0||(j&a[i])!=0)
                continue;
            for(int k=0;k<(1<<m);k++){
                if((k&(k<<1))!=0||(k&(k>>1))!=0||(k&(k<<2))!=0||(k&(k>>2))!=0||(k&a[i])!=0)
                    continue;
                for(int l=0;l<(1<<m);l++){
                    if((k&j)!=0||(k&l)!=0||(j&l)!=0)
                        continue;
                    dp[i][j][k]=max(dp[i][j][k],dp[i-1][k][l]+__builtin_popcount(j));
                }
            }
        }
    int ans=0;
    for(int i=0;i<(1<<m);i++)
        for(int j=0;j<(1<<m);j++)
            ans=max(ans,dp[n][i][j]);   
    cout<<ans;
    return 0;
}

by 晴空一鹤 @ 2022-01-24 15:50:13

@する 你也来调啊


|