30求助

P2704 [NOI2001] 炮兵阵地

Episode9 @ 2019-01-26 15:39:54

#include<bits/stdc++.h>
using namespace std;
int n,m;
int p[105],f[105][2005][2005],s[1<<10],top=0,cnt[1<<10];
int countBits(int x) 
{
    int count=0;
    while(x) 
    {
        if (x&1) count++;
        x>>=1;
    }
    return count;
}
int main()
{
    cin>>n>>m;
    for (int i=1;i<=n;i++)
     for (int j=1;j<=m;j++)
    {
        char ch;
        cin>>ch;
        if (ch=='H') p[i]+=1<<j;
    }
    for (int i=0;i<=(1<<m)-1;i++) if ((i&(i<<2))==0&&(i&(i<<1))==0) {s[++top]=i;cnt[i]=countBits(i);}
    for (int i=1;i<=top;i++) if ((p[1]&s[i])==0) f[1][s[i]][1]=cnt[s[i]];   
    for(int i=1;i<=top;i++)
        for(int j=1;j<=top;j++)
         if ((s[i]&s[j])==0&&(p[1]&s[i])==0&&(p[2]&&s[j])==0) f[2][s[j]][s[i]]=cnt[s[i]]+cnt[s[j]];
    for (int i=3;i<=n;i++)
    {
        for (int j=1;j<=top;j++)
        {
            if ((p[i-1]&s[j])!=0) continue;
            for (int k=1;k<=top;k++)
            {
                if ((p[i]&s[k])!=0) continue;
                for (int g=1;g<=top;g++)
                {
                    if ((p[i-2]&s[g])!=0) continue;
                    if ((s[j]&s[k])==0&&(s[j]&s[g])==0&&(s[k]&s[g])==0) f[i][s[k]][s[j]]=max(f[i][s[k]][s[j]],f[i-1][s[j]][s[g]]+cnt[s[k]]);
                }
            }
        }
    }
    int ans=0;
    for (int i=1;i<=top;i++) 
    for (int j=1;j<=top;j++)
    ans=max(f[n][s[i]][s[j]],ans);
    cout<<ans<<endl;
    return 0;
}

|