• 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
    ",ans);
        return 0;
    }
  • 相关阅读:
    笔记0510
    笔记0514
    笔记0521
    GridView专题
    笔记0418
    笔记0516
    笔记0515
    笔记0507
    Python 安装与环境变量配置
    ffmpeg 下载安装和简单应用
  • 原文地址:https://www.cnblogs.com/zufezzt/p/6361446.html
Copyright © 2020-2023  润新知