Poj 2226 Muddy Fields【二分匹配】

来源:互联网 发布:nginx 分布式缓存原理 编辑:程序博客网 时间:2024/05/23 16:52

Muddy Fields

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 9825

 

Accepted: 3652

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,长度不限的木板去覆盖所有的泥土地。但是这里有个限制条件,木板不能覆盖草地,问最少使用多少木板。


思路:二分图---最小点覆盖问题的转化。其实在这个时候说最小点覆盖,大家可能一时摸不到头脑,我们不急着直接得出结论,我们一步一步来解决这个问题。


1、对于某一个格子上的泥土地来讲,要么这个地方被横向的木板所覆盖,要么被纵向的木板所覆盖,总之,它一定不会被其他方式的木板所覆盖。


2、我们对于一片连在一起的横向泥土地连起来标上同一个标号,表示如果全部用横向的木板来覆盖泥土对应的第几块木板编号,比如样例中:

1020033344400050


3、我们再对于一片连在一起的纵向泥土地连起来标上同一个标号,表示如果全部用纵向的木板来覆盖泥土对应的第几块木板编号,比如样例中:
1040034023450040

4、然后我们再回归刚刚的思路,对于某一个点,这个地方要么被横向的木板所覆盖,要么被纵向的木板所覆盖。那么就是说,这个点要么属于横向木板集合,要么属于纵向木板集合,也就是说,这个点符合二分图中的某个集合中的点,那么我们不难继续向下去想,如果这个点是草地,不去处理,如果是泥土地,那么这个泥土地一定属于某一种木板所覆盖的点。

5、而我们要处理的是木板个数,并不需要管一个格子到底属于哪一种木板覆盖。那么这个时候也就能理解了:我们将横向木板看成一个点,将纵向木板也看成一个点,其如果有重叠的部分,那么这个地方就是可以用来讨论到底用哪一种木板来覆盖的地方,这个时候我们将重叠部分作为边,连接两个点,构建成一个图,例如样例:



1--1

2--4

3--3

3--4

3--5

4--2

4--3

4--4

即这个时候,进行一次最大二分匹配求一下最小路径覆盖即可。


AC代码:

#include<stdio.h>#include<string.h>#include<vector>using namespace std;vector<int >mp[25000];int n,m,rowcont;char a[155][155];int row[155][155];int col[155][155];int match[25000];int vis[25000];void Getmap(){    int cont=1;    for(int i=0; i<n; i++)    {        for(int j=0; j<m; j++)        {            if(a[i][j]=='.')            {                if(a[i][j-1]=='*')cont++;                row[i][j]=0;            }            if(a[i][j]=='*')            {                row[i][j]=cont;            }        }        if(a[i][m-1]=='*')cont++;    }    rowcont=cont;    cont=1;    for(int i=0;i<m;i++)    {        for(int j=0;j<n;j++)        {            if(a[j][i]=='.')            {                if(a[j-1][i]=='*')cont++;                col[j][i]=0;            }            if(a[j][i]=='*')            {                col[j][i]=cont;            }        }        if(a[n-1][i]=='*')cont++;    }    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            if(row[i][j]==0)continue;            mp[row[i][j]].push_back(col[i][j]);        }    }}int find(int u){    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        if(vis[v]==0)        {            vis[v]=1;            if(match[v]==-1||find(match[v]))            {                match[v]=u;                return 1;            }        }    }    return 0;}int main(){    while(~scanf("%d%d",&n,&m))    {        memset(match,-1,sizeof(match));        for(int i=0; i<n; i++)        {            scanf("%s",a[i]);        }        Getmap();        int output=0;        for(int i=1;i<=rowcont;i++)        {            memset(vis,0,sizeof(vis));            if(find(i))output++;        }        printf("%d\n",output);    }}/*4 4*.*..*.***....*.*/



0 0
原创粉丝点击