[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

原文地址:https://www.cnblogs.com/ghostcai/p/9247455.html