poj2226更改行列匹配建图

解决农场中泥泞区域的问题,通过放置一系列1单位宽的木板来覆盖所有泥泞部分,同时确保每块木板平行于场地的一侧,并且不重叠草地。采用图论中的匹配算法,计算所需的最少木板数量。

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

Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10961 Accepted: 4071

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.

Source

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=58;
char mp[N][N];
int used[N*N],head[N*N],tot,n,m;
struct node
{
    int to,next;
} e[100861];
bool vis[N*N];
int aa[N][N],bb[N][N],a,b;
void add(int u,int v)
{
    e[tot].to=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
bool findx(int u)
{
    for(int i=head[u]; ~i; i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v]) continue;
        vis[v]=1;
        if(!used[v]||findx(used[v]))
        {
            used[v]=u;
            return true;
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int maxmatch=0;
        tot=0;
        a=b=1;
        memset(head,-1,sizeof(head));
        memset(aa,0,sizeof(aa));
        memset(bb,0,sizeof(bb));
        for(int i=1; i<=n; ++i) scanf("%s",mp[i]+1);
        for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j)
            {
                bool c=0;
                while(mp[i][j]=='*'&&j<=m) c=1,aa[i][j++]=a;
                if(c) ++a;
            }
        for(int i=1; i<=m; ++i) for(int j=1; j<=n; ++j)
            {
                bool c=0;
                while(mp[j][i]=='*'&&j<=n) c=1,bb[j++][i]=b;
                if(c) ++b;
            }
        for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) if(aa[i][j]) add(aa[i][j],bb[i][j]);
        memset(used,0,sizeof(used));
        for(int i=1; i<a; ++i)
        {
            memset(vis,0,sizeof(vis));
            if(findx(i)) ++maxmatch;
        }
        printf("%d\n",maxmatch);
    }
}

 

转载于:https://www.cnblogs.com/mfys/p/7614951.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值