zoukankan      html  css  js  c++  java
  • POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12565   Accepted: 4651

    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 //F - Muddy Fields POJ - 2226
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 const int maxn=55;
     9 const int N=maxn*maxn/2;
    10 int G[N][N],vis[N],link[N];
    11 int n;
    12 bool Find(int u){
    13     for(int i=1;i<=n;i++){
    14         if(G[u][i]&&!vis[i]){
    15             vis[i]=1;
    16             if(link[i]==-1||Find(link[i])){
    17                 link[i]=u;
    18                 return true;
    19             }
    20         }
    21     }
    22     return false;
    23 }
    24 int solve(){
    25     int cnt=0;
    26     memset(link,-1,sizeof(link));
    27     for(int i=1;i<=n;i++){
    28         memset(vis,0,sizeof(vis));
    29         if(Find(i))cnt++;
    30     }
    31     return cnt;
    32 }
    33 char mat[maxn][maxn];
    34 int vis1[maxn][maxn],vis2[maxn][maxn];
    35 int main(){
    36     int m;
    37     for(int i=0;i<=50;i++)
    38         mat[0][i]=mat[i][0]='.';
    39     while(~scanf("%d%d",&n,&m)){
    40         for(int i=1;i<=n;i++){
    41             for(int j=1;j<=m;j++)
    42                 cin>>mat[i][j];
    43         }
    44         int tot1=1,tot2=1;
    45         memset(vis1,0,sizeof(vis1));
    46         memset(vis2,0,sizeof(vis2));
    47         for(int i=1;i<=n;i++){
    48             for(int j=1;j<=m;j++){
    49                 if(mat[i][j]=='*'){
    50                     if(!vis1[i][j-1])vis1[i][j]=tot1++;
    51                     else vis1[i][j]=vis1[i][j-1];
    52                     if(!vis2[i-1][j])vis2[i][j]=tot2++;
    53                     else vis2[i][j]=vis2[i-1][j];
    54                 }
    55             }
    56         }
    57         memset(G,0,sizeof(G));
    58         for(int i=1;i<=n;i++){
    59             for(int j=1;j<=m;j++){
    60                 if(mat[i][j]=='*')
    61                     G[vis1[i][j]][vis2[i][j]]=1;
    62             }
    63         }
    64         n=max(tot1,tot2);
    65         printf("%d
    ",solve());
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    第十二章 基本数据类型
    第十一章 变量名的力量
    第十章 使用变量的一般事项
    第九章 伪代码编程过程
    第八章 防御式编程
    JMeter简介
    第七章 高质量的子程序
    第六章 可以工作的类
    第五章 软件构建中的设计
    第四章 关键的“构建”决策
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9826265.html
Copyright © 2011-2022 走看看