POJ - 2226 Muddy Fields

A - Muddy Fields  POJ - 2226 

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.
 
题意:给出一个田地,然后'*'号代表是泥土地,'.'代表植物,我们有无数块木板,宽为1,长为任意的,然后求用最少的木板数量盖住所有的泥土地,不能盖住植物,木板可以重叠
 
思路:因为我们盖住泥地的话,肯定是尽量长一点,也就是说我们可以把图中的泥土分为行联通块和列联通块, 因为一个模板肯定是把一个行联通块或者列联通块盖住,所以我们可以
把联通块当成是一个点,然后我们又必须要把所有的点都覆盖住,就是求一个二分图的最小点覆盖,
又有个定理是 最小点覆盖数=二分图最大匹配
 
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int mp[1001][1001];
int vis[1001];
int girl[1001];
int lid,rid;
int dfs(int x)
{
    for(int i=1;i<=rid;i++)
    {
        if(mp[x][i]&&vis[i]==0)
        {
            vis[i]=1;
            if(girl[i]==0||dfs(girl[i]))
            {
                girl[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int n,m;
    char str[1001][1001];
    int col[1001][1001],row[1001][1001];
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",str[i]+1);
    }
    lid=0,rid=0;
    memset(col,0,sizeof(col));
    memset(row,0,sizeof(row));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(str[i][j]=='*')
            {
                int x=i,y=j;
                if(col[i][j]==0)
                {
                    lid++;
                    while(y<=m&&str[i][y]=='*') col[i][y++]=lid;//给所有的行联通块编号
                }
                if(row[i][j]==0)
                {
                    rid++;
                    while(x<=n&&str[x][j]=='*') row[x++][j]=rid;//给所有的列联通快编号
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(str[i][j]=='*')
            {
                mp[col[i][j]][row[i][j]]=1;
            }
        }
    }
    int sum=0;
    for(int i=1;i<=lid;i++)
    {
        memset(vis,0,sizeof(vis));
        sum+=dfs(i);
    }
    printf("%d",sum);
}

 

转载于:https://www.cnblogs.com/Lis-/p/9501691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值