POJ 2226 Muddy Fields (最小点覆盖)

52 篇文章 0 订阅
Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6671 Accepted: 2458

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

思路:题意是求最小的木板数,使得所有的'*'被覆盖,并且不覆盖到'.'。那么对于每个‘*’点来说,要么被模板横着覆盖,要么被木板竖着覆盖,我们对于所有横着的尽可能长的木板集合作为X集,竖着的尽可能长的木板集合作为Y集,然后对于每个‘*’点,作为一条边,从它所属的X集到Y集连接一条边,这样,求出该X到Y的二分图的最大匹配数就是最小点覆盖,最小点覆盖就是用最少的点覆盖所有的边,这里的边就是指‘*’点。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=3000;
const int maxm=100000;
int to[maxm],next[maxm],head[maxn],edge;
bool vis[maxn];
int match[maxn],n,p,belong[55][55][2];
char map[55][55];
inline void add(int u,int v)
{
    to[edge]=v,next[edge]=head[u],head[u]=edge++;
}
bool find(int now)
{
    int i,v;
    for(i=head[now]; ~i; i=next[i])
        if(!vis[v=to[i]])
        {
            vis[v]=true;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=now;
                return true;
            }
        }
    return false;
}
int main()
{
    int i,j,m,n,u,v,cnt;
    while(~scanf("%d%d",&n,&m))
    {
        memset(match,-1,sizeof(match));
        memset(head,-1,sizeof(head));
        cnt=edge=0;
        for(i=0; i<n; i++)
            scanf("%s",map[i]);
        bool f=0;
        for(i=0; i<n; i++)
            for(j=0,f=0; j<m; j++)
            {
                if(map[i][j]=='*')
                {
                    if(!f) cnt++,f=1;
                    belong[i][j][0]=cnt;
                }
                else if(f) f=0;
            }
        u=cnt;
        for(j=0; j<m; j++)
            for(i=0,f=0; i<n; i++)
            {
                if(map[i][j]=='*')
                {
                    if(!f) cnt++,f=1;
                    belong[i][j][1]=cnt;
                }
                else if(f) f=0;
            }
        v=cnt;
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
                if(map[i][j]=='*')
                    add(belong[i][j][0],belong[i][j][1]);
        int res=0;
        for(i=1; i<=u; i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i))
                res++;
        }
        printf("%d\n",res);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值