Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7340   Accepted: 2715

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

 //9008K    63MS    C++    1570B    2013-11-10 20:07:57
/* (网摘思路)
题意:给定一个矩阵,其中有一些地方有水,用一些长度任意,宽度为1的木板盖住这些有水的地方,不能挡住草地,木板可覆盖,问至少需要几块板子。 分析:二分图最小点集覆盖。二分图建图方法如下,把每段连续的横向的水洼看成是一个X集合中的点,每段连续的纵向的水洼看成是Y集合中的点。
矩阵每个有水单元看成是一个边,它连接了它所在的横向水洼在X集合中对应的点和它所在的纵向水洼在Y集合中对应的点。(对于一段连续的水洼,
如果要铺木板,一定要铺得尽量长)这样最小点集覆盖的意义就变成了,矩阵中的每个有水的单元(二分图中的每条边)所在的横向和纵向的水洼
(在X集合和Y集合中的两个端点)至少有一个被覆盖。求至少需要选几个点。 */
#include<stdio.h>
#include<string.h>
struct node{
int x;
int y;
}edge[][];
char c[][];
int g[][];
int vis[];
int match[];
int n,m,N,M;
void build() //建图
{
memset(g,,sizeof(g));
N=M=;
for(int i=;i<n;i++) //处理行,作二分图的一集合
for(int j=;j<m;j++){
while(c[i][j]=='.' && j<m) j++;
while(c[i][j]=='*' && j<m){
edge[i][j].x=N;
j++;
}
N++;
}
for(int j=;j<m;j++) //处理列,作二分图的另一集合
for(int i=;i<n;i++){
while(c[i][j]=='.' && i<n) i++;
while(c[i][j]=='*' && i<n){
edge[i][j].y=M;
i++;
}
M++;
}
for(int i=;i<n;i++) //建图
for(int j=;j<m;j++)
if(c[i][j]=='*')
g[edge[i][j].x][edge[i][j].y]=;
}
int dfs(int x)
{
for(int i=;i<M;i++){
if(!vis[i] && g[x][i]){
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
}
int hungary() //匈牙利算法
{
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<N;i++){
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
return ans;
}
int main(void)
{
while(scanf("%d%d%*c",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
scanf("%s",c[i]);
build();
printf("%d\n",hungary());
}
return ;
}

poj 2226 Muddy Fields (二分匹配)的更多相关文章

  1. POJ 2226 Muddy Fields(最小顶点覆盖)

    POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...

  2. poj 2226 Muddy Fields (转化成二分图的最小覆盖)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  3. poj 2226 Muddy Fields(最小覆盖点+构图)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  4. poj 2226 Muddy Fields(最小点覆盖+巧妙构图)

      Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= ...

  5. POJ 2226 Muddy Fields(二分匹配 巧妙的建图)

    Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R ...

  6. poj 2226 Muddy Fields(合理建图+二分匹配)

    /* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...

  7. POJ 2226 Muddy Fields (二分图匹配)

    [题目链接] http://poj.org/problem?id=2226 [题目大意] 给出一张图,上面有泥和草地,有泥的地方需要用1*k的木板覆盖, 有草地的地方不希望被覆盖,问在此条件下需要的最 ...

  8. [POJ] 2226 Muddy Fields(二分图最小点覆盖)

    题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

  9. poj 2226 Muddy Fields(水二分图)

    Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 ...

随机推荐

  1. BootStrap的动态模态框及静态模态框

    1.要用bootStrap这个框架就必须要重载它的class类,也就是说class要一样 代码如下: 有疑问的可以在下面留言,欢迎大家一起交流 1.1动态模态框 <!DOCTYPE html&g ...

  2. js-scroll判断页面是向上滚动还是向下滚动

    原理:那当前的scrollTop和之前的scrollTop对比 如果变大了,表示向下滚动(scrollTop值变大): 如果变小了,表示向上滚动(scrollTop值变小). 方法一:js代码: $( ...

  3. 解决url传递过程中加号变空格的问题

    url传递过程中加号变空格 在接收url参数的过程中,会发现如果参数中存在‘+’号,接收后会变成空格. 如11+22接收后变成11 22. 要解决这个问题,需要将加号替换为%2B进行传递. 如11%2 ...

  4. php实现redis

    <?php //实例化Redis对象 $red=new Redis(); //链接redis服务 $red->connect('localhost','6379'); //具体操作 $re ...

  5. IdeaVim插件使用技巧--JetBrains Plugin Vim

    在 IDEA Intellij小技巧和插件 一文中简单介绍了一下IdeaVim插件.在这里详细总结一下这个插件在日常编程中的一些常用小技巧.供有兴趣使用这个插件,但对Vim还不十分熟悉的朋友参考.当然 ...

  6. Awakening Your Senses【唤醒你的感觉官能】

    Awakening Your Senses Give youself a test. Which way is the wind blowing? How many kinds of wildflow ...

  7. nyoj 525 一道水题【字符串(分割)】

    参考:https://blog.csdn.net/dxx_111/article/details/48154687 #include <iostream> #include <cst ...

  8. 什么是Session共享?请举出使用场景

    是指在一个浏览器对应多个Web服务时,服务端的Session数据需要共享.例如单点登录.Web服务器集群等场景都需要用到.多子服务. Session共享有多种解决方案,例如Tomcat插件,我最喜欢的 ...

  9. 关于cookie的一些学习笔记

    0x00 发现自己对一些原理性的东西实在是太不了解 最近看了<cookie之困>记一下笔记 0x01 因为http是无状态的 所以需要cookie和session来保持http的会话状态和 ...

  10. 6.JAVA知识点归纳整理

    一.jdk初识与HelloWord: 二.java基础: 2.1 标识符_关键字_数据类型 2.2 数据类型转换 2.3 程序编写格式 2.4 运算符 2.5 分支与for循环 2.6 while_b ...