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

应为经典 所以不必讲解,的一个人细品

我希望以后再看一次

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define maxn 1000
#define INF 99999999


int Mark[maxn*3];
bool Flag[maxn*3];
bool Map[maxn*3][maxn*3];
char str[maxn/2][maxn/2];
int Map1[maxn/2][maxn/2];
int Map2[maxn/2][maxn/2];


bool Find(int x,int n)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(Flag[i]||!Map[x][i])
            continue;
        Flag[i]=true;
        if(!Mark[i]||Find(Mark[i],n))
        {
            Mark[i]=x;
            return true;
        }
    }
    return false;
}


int main()
{
    int n,m;
    int i,j;
    char c;
    int t1,t2;
    int x,y;
    int Count;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                cin>>c;
                str[i][j]=c;
                str[i][0]='.';
                str[0][j]='.';
            }
        memset(Map1,0,sizeof(Map1));
        memset(Map2,0,sizeof(Map2));
        t1=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(str[i][j]=='*')
                {
                    if(str[i][j-1]=='*')
                        Map1[i][j]=t1;
                    else
                        Map1[i][j]=++t1;
                }
            }
        }
        t2=0;
        for(j=1;j<=m;j++)
            for(i=1;i<=n;i++)
            {
                if(str[i][j]=='*')
                {
                    if(str[i-1][j]=='*')
                        Map2[i][j]=t2;
                   else
                        Map2[i][j]=++t2;
                }
            }
        memset(Map,false,sizeof(Map));
        memset(Mark,0,sizeof(Mark));
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                if(str[i][j]=='*')
                {
                    x=Map1[i][j];
                    y=Map2[i][j];
                    Map[x][y]=true;
                }
            }
        Count=0;
        for(i=1;i<=t1;i++)
        {
            memset(Flag,false,sizeof(Flag));
            if(Find(i,t2))
                Count++;
        }
        printf("%d\n",Count);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值