POJ 2226

来源:互联网 发布:战地4网络不好会掉帧吗 编辑:程序博客网 时间:2024/05/23 17:14
Muddy Fields
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7260 Accepted: 2692

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

有一个方格的草地,一些草地变成了泥坑,现在要用宽度为1,长任意的木板覆盖所有泥坑。如果只是覆盖,那么以行列为两部建二分图求最小点覆盖即可。但是还有一个要求,就是不能覆盖掉草地,那么建二分图的时候就需要对行列拆点。考虑如下一行
***....***
两端是*,中间是点,对这样建图的时候我们要拆为两个点,一个表示前面连续的*,一个表示后面连续的*。即建图的时候把每行每列连续的泥坑拆一个点,不然会覆盖草地,这样再建立二分图求最小点覆盖。

#include<cstdio>#include<cstring>#define maxn 509using namespace std;int n,m;int a[maxn][maxn],cx[maxn],cy[maxn],vis[maxn];char s[maxn][maxn];int id[maxn][maxn],idd[maxn][maxn];int path(int u){    int v;    for(int v=1;v<=m;v++)    {        if(a[u][v]&&!vis[v])        {            vis[v]=1;            if(cy[v]==-1||path(cy[v]))            {                cy[v]=u;                cx[u]=v;                return 1;            }        }    }    return 0;}int match(){    int res=0;    memset(cx,-1,sizeof(cx));    memset(cy,-1,sizeof(cy));    for(int i=1;i<=n;i++)    {        if(cx[i]==-1)        {            memset(vis,0,sizeof(vis));            res+=path(i);        }    }    return res;}int main(){    scanf("%d%d",&n,&m);    int n1=1,n2=1;    for(int i=0;i<n;i++)        scanf("%s",s[i]);    for(int i=0;i<n;i++)    {        int j=0;        while(j<m)        {            while(s[i][j]=='.'&&j<m)j++;            int cnt=0;            while(s[i][j]=='*'&&j<m)j++,cnt++;            if(cnt)            {                for(int k=j-1;k>j-1-cnt;k--)                    id[i][k]=n1;            }            n1++;        }    }    for(int i=0;i<m;i++)    {        int j=0;        while(j<n)        {            while(j<n&&s[j][i]=='.')j++;            int cnt=0;            while(j<n&&s[j][i]=='*')cnt++,j++;            if(cnt)            {                for(int k=j-1;k>j-1-cnt;k--)                    idd[k][i]=n2;            }            n2++;        }    }    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)        if(s[i][j]=='*')        a[id[i][j]][idd[i][j]]=1;    n=n1-1,m=n2-1;    printf("%d",match());}



原创粉丝点击