Muddy Fields+POJ+二分图最大匹配

来源:互联网 发布:单片机最小系统 编辑:程序博客网 时间:2024/05/11 18:08
Muddy Fields
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8173 Accepted: 3012

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
解决方案:此题由于不能跨过草地,所以,直接套用模板就不行了。先把每行,每列的'*'连成一块,在建立二分图,求其最大匹配,最大匹配即为最小覆盖点。
code:
#include<iostream>#include<cstdio>#include<vector>#include<cstring>using namespace std;char Map[55][55];int U[55][55];int V[55][55];int Left[55*55];bool solve[55*55];vector<int >G[55*55];int R,C;int v,u;bool dfs(int u){    int len=G[u].size();    for(int i=0; i<len; i++)    {        int v=G[u][i];        if(!solve[v])        {            solve[v]=true;            if(Left[v]==-1||dfs(Left[v]))            {                Left[v]=u;                return true;            }        }    }    return false;}int main(){    while(~scanf("%d%d",&R,&C))    {        for(int i=0; i<=R*C; i++)        {            G[i].clear();        }        memset(Left,-1,sizeof(Left));        for(int i=0; i<R; i++)        {            scanf("%s",Map[i]);        }        u=v=1;        memset(U,0,sizeof(U));        memset(V,0,sizeof(V));        for(int i=0; i<R; i++)        {            for(int j=0; j<C; j++)            {                if(Map[i][j]=='*')                {                    while(j<=C&&Map[i][j]=='*')                    {                        U[i][j]=u;                        j++;                    }                    u++;                }            }        }        for(int i=0; i<C; i++)            for(int j=0; j<R; j++)            {                if(Map[j][i]=='*')                {                    while(j<=R&&Map[j][i]=='*')                    {                        V[j][i]=v;                        j++;                    }                    v++;                }            }        for(int i=0; i<R; i++)            for(int j=0; j<C; j++)            {                if(Map[i][j]=='*')                {                    G[U[i][j]].push_back(V[i][j]);                }            }        int cnt=0;        for(int i=1; i<u; i++)        {            memset(solve,false,sizeof(solve));            if(dfs(i))            {                cnt++;            }        }        printf("%d\n",cnt);    }    return 0;}

0 0
原创粉丝点击