zoukankan      html  css  js  c++  java
  • POJ 2226

    Muddy Fields
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7557   Accepted: 2791

    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

     
    每个点所在的行连通块对点所在的列连通快连一条边,求二分图最大匹配即可。
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <vector>
     6 
     7 using namespace std;
     8 
     9 #define maxn 55
    10 #define maxnode 2505
    11 
    12 char s[maxn][maxn];
    13 int un,vn,r,c;
    14 int match[maxnode];
    15 bool vis[maxnode];
    16 vector<int> G[maxnode];
    17 int row[maxn][maxn],col[maxn][maxn];
    18 
    19 bool dfs(int u) {
    20         for(int i = 0; i < G[u].size(); ++i) {
    21                 int v = G[u][i];
    22                 if(vis[v]) continue;
    23                 vis[v] = 1;
    24                 if(match[v] == -1 || dfs(match[v])) {
    25                         match[v] = u;
    26                         return true;
    27 
    28                 }
    29         }
    30 
    31         return false;
    32 }
    33 
    34 void build() {
    35         un = vn = 0;
    36 
    37         for(int i = 1; i <= r; ++i) {
    38                 for(int j = 0; j < c; ++j) {
    39                         if(s[i][j] == '*') {
    40                                 row[i][j] = s[i][j - 1] == '*' ?
    41                                             row[i][j - 1] : ++un;
    42                         }
    43 
    44                 }
    45         }
    46 
    47         for(int i = 0; i < c; ++i) {
    48                 for(int j = 1; j <= r; ++j) {
    49                         if(s[j][i] == '*') {
    50                                     col[j][i] = s[j - 1][i] == '*' ?
    51                                                 col[j - 1][i] : ++vn;
    52                         }
    53                 }
    54         }
    55 
    56         for(int i = 1; i <= r; ++i) {
    57                 for(int j = 0; j < c; ++j) {
    58                         if(s[i][j] == '*') {
    59                                // printf("%d %d
    ",row[i][j],col[i][j]);
    60                                 G[ row[i][j] ].push_back(col[i][j]);
    61                         }
    62                 }
    63         }
    64 
    65 
    66 }
    67 
    68 void solve() {
    69 
    70         build();
    71 
    72         int ans = 0;
    73 
    74         for(int i = 1; i <= vn; ++i) match[i] = -1;
    75 
    76         for(int i = 1; i <= un; ++i) {
    77                 memset(vis,0,sizeof(vis));
    78                 if(dfs(i)) ++ans;
    79         }
    80 
    81         printf("%d
    ",ans);
    82 }
    83 
    84 int main() {
    85         freopen("sw.in","r",stdin);
    86 
    87         scanf("%d%d",&r,&c);
    88 
    89         for(int i = 1; i <= r; ++i) {
    90                 scanf("%s",s[i]);
    91                 //puts(s[i]);
    92         }
    93 
    94         solve();
    95 
    96         return 0;
    97 }
    View Code
  • 相关阅读:
    .NET的URL重写
    基于Bootstrap+jQuery.validate Form表单验证实践
    JS正则表达式验证数字非常全
    Windows 系统下设置Nodejs NPM全局路径
    PHP计划任务:如何使用Linux的Crontab执行PHP脚本(转)
    linux使用crontab实现PHP执行定时任务(转)
    phpstorm 设置
    phpdoctor 安装,配置,生成文档
    phpQuery—基于jQuery的PHP实现(转)
    将C#文档注释生成.chm帮助文档(转)
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3608689.html
Copyright © 2011-2022 走看看