Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9754   Accepted: 3618

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

题意:一个row*col的矩阵表示一块田地,'*'表示湿地,'.'表示草地。现在FJ要在田地上铺木板盖掉所有的湿地,露出所有的草地。每块木板的宽度为1,长度为任意长。问FJ最少用几块木板就可以完成任务?
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std; vector<int> G[5005];
int match[5005],used[5005];
int n,r,c,cntl,cntr,m;
int nel[55][55],ner[55][55]; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int w=match[v];
if(w<0||!used[w]&&dfs(w))
{
match[u]=v;
match[v]=u;
return true;
}
}
return false;
} int bipartite_match()
{
memset(match,-1,sizeof(match));
int res=0;
for(int i=1;i<=cntl+cntr;i++)
if(match[i]<0)
{
memset(used,0,sizeof(used));
if(dfs(i)) res++;
}
return res;
} char s[55][55]; void build()
{
for(int i=1;i<=cntl+cntr;i++) G[i].clear();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(s[i][j]=='*')
add_edge(nel[i][j],cntl+ner[i][j]);
} int main()
{
while(~scanf("%d %d",&n,&m))
{
cntl=0;cntr=0;
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
for(int j=1;j<=m;)
{
while(s[i][j]=='.'&&j<=m) j++;
if(j>m) break;
cntl++;
while(s[i][j]=='*')
{
nel[i][j]=cntl;
j++;
}
}
}
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;)
{
while(s[i][j]=='.'&&i<=n) i++;
if(i>n) break;
cntr++;
while(s[i][j]=='*')
{
ner[i][j]=cntr;
i++;
}
}
}
build();
printf("%d\n",bipartite_match());
}
return 0;
}

  分析:爽啊,这道题算是自己没看题解独立完成的吧,水平开始有点出来了

这道题跟以前做的那道毁灭星球的题目最大的不同就是要让矩阵中的草地都露出来,这就决定了他的

建图方式有些不太一样,建图:首先预处理出每个顶点在行中属于的标号,与在列中属于的标号,标号时对于连续的一段进行合并,这一段连续的*标号成同一个数字(这也是能进行二分匹配的原因,因为能用同一块木板),接下来建图,左边代表行标号,右边代表列标号,根据二分图中最小顶点覆盖等于最大匹配数即可。

然后

TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图的更多相关文章

  1. TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

    Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5004   Accepted: 1444 ...

  2. HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧

    题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...

  3. POJ 2226 Muddy Fields 二分图(难点在于建图)

    题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...

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

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

  5. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

  6. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. poj 1034 The dog task (二分匹配)

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2559   Accepted: 1038   Sp ...

  8. POJ 1466 大学谈恋爱 二分匹配变形 最大独立集

    Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11694   Accepted: 5230 D ...

  9. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

随机推荐

  1. SQL----Group By and Having

    合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. SQL GROUP BY 语法 ...

  2. call,apply,bind的理解

    call,apply,bind均是用于改变this指向. 三者相似之处: 1:都是用于改变函数的this指向. 2:第一个参数都是this要指向的对象. 3:都可以通过后面的参数进行对方法的传参. l ...

  3. Django框架——基础之模型系统(ORM相关操作)

    ------------恢复内容开始------------ 1.必定会的十三条! 1.1记忆方法一:(按字母顺序记忆)   <1> all(): 查询所有结果 <2> cou ...

  4. deep_learning_Function_tf.train.ExponentialMovingAverage()滑动平均

    近来看batch normalization的代码时,遇到tf.train.ExponentialMovingAverage()函数,特此记录. tf.train.ExponentialMovingA ...

  5. PAT Basic 1017 A除以B (20 分)

    本题要求计算 /,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: 输入在一行中依次给出 A 和 B,中间以 1 ...

  6. Python之路:进程、线程

    目录 一.进程与线程区别 1.1 什么是线程 1.2 什么是进程 1.3 进程与线程的区别 二.Python GIL全局解释器锁 三.线程 3.1 threading模块 3.2 Join & ...

  7. monkeyrunner脚本录制和回放下载

    链接:https://pan.baidu.com/s/1Kye_E9u_WXeppFMlLhr_Cg 提取码:2coy

  8. PropertyPlaceholderConfigurer implements BeanFactoryPostProcessor

    BeanFactoryPostProcessor的应用 最常用的一个应用就是org.springframework.beans.factory.config.PropertyPlaceholderCo ...

  9. py实现ftp

    https://www.cnblogs.com/wangziyi0513/p/11077323.html 参考原始代码: 修改了一下: 许多网友问中文路径乱码怎么办,我觉得应该讲中文路径转码后再发送. ...

  10. poj3666/CF714E/hdu5256/BZOJ1367(???) Making the Grade[线性DP+离散化]

    给个$n<=2000$长度数列,可以把每个数改为另一个数代价是两数之差的绝对值.求把它改为单调不增or不减序列最小代价. 话说这题其实是一个结论题..找到结论应该就很好做了呢. 手玩的时候就有感 ...