蒟蒻求调状压dp

P2704 [NOI2001] 炮兵阵地

dk_qwq @ 2022-09-22 21:15:36

球球了,来位大神帮我调调吧,已经调一晚上了

https://www.luogu.com.cn/record/87225453

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int N=105,M=1<<11;
string map[N];
vector<int>v[N];
int n,m;
inline bool check(int k,int x){
    if((x&(x<<1))||(x&(x<<2))) return 0;
    for(int i=0;i<n;i++)
        if((x&(1<<i))&&(map[k][i]=='H')) return 0;
    return 1;
}
inline int count(int x){
    int cnt=0;
    while(x){
        if(x&1) cnt++;
        x>>=1;
    }
    return cnt;
}
int g[M][M];
int f[M][M];
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) cin>>map[i];
    for(int i=1;i<=n;i++){
        for(int j=0;j<(1<<m);j++){
            if(!check(i,j)) continue;
            v[i].push_back(j);
        }
    }
    for(auto i:v[1]) g[0][i]=count(i);
    for(auto i:v[1]){
        for(auto j:v[2]){
            if(i&j) continue;
            f[i][j]=max(f[i][j],g[0][i]+count(j));
        }
    }
    swap(f,g);
    memset(f,0,sizeof(f));
    for(int k=3;k<=n;k++){
        for(auto i:v[k]){
            for(auto j:v[k-1]){
                if(i&j) continue;
                for(auto l:v[k-2]){
                    if(j&l||i&l) continue;
                    f[j][i]=max(f[j][i],g[l][j]+count(i));
                }
            }
        }
        swap(f,g);
        memset(f,0,sizeof(f));
    }
    int ans=0;
    for(auto i:v[n-1]){
        for(auto j:v[n]){
            if(i&j) continue;
            ans=max(ans,g[i][j]);
        }
    }
    cout<<ans<<endl;
}

by dk_qwq @ 2022-09-22 21:46:27

@dkqwq 已AC,check里面把m写成了n/kk


|