【二分图】poj 2226 Muddy Fields

Muddy Fields
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 10554
Accepted: 3925

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.



每一个横向或纵向的*区域都可以看做是一个联通块
那么只要把横向的连通块放在左集合,把纵向的连通块放在右集合 把点看左边
那么只要所有的边被覆盖则就是覆盖掉所有的*了
这样想来因为左右集合放的就是所求的连通块,那么只要求出最小点覆盖集即ok啦
最小点覆盖集 = 二分图最大匹配


///AC代码
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 100005
#define PI acos(-1.0)
using namespace std;
typedef long long LL;
const int N = 302;
/*变种1:二分图的最小顶点覆盖:
假如选了一个点就相当于覆盖了以它为端点的所有边,
你需要选择最少的点来覆盖所有的边
二分图的最小顶点覆盖数 = 二分图的最大匹配数


变种2:DAG图(无回路有向图)的最小路径覆盖
路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,
且任何一个顶点有且只有一条路径与之关联,
如果把这些路径中的每条路径从它的起始点走到它的终点,
那么恰好可以经过图中的每个顶点一次且仅一次
DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)


变种3: 二分图的最大独立集:在图中选取最多的点,使任意所选两点均不相连
二分图的最大独立集数 = 节点数(n)- 最大匹配数(m)
*/


/*=***************************************************
二分图匹配(匈牙利算法的DFS实现)
INIT:g[][]两边定点划分的情况
CALL:res=hungary();输出最大匹配数
优点:适于稠密图,DFS找增广路快,实现简洁易于理解
时间复杂度:O(VE);
***************************************************=*/
const int MAXN = 1000;
int uN, vN; //u,v数目
int g[MAXN][MAXN];//编号是0~n-1的
int real[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int n;

bool dfs(int u)
{
    int v;
    for (v = 1; v <= vN; v++)
        if (g[u][v] && !used[v])
        {
            used[v] = true;
            if (linker[v] == -1 || dfs(linker[v]))
            {
                linker[v] = u;
                return true;
            }
        }
    return false;
}
int hungary()
{
    int res = 0;
    int u;
    memset(linker, -1, sizeof(linker));
    for (u = 1; u <= uN; u++)
    {
        memset(used, 0, sizeof(used));
        if (dfs(u))
        {
            res++;
        }
    }
    return res;
}

char mat[1010][1010];
int vis1[1010][1010], vis2[1010][1010];
int main()
{
    int m;
    for (int i = 0; i <= 50; i++)
    {
        mat[0][i] = mat[i][0] = '.';
    }
    while (EOF != scanf("%d %d", &n, &m))
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                scanf("\n%c", &mat[i][j]);
            }
        }
        int tot1 = 1;
        int tot2 = 1;
        memset(vis1, 0, sizeof(vis1));
        memset(vis2, 0, sizeof(vis2));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (mat[i][j] == '*')
                {
                    if (!vis1[i][j - 1])
                    {
                        vis1[i][j] = tot1++;
                    }
                    else
                    {
                        vis1[i][j] = vis1[i][j - 1];
                    }
                    if (!vis2[i - 1][j])
                    {
                        vis2[i][j] = tot2++;
                    }
                    else
                    {
                        vis2[i][j] = vis2[i - 1][j];
                    }
                }
            }
        }
        memset(g, 0, sizeof(g));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (mat[i][j] == '*')
                {
                    g[vis1[i][j]][vis2[i][j]] = 1;
                }
            }
        }
        uN = max(tot1, tot2);
        vN = max(tot1, tot2);
        printf("%d\n", hungary());
    }
    return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值