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.
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.
* 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.
木板只能放在行或列的泥地联通块里。
把行、列连通块看成左、右部的点,每个泥地对他所在的行、列连通块连一条边。
然后求最小覆盖。
#include<cstdio>
#include<cstring>
#define M(a) memset(a,0,sizeof(a))
int first[70000],next[70000],to[70000],r,c,m,n,num1[60][60],num2[60][60],f[70000];
bool map[60][60],v[70000];
bool dfs(int p)
{
int i,j,k,x,y,z;
for (i=first[p];i;i=next[i])
if (!v[to[i]])
{
v[to[i]]=1;
if (!f[to[i]]||dfs(f[to[i]]))
{
f[to[i]]=p;
return 1;
}
}
return 0;
}
int main()
{
int i,j,k,x,y,z,ans;
bool con;
char s[60];
scanf("%d%d",&c,&r);
for (i=1;i<=c;i++)
{
scanf("%s",s+1);
for (j=1;j<=r;j++)
map[i][j]=(s[j]=='*');
}
m=0;
for (i=1;i<=c;i++)
{
con=0;
for (j=1;j<=r;j++)
if (!map[i][j])
con=0;
else
{
if (con) num1[i][j]=m;
else
{
num1[i][j]=++m;
con=1;
}
}
}
for (j=1;j<=r;j++)
{
con=0;
for (i=1;i<=c;i++)
if (!map[i][j])
con=0;
else
{
if (con) num2[i][j]=m;
else
{
num2[i][j]=++m;
con=1;
}
}
}
n=0;
for (i=1;i<=c;i++)
for (j=1;j<=r;j++)
if (map[i][j])
{
x=num1[i][j];
y=num2[i][j];
n++;
next[n]=first[x];
to[n]=y;
first[x]=n;
}
ans=0;
for (i=1;i<=m;i++)
{
M(v);
if (dfs(i)) ans++;
}
printf("%d\n",ans);
}