POJ2226:Muddy Fields(二分图)

来源:互联网 发布:adobe系列软件介绍 编辑:程序博客网 时间:2024/04/29 07:09

Muddy Fields
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10832 Accepted: 4025

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

USACO 2005 January Gold

题意:N*M的地图,*是泥地,.是草地,有无限块宽为1的无限长的木板,需要把泥地盖住,而不能盖住草地,问所需木板的最少数目。

思路:对于每个*,横纵坐标建边,这就是一个二分图,即求最小点覆盖数=最大匹配数,但是这里要求不能盖住草地,所以同一行(或列)非连续的泥地,都视为不同行(或列),这样处理下再跑最大匹配即可。

# include <iostream># include <cstdio># include <cstring># include <vector>using namespace std;const int N = 53;int l[2600], vis[2600];char s[N][N];int p[N][N], q[N][N];vector<int>v[2600];int cnt = 0, cnt2 = 0;int dfs(int u){    for(int i=0; i<v[u].size(); ++i)    {        int j = v[u][i];        if(!vis[j])        {            vis[j] = 1;            if(l[j]==-1 || dfs(l[j]))            {                l[j] = u;                return 1;            }        }    }    return 0;}int main(){    int n, m;    while(~scanf("%d%d",&n,&m))    {        cnt = cnt2 = 0;        memset(l, -1, sizeof(l));        for(int i=1; i<=n; ++i)            scanf("%s",s[i]+1);        for(int i=1; i<=n; ++i)            for(int j=1; j<=m; ++j)                if(s[i][j] == '*')                {                    if(j-1>0&&s[i][j-1]=='*') q[i][j] = q[i][j-1];                    else q[i][j] = ++cnt2;                    if(i-1>0&&s[i-1][j]=='*') p[i][j] = p[i-1][j];                    else p[i][j] = ++cnt;                }        for(int i=0; i<=cnt2; ++i) v[i].clear();        for(int i=1; i<=n; ++i)            for(int j=1; j<=m; ++j)                if(s[i][j] == '*')                v[q[i][j]].push_back(p[i][j]);        int ans = 0;        for(int i=1; i<=cnt2; ++i)        {            memset(vis, 0, sizeof(vis));            if(dfs(i)) ++ans;        }        printf("%d\n",ans);    }    return 0;}


原创粉丝点击