码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2226

时间:2019-09-01 16:40:15      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:cep   二分   cli   field   input   needed   esc   code   href   

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13386   Accepted: 4923

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.

Source

 
将 * 分别按行和列分组连边
跑个二分图就行
技术图片
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 55;

char mp[maxn][maxn];

bool vis[maxn*maxn],G[maxn*maxn][maxn*maxn];

int id1[maxn][maxn],id2[maxn][maxn],tt1,tt2,g[maxn*maxn],n,m,tt;

bool dfs(int u){
    for(int v=1;v<=tt;v++){
        if(!G[u][v]) continue;
        if(!vis[v]){
            vis[v]=1;
            if(!g[v]||dfs(g[v])){
                g[v]=u;
                return 1;
             }
        }
    }
    return 0;
}

void debug(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) printf("%c ",mp[i][j]);
        puts("");
    }
       
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
//    debug();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(mp[i][j]!=*) continue;
            if(!id1[i-1][j]) id1[i][j]=++tt1;
            else id1[i][j]=id1[i-1][j];
            if(!id2[i][j-1]) id2[i][j]=++tt2;
            else id2[i][j]=id2[i][j-1];
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(mp[i][j]==*) G[id1[i][j]][id2[i][j]]=1;
        }
    int ans=0;
    tt=max(tt1,tt2);
    for(int i=1;i<=tt;i++){
        memset(vis,0,sizeof(vis));
        if(dfs(i)) ans++;
    }
    printf("%d\n",ans);
    return 0;
}
View Code

 

poj 2226

标签:cep   二分   cli   field   input   needed   esc   code   href   

原文地址:https://www.cnblogs.com/plysc/p/11442552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!