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 : Two space-separated integers: R and C 

* Lines ..R+: Each line contains a string of C characters, with '*' representing a muddy patch, and '.' representing a grassy patch. No spaces are present.

Output

* Line : A single integer representing the number of boards FJ needs.

Sample Input

*.*.
.***
***.
..*.

Sample Output


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;
#define N 560
int n,m; char mp[N][N];
int cnt[N][N];
int cnt1[N][N];
int fina[N][N];
int match[N];
int vis[N];
int tmp,tmp1;
bool dfs(int x){
for(int i=;i<=tmp1;i++){
if(!vis[i] && fina[x][i]) {
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return true;
}
}
}
return false;
}
void solve(){
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<=tmp;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
}
printf("%d\n",ans); }
int main()
{
while(scanf("%d%d",&n,&m)==){
memset(mp,,sizeof(mp));
for(int i=;i<n;i++){
scanf("%s",mp[i]);
}
memset(cnt,-,sizeof(cnt));
memset(cnt1,-,sizeof(cnt1));
tmp=;
for(int i=;i<n;i++){
int flag=;
for(int j=;j<m;j++){ if(flag== && mp[i][j]=='*'){
flag=;
cnt[i][j]=++tmp;
}
else if(flag== && mp[i][j]=='*'){
cnt[i][j]=tmp;
}
else if(mp[i][j]=='.'){
flag=;
}
}
} tmp1=;
for(int j=;j<m;j++){
int flag=;
for(int i=;i<n;i++){
if(flag== && mp[i][j]=='*'){
flag=;
cnt1[i][j]=++tmp1;
}
else if(flag== && mp[i][j]=='*'){
cnt1[i][j]=tmp1;
}
else if(mp[i][j]=='.'){
flag=;
}
}
}
memset(fina,,sizeof(fina));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(cnt[i][j]!=- && cnt1[i][j]!=-){
fina[cnt[i][j]][cnt1[i][j]]=;
}
}
} solve();
}
return ;
}

poj 2226 Muddy Fields(最小点覆盖+巧妙构图)的更多相关文章

  1. POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)

    题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...

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

    *.*. .*** ***. ..*. 题意:有一个N*M的像素图,现在问最少能用几块1*k的木条覆盖所有的 * 点,k为>=1的任意值. 分析:和小行星那题很像.小行星那题是将一整行(列)看作 ...

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

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

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

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

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

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

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

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

  7. POJ 2226 Muddy Fields(最小点覆盖)题解

    题意:一片r*c的地,有些地方是泥地,需要铺地板.这些地板宽1,长无限,但只能铺在泥地上不能压到其他地方,问你铺满所有泥地最少几块 思路:我们把一行中连续的泥地看成整体,并把所有横的整体里的点编成一个 ...

  8. poj 2226 Muddy Fields(最小点覆盖)

    题意: M*N的矩阵,每个格不是*就是#.     *代表水坑,#代表草地. 农民要每次可以用一块宽为1,长不限的木板去铺这个矩阵.要求这块木板不能覆盖草地.木板可以重复覆盖(即一块木板与另一块木板有 ...

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

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

随机推荐

  1. Xcode7真机测试

    根据这个网址上的步骤能够完成真机测试,我已经试过了,还不错 http://www.bubuko.com/infodetail-1061938.html

  2. java总结文章

    java总结文章 原创地址: http://www.cnblogs.com/Alandre/ (泥沙砖瓦浆木匠),须要转载的,保留下! Thanks Talk is cheap. Show me th ...

  3. [Git] Automatically running tests before commits with ghooks

    Wouldn't it be nice if everyone ran the tests before committing code? With ghooks, you can automatic ...

  4. 【SSH三框架】Struts2第六章的基础:他们拦截函数的定义

    干web当然,需要做的事情时,项目管理登录身份验证及其他权利.假设我们必须使用相应的登陆,未经允许是不可能的. 因此,我们需要使用拦截器,拦截功能struts2它集成.当然,有可能在Spring正在使 ...

  5. Hadoop 开源调度系统zeus(二)

    紧跟之前Hadoop 开源调度系统zeus(一) 本节主要介绍一下zeus的架构: 先给一个zeus的架构图 无论Master还是Worker都有一套WEB UI,无论从哪个上面去看,看到的结果都是一 ...

  6. Android设置里面默认存储器选项(default write disk)的实现

    原生的Android设置里面没有默认存储器的选项,可是MTK偏偏加上了这个功能,可能MTK认为这个比較实用吧,所以,他们在原生的基础上面做了改动.加上了这个功能.可是高通平台没有这个功能.相对MTK来 ...

  7. Java基础知识强化59:String(字符串)和其他类型的相互转化

    1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...

  8. VCS仿真查看coverage

    VCS仿真查看coverage Design compiler & simulate #!/bin/csh -f setenv NOVAS_HOME /EDASynopsys/verdi3-I ...

  9. BFC——块级格式上下文

    BFC中的B指的是block,对应的还有IFC,I指的是inline.对于BFC的理解可以参考层叠上下文.页面中,盒子的排布规则,是生效在对应的BFC中.两个BFC中的布局互不影响.页面的本身的根本身 ...

  10. 《第一行代码》学习笔记35-服务Service(2)

    1.Android的UI线程不安全,想要更新应用程序里的UI元素,则须在主线程中进行,否则会出现异常. 2.Android不允许在子线程里进行UI操作,对于该情况,Android提供了一套异步消息处理 ...