(最小点覆盖) poj 2226

本文介绍了一种解决牧场中泥泞区域覆盖问题的算法。通过建立一个高效的图模型,利用匹配算法来确定最少数量的木板,以覆盖所有泥泞区域,同时允许奶牛正常觅食。文章详细阐述了算法实现过程及示例。

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

H - Muddy Fields
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2226

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.

 

 建图是关键
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
char s[55][55];
int n,m,t1,t2,g1[55][55],g2[55][55],mp[1010][1010],ans,link[1010],mark[1010];
void init()
{
    t1=0,t2=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            while(s[i][j]=='*')
            {
                g1[i][j]=t1;
                j++;
            }
            t1++;
        }
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            while(s[j][i]=='*')
            {
                g2[j][i]=t2;
                j++;
            }
            t2++;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(s[i][j]=='*')
            {
                mp[g1[i][j]][g2[i][j]]=1;
            }
        }
    }
}
bool dfs(int x)
{
    for(int i=0;i<t2;i++)
    {
        if(mark[i]==-1&&mp[x][i])
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        memset(link,-1,sizeof(link));
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        init();
        for(int i=0;i<t1;i++)
        {
            memset(mark,-1,sizeof(mark));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/water-full/p/4460953.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值