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. lesson3.1:java公平锁和非公平锁及读写锁

    关于这四种锁的各自情况,网上有很多文章做了介绍,本不想单独开章节介绍,本章只介绍这四种锁的一些源码特点及注意事项. demo 源码:https://github.com/mantuliu/javaAd ...

  2. 微信中web页面实现和公众号中查看图片一样的效果

    最近开发了一套资讯相关的web页面,嵌套在微信中,可支持点赞.评论等...在文章详情中,图片需要点击放大,随手势放大缩小,左右可滑动切换,总之类似于微信公众号效果. 开始想的方案是用轮播插件.或者在i ...

  3. volatile-java关键字

    volatile的作用: 作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 简单地说就是防止编译器对代码进行优化.比如如下程序: XBYTE[2]=0x55; XBYTE[2 ...

  4. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  5. php增删改查,自己写的demo

    1.链接数据库通用方法:conn.php <?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root ...

  6. uploadify上传控件中文的乱码解决办法

    uploadify上传控件中文的乱码解决办法 网站用的gb2312的编码,用uploadify上传控件上传中文时在IE能部分成功,FF,Chrome则完全失败,查找了一天原因,结果发现是页面编码问题, ...

  7. mybatis之mapper.xml分析

    select: id:方法名,在同一个mapper.xml中,要保持唯一 parameterType:指定输入的参数类型,不是必须的,如果不指定,mybatis会自动识别(推荐指定). resultT ...

  8. 关于为什么RAID5往往掉一个盘后第二个盘也立刻挂掉的原因分析

    很多人遇到过服务器RAID5挂掉,往往掉一个盘后,第二个盘也立刻挂掉. 大家都知道RAID5 一次允许一个盘缺失, RAID 5也是以数据的校验位来保证数据的安全,但它不是以单独硬盘来存放数据的校验位 ...

  9. Asp.Net WebAPI传递json对象、后台手动接收参数

    1.前台代码 /* * 跨域请求Post * 1个对象参数,后台JObject接受 */ $.post(apiUrl.getOne("PostFourth"), { name: } ...

  10. 关于.net根目录路径的问题

    今天做了一个项目,用了url重写,但是在本地目录是localhost/BK/index.aspx,而其他目录也必须带bk,不带的话就找不到页面,(iis里正常) 应该主目录去掉bk,设置方法,右击网站 ...