poj 2226 Muddy Fields (二分图)

时间:2021-02-07 06:14:28
Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5848   Accepted: 2166

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.
 
这题难点在建图
参考了discuss和别人的结题报告才明白
 
用到的定理:最小点覆盖数=最大匹配数
#include<iostream>
using namespace std;

char input[55][55];
int xlabel[500][500];
int ylabel[51][51];
int map[2500][2500];
int xmax, ymax;
int r, c;
int father[5000];
int mark[5000];

int dfs(int a)
{
    for(int i=1;i<ymax;i++)
    {
        if(mark[i]!=1 && map[a][i]!=0)
        {
            mark[i]=1;
            if(father[i]==0 || dfs(father[i])==1)
            {
                father[i]=a;
                return 1;
            }
        }
    }
    return 0;
}

int hung()
{
    int res=0;
    memset(father, 0, sizeof(father));
    for(int i=1;i<xmax;i++)
    {
        memset(mark, 0, sizeof(mark));
        if(bfs(i)==1)
            res++;
    }
    return res;
}

int main()
{

    freopen("e:\\data.txt", "r", stdin);   
    freopen("e:\\out.txt", "w", stdout);

    cin>>r>>c;
    for(int i=0;i<r;i++)
    {
        cin>>input[i];
    }
    memset(xlabel, 0, sizeof(xlabel));
    memset(ylabel, 0, sizeof(ylabel));
    memset(map, 0, sizeof(map));
    int num=0;
    xmax=1;ymax=1;
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            if(input[i][j]=='*')
            {
                xlabel[i][j]=xmax;
                num=0;
            }
            else
            {
                if(num==0)
                    xmax++;
                num++;
            }
        }
        if(num==0)
        {
            xmax++;
            num=-1;
        }
    }
    num=0;
    for(int j=0;j<c;j++)
    {
        for(int i=0;i<r;i++)
        {
            if(input[i][j]=='*')
            {
                ylabel[i][j]=ymax;
                num=0;
            }
            else
            {
                if(num==0)
                    ymax++;
                num++;
            }
        }
        if(num==0)
        {
            ymax++;
            num=-1;
        }
    }
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            if(input[i][j]=='*')
            {
                map[xlabel[i][j]][ylabel[i][j]]=1;
            }
        }
    }
    //for(int i=0;i<r;i++)
    //{
    //    for(int j=0;j<c;j++)
    //    {
    //        cout<<xlabel[i][j]<<" ";
    //    }
    //    cout<<endl;
    //}
    //cout<<endl;
    //for(int i=0;i<r;i++)
    //{
    //    for(int j=0;j<c;j++)
    //    {
    //        cout<<ylabel[i][j]<<" ";
    //    }
    //    cout<<endl;
    //}
    //cout<<endl;
    //for(int i=1;i<=xmax;i++)
    //{
    //    for(int j=1;j<=ymax;j++)
    //    {
    //        cout<<map[i][j]<<" ";
    //    }
    //    cout<<endl;
    //}
    //cout<<endl;
    //cout<<xmax<<" "<<ymax<<endl;
    cout<<hung()<<endl;
}