POJ 2226

Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9033 Accepted: 3355

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. 
        这道题是一道二分图的题,但是你要考虑如果不用二分图做会发生什么,题目要求用木板覆盖水洼,但是是x,y轴的完全覆盖,所以如果用搜索(一般会这么考虑),会出现大量的重复时间,这样就可能会导致超时。所以使用二分图来简化步骤
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
struct ads{
    int x;
    int y;
}dres[60][60];
char dot[60][60];
int ground[2000][2000];
int L[2000],flag[2000];
int n,m,a,b;
int dfs(int X){
    for(int i=0;i<b;i++){
        if(flag[i]==0&&ground[X][i]==1){
            flag[i]=1;
            if(L[i]==-1||dfs(L[i])){
                L[i]=X;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(ground,0,sizeof(ground));

        for(int i=0;i<n;i++)
            scanf("%s",dot[i]);

        a=b=0;
        for(int i=0;i<n;i++){
            int j=0;
            while(j<m){
                while(dot[i][j]=='.'&&j<m) j++;
                while(dot[i][j]=='*'&&j<m){
                    dres[i][j].x=a;j++;
                }
                j++;a++;
            }
        }
         for(int j=0;j<m;j++){
            int i=0;
            while(i<n){
                while(dot[i][j]=='.'&&i<n) i++;
                while(dot[i][j]=='*'&&i<n){
                    dres[i][j].y=b;i++;
                }
                b++;i++;
            }
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
            if(dot[i][j]=='*') ground[dres[i][j].x][dres[i][j].y]=1;
        }

        memset(L,-1,sizeof(L));
        int num=0;
        for(int i=0;i<a;i++){
            memset(flag,0,sizeof(flag));
            if(dfs(i)) num++;
        }
        printf("%d\n",num);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值