poj 2226 Muddy Fields (二分图)

本文探讨了一种通过放置木板来覆盖泥泞区域的方法,旨在寻找覆盖所有泥泞区域所需的最少木板数量。该问题转化为求解二分图的最大匹配数,利用匈牙利算法实现了高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

转载于:https://www.cnblogs.com/w0w0/archive/2012/06/07/2540019.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值