POJ 2226 Muddy Fields

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


分析:二分图最小点集覆盖。二分图建图方法如下,把每段连续的横向的水洼看成是一个X集合中的点,每段连续的纵向的水洼看成是Y集合中的点。矩阵每个有水单元看成是一个边,它连接了它所在的横向水洼在X集合中对应的点和它所在的纵向水洼在Y集合中对应的点。(对于一段连续的水洼,如果要铺木板,一定要铺得尽量长)这样最小点集覆盖的意义就变成了,矩阵中的每个有水的单元(二分图中的每条边)所在的横向和纵向的水洼(在X集合和Y集合中的两个端点)至少有一个被覆盖。求至少需要选几个点。
迷之建图。


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int r,c,nc,nr,ans,lft[1005],a[1005][1005],b[1005][1005],d[1005][1005];
char ch[1005][1005];
bool vis[1005];
bool match(int u)
{
    for(int i=1;i<=nc;i++)
        if(d[u][i]&&!vis[i])
        {
            vis[i]=1;
            if(lft[i]==-1||match(lft[i]))
            {
                lft[i]=u;
                return 1;
            }
        }
    return 0;
}
int main()
{
    scanf("%d%d",&r,&c);
    for(int i=1;i<=r;i++)
        scanf("%s",ch[i]+1);
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
            if(ch[i][j]=='*')
            {
                if(ch[i][j-1]=='*')
                    a[i][j]=a[i][j-1];
                else
                    a[i][j]=++nr;
            }
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
            if(ch[i][j]=='*')
            {
                if(ch[i-1][j]=='*')
                    b[i][j]=b[i-1][j];
                else
                    b[i][j]=++nc;
                d[a[i][j]][b[i][j]]=1;
            }
    memset(lft,-1,sizeof(lft));
    for(int i=1;i<=nr;i++)
    {
        memset(vis,0,sizeof(vis));
        if(match(i))
            ++ans;
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值