Muddy Fields
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11146 | Accepted: 4152 |
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.
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.
Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
..2.
Board 2 overlaps boards 3 and 4.
思路:每一个横向或纵向的*区域都可以看做是一个联通块,那么只要把横向的连通块放在左集合,把纵向的连通块放在右集合,把点看左边那么只要所有的边被覆盖则就是覆盖掉所有的*了,这样想来因为左右集合放的就是所求的连通块,只要求出最小点覆盖集就可以了。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
int mm[1500][1500];
int g[1500];
int used[1500];
char c[55][55];
int v1[55][55],v2[55][55];
int find(int x)
{
int i;
for(i=1;i<=n;i++)
{
if(mm[x][i]==1&&used[i]==0)
{
used[i]=1;
if(g[i]==0||find(g[i]))
{
g[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(mm,0,sizeof(mm));
memset(g,0,sizeof(g));
int i,j;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("\n%c",&c[i][j]);
}
}
int x=1,y=1;
memset(v1,0,sizeof(v1));
memset(v2,0,sizeof(v2));
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(c[i][j]=='*')
{
if(!v1[i][j-1])
{
v1[i][j]=x++;
}
else v1[i][j]=v1[i][j-1];
if(!v2[i-1][j])
{
v2[i][j]=y++;
}
else v2[i][j]=v2[i-1][j];
}
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(c[i][j]=='*')
{
mm[v1[i][j]][v2[i][j]]=1;
}
}
}
n=max(x,y);
int sum=0;
for(i=1;i<=n;i++)
{
memset(used,0,sizeof(used));
if(find(i))sum++;
}
printf("%d\n",sum);
}
return 0;
}