zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    exec系列函数和system函数
    fork函数相关总结
    文件的内核结构file和dup实现重定向
    进程基本概述
    fcntl 函数与文件锁
    文件的属性
    目录的操作
    文件的读取写入
    文件的打开关闭
    浅谈原始套接字 SOCK_RAW 的内幕及其应用(port scan, packet sniffer, syn flood, icmp flood)
  • 原文地址:https://www.cnblogs.com/zufezzt/p/6361446.html
Copyright © 2011-2022 走看看