来源poj2226
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.
有点像小行星,但是它值能覆盖连续的泥池,所以要构图,将横着放的板子和纵放的板子编号,然后连起来算最小点覆盖
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1017;
int pre[N];
int visit[N],line[N][N],line1[N][N],line2[N][N];
char Map[N][N];
int n,m,y,x;
int cas,cas1;
bool find(int x)
{
rep(i,1,cas1+1)
{
if(line[x][i]&&visit[i]==0)
{
visit[i]=1;
if(pre[i]==0||find(pre[i]))
{
pre[i]=x;
return true;
}
}
}
return false;
}
void deal()
{
cas=0;
rep(i,1,n+1)
{
rep(j,1,m+1)
{
if(Map[i][j]=='*')
{
cas++;
while(j<=m&&Map[i][j]=='*')
{
line1[i][j]=cas;
j++;
}
}
}
}
cas1=0;
rep(j,1,m+1)
{
rep(i,1,n+1)
{
if(Map[i][j]=='*')
{
cas1++;
while(i<=n&&Map[i][j]=='*')
{
line2[i][j]=cas1;
i++;
}
}
}
}
rep(i,1,n+1)
rep(j,1,m+1)
if(Map[i][j]=='*')
line[line1[i][j]][line2[i][j]]=1;
}
int main()
{
while(~sf("%d%d",&n,&m))
{
mm(Map,'0');
mm(line,0);
mm(line2,0);
mm(line1,0);
mm(pre,0);
for(int i = 1; i <=n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>Map[i][j];
}
}
deal();
int ans=0;
rep(i,1,cas+1)
{
mm(visit,0);
if(find(i))
ans++;
}
pf("%d
",ans);
}
return 0;
}