2010-2011 ACM-ICPC, NEERC, Southern Subregional Contest C Explode 'Em All

暴力枚举,状态压缩。

枚举哪几行放,复杂度为$O(2^{25})$,大概有$3000$多万种情况。假设有$x$行放了,没放的那几行状态或起来为$st$,如果$st$中$1$的个数大于$x$,那么不可取;否则用$x$更新答案。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-10;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c))
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
}

char s[30][30];
int n,m,ans,num[30];
int f[40000000];

void dfs(int x,int cnt,int st)
{
    if(cnt>=ans) return ;
    if(x==n)
    {
        if(f[st]<=cnt) ans=min(ans,cnt);
        return ;
    }

    dfs(x+1,cnt+1,st);
    dfs(x+1,cnt,st|num[x]);
}

int lowbit(int x)
{
    return x&(-x);
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    for(int i=1;i<(1<<25);i++) f[i]=f[i-lowbit(i)]+1;

    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%s",s[i]);
        for(int j=0;j<m;j++)
            if(s[i][j]=='*') num[i]=num[i]+(1<<j);
    }

    ans=min(n,m); dfs(0,0,0);
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2017-02-02 18:28  Fighting_Heart  阅读(364)  评论(0编辑  收藏  举报