【POJ】2226Muddy Fields(二分图最小覆盖,建图思想)

Language:Default

Muddy Fields

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13375 Accepted: 4918

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.

 

题目大意:现在给你一个N*M的图,上面的是一块块的地,图上的*代表的是泥地,.代表的是正常的草地,然后现在我们相用一些木板把这些泥地封起来,每一个木板是宽度为1长度任意的,然后有一个条件就是一个木板是不能够覆盖草地的,

最后问你最少多少木板能够把泥地都给覆盖掉,

思路:这个题目的建图思想确实是比较巧妙的,

我们首先考虑到每一个格子里面的泥地,这个泥地要么被横着的木板覆盖,要么就是被竖着的木板覆盖,那么这个题目的基本建图思想就可以找到了,

我们把所有的泥地(也就是泥地联通块),给分为两类,一类是横着的,一类是竖着的,这里不要误解一个泥块既可以属于一个竖着的泥地,也可以属于横着的泥地,我们把所有的泥地都编号,对某一个泥块的行泥地与其列泥地建边,这样子就是我们的那个要么被横着的木板覆盖,要么就是被竖着的木板覆盖然后正常跑一边最大匹配就行了,因为最大匹配=最小点覆盖

说的感觉不大清楚。。。。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn=50+10;
char mp[maxn][maxn];
int pre[maxn*maxn],vis[maxn*maxn];
int pos[maxn][maxn];
int n,m;
vector<int >G[maxn*maxn];
bool DFS(int u)
{
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(vis[v]) continue;
        vis[v]=1;
        if(pre[v]==0||DFS(pre[v]))
        {
            pre[v]=u;
            return true;
        }
    }
    return false;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%s",mp[i]+1);
    int idx=0,idl=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(mp[i][j]=='*')
            {
                pos[i][j]=++idx;
                for(;j<=m;j++)
                    if(mp[i][j]=='*')
                        pos[i][j]=idx;
                    else
                        break;
            }
    for(int j=1;j<=m;j++)
    {
        for(int i=1;i<=n;i++)
        {
            if(mp[i][j]=='*')
            {
                int id=pos[i][j];
                G[id].push_back(++idl);
                for(;i<=n;i++)
                    if(mp[i][j]=='*')
                        G[pos[i][j]].push_back(idl);
                    else
                        break;
            }
        }
    }
    int ans=0;
    for(int i=1;i<=idx;i++)
    {
        memset(vis,0,sizeof(vis));
        if(DFS(i)) ans++;
    }
    printf("%d\n",ans);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值