Muddy Fields(POJ 2226)

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.

我似乎发现了一种二分图匹配的特征,即使你看起来再不像二分图,但凡提到最小覆盖,你就要考虑二分图(我第一眼看上去是暴搜……)
题解:
  1. 我们把横着覆盖可以连续的木板记录次序(cnx),放在二分图的一边
  2. 再把竖着覆盖可以连续的木板记录次序(cny),放在二分图的另一边

我们记录的cnx,cny其实是只横覆盖只竖覆盖所需的木板数,我们通过求出 最大匹配,就可以得出 最小点覆盖(板覆盖)

 

code

#include<stdio.h> 
#include<string.h>
#include<algorithm> 
using namespace std;
const int N=110,M=110,MX=1010;
struct node {
    int x,y;
}a[N][M];
struct Edge {
    int to,next;
}edge[MX];
char mp[N][M];
bool vis[MX];
int n,m,cnt,ans,cnx=1,cny=1;
int first[MX],match[MX];
void add(int from,int to) {
    edge[++cnt].to=to; 
    edge[cnt].next=first[from];
    first[from]=cnt;
}

bool hungry(int x) {
    for(int i=first[x];i;i=edge[i].next) {
        int to=edge[i].to;
        if(vis[to]) continue;
        vis[to]=1;
        if(!match[to] || hungry(match[to])) {
            match[to]=x;
            return 1;
        }
    }
    return 0;
}

int main() 
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i) 
        scanf("%s",mp[i]+1); 
    for(int i=1;i<=n;++i) {
        int j=1;
        while(j<=m) {
            while(j<=m && mp[i][j]=='.') ++j;
            if(j>m) break;
            while(j<=m && mp[i][j]=='*') {
                a[i][j].x=cnx;
                ++j;
            }
            cnx++;
        }
    }
    for(int j=1;j<=m;++j) {
        int i=1;
        while(i<=n) {
            while(i<=n && mp[i][j]=='.') ++i;
            if(i>n) break;
            while(i<=n && mp[i][j]=='*') {
                a[i][j].y=cny;
                ++i;
            }
            cny++;
        }
    }
    for(int i=1;i<=n;++i) 
        for(int j=1;j<=m;++j) 
            if(mp[i][j]=='*') {
                add(a[i][j].x,a[i][j].y);
            }
    for(int i=1;i<=cnx;++i) {
        memset(vis,0,sizeof(vis));
        ans+=hungry(i);
    }
    printf("%d",ans);
    return 0;
}
/*
4 4
*.*.
.***
***.
..*.
*/

 

posted @ 2018-10-11 11:39  qseer  阅读(150)  评论(0编辑  收藏  举报