POJ2226*行列模型,二分图最小顶点覆盖

Muddy Fields

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,长度任意的若干块木板覆盖所有的水,并不能覆盖草,木板可以交叉,但只能横竖放置,问最少要多少块板。(*代表湿地,.代表草地)

分析:一开始以为这一道题目和poj3041差不多,于是直接交了一发,emmmm,哈哈哈,果断WA,原来这一道题目和那一道是有区别的,区别在于这一道题目只能处理连续的区域,也就是连通区域,中间是不能有草地的,比如下面这一组数据,如果是直接去求的话,错误的答案是3,但正确是答案应该是4

***

*.*

***

,那么这一道题目就需要重新去建立这个二分图了。

这个二分图主要是有两部分组成的,先每一行去遍历处理,将每一行中练出的区域分别独立的标上号,那么就能得到一块块每行连续的区域了,同理,每一列也是这样处理的。

那么这就可以每一行每一列的处理去二分匹配了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<deque>
#include<cstring>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,m,k;
char str[100][100];
int e[2000][2000],r[100][100],c[100][100];
int sy[2000];
bool vis[2000];
int dfs(int u){
    rep(i,1,m){
        if(!vis[i]&&e[u][i]){
            vis[i]=1;
            if(!sy[i]||dfs(sy[i]))
            {
                sy[i]=u;
                return 1;
            }
        }
    }
    return 0;
}

int solve(){
    int ans=0;
    memset(sy,0,sizeof sy);
    rep(i,1,n){
        memset(vis,0,sizeof vis);
        ans+=dfs(i);
    }
    return ans;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    scanf("%d%d",&n,&m);
    rep(i,1,n){
        scanf("%s",str[i]+1);
    }
    int rcnt=0,ccnt=0;
    rep(i,1,n){
        rep(j,1,m){
            if(str[i][j]=='*'){
                rcnt++;
                while(j<=m&&str[i][j]=='*')
                r[i][j++]=rcnt;
                j--;
            }
        }
    }
    rep(j,1,m){
        rep(i,1,n){
            if(str[i][j]=='*'){
                ccnt++;
                while(i<=n&&str[i][j]=='*')
                    c[i++][j]=ccnt;
                i--;
            }
        }
    }
    rep(i,1,n){
        rep(j,1,m){
            if(str[i][j]=='*')
                e[r[i][j]][c[i][j]]=1;
        }
    }
    rep(i,1,n){
        rep(j,1,m)
        printf("%d ",r[i][j]);
        printf("\n");
    }
    printf("\n");
    rep(i,1,n){
        rep(j,1,m)
        printf("%d ",c[i][j]);
        printf("\n");
    }
    printf("\n");
    n=rcnt;
    m=ccnt;
    rep(i,1,n){
        rep(j,1,m){
            printf("%d ",e[i][j]);
        }
        printf("\n");
    }
    printf("%d\n",solve());
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值