zoukankan      html  css  js  c++  java
  • [POJ] 2223 Muddy Fields

    Time Limit: 1000MS      Memory Limit: 65536K
    Total Submissions: 11490        Accepted: 4270
    Description
    
    Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat. 
    
    To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows' field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field. 
    
    Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other. 
    
    Compute the minimum number of boards FJ requires to cover all the mud in the field.
    Input
    
    * Line 1: Two space-separated integers: R and C 
    
    * Lines 2..R+1: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.
    Output
    
    * Line 1: A single integer representing the number of boards FJ needs.
    Sample Input
    
    4 4
    *.*.
    .***
    ***.
    ..*.
    Sample Output
    
    4
    Hint
    
    OUTPUT DETAILS: 
    
    Boards 1, 2, 3 and 4 are placed as follows: 
    1.2. 
    .333 
    444. 
    ..2. 
    Board 2 overlaps boards 3 and 4.
    Source
    
    USACO 2005 January Gold

    二分图的精华大概就是这样的构图思想了,以行、列为左右部点,连续的*为点构图,求最小点覆盖即可。

    真的很神奇。

    //Stay foolish,stay hungry,stay young,stay simple
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    const int MAXN=10005;
    
    char mp[MAXN][MAXN];
    bool used[MAXN];
    int match[MAXN];
    
    int n,m;
    
    struct Edge{
        int next,to;
    }e[MAXN];
    int ecnt,head[MAXN];
    inline void add(int x,int y){
        e[++ecnt].next = head[x];
        e[ecnt].to = y;
        head[x]=ecnt;
    }
    bool hungary(int x)
    {
        for(int i=head[x];i;i=e[i].next)
        {
            int v = e[i].to;
            if(!used[v])
            {
                used[v] = true;
                if(match[v]<0||hungary(match[v]))
                {
                    match[v]=x;
                    return true;
                }
            }
        }
        return false;
    }
    int dfs()
    {
        int ret=0;
        memset(match,-1,sizeof(match));
        for(int i = 1;i <=n*m;i++)
        {
            memset(used,false,sizeof(used));
            if(hungary(i)) ret++;
        }
        return ret;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(head,0,sizeof(head));
            ecnt=0;
            for(int i=1;i<=n;i++)
                scanf("%s",mp[i]+1);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(mp[i][j] == '*')
                    {
                        int x = i,y = j;
                        while(x > 1 && mp[x-1][j] == '*') x--;
                        while(y > 1 && mp[i][y-1] == '*') y--;
                        add((x-1)*m+j,i*m+(y-1)+n*m);
                    }
                }
            }
            printf("%d
    ",dfs());
        }
        return 0;
    }

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247455.html

  • 相关阅读:
    拓端tecdat|R语言KERAS深度学习CNN卷积神经网络分类识别手写数字图像数据(MNIST)
    拓端tecdat|R语言资产配置策略量化模型:改进的移动平均线策略动态回测
    拓端tecdat|R语言量化:合成波动率指数移动平均策略分析标准普尔500波动率指数(VIX)
    拓端tecdat|Python中的多项式回归拟合非线性关系实例
    从集团管控到集团赋能
    性能之巅-优化你的程序
    3个小时,从学到做,我用低代码平台搭了一套管理系统
    Hadoop架构原理
    硬核操作系统讲解
    一文弄懂什么是DevOps
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247455.html
Copyright © 2011-2022 走看看