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. cavium octeon 处理器启动总线Bootbus 简介

    cavium octeon 处理器启动总线Bootbus 简介: 韩大卫@吉林师范大学 Boot-bus(启动总线)是cavium octeon处理器的一种用于启动系统的硬件. CPU通过boot b ...

  2. 怎么给iOS项目打包

    1  首先要选中项目中的真机測试,不要模拟器 ,然后从上边的菜单条中找product watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29tZXJhaW43 ...

  3. Could not find the Visual SourceSafe Internet Web Service connection information

    Visual SourceSafe Internet---------------------------Could not find the Visual SourceSafe Internet W ...

  4. AngularJs练习Demo15自定义服务

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  5. OC随笔一:类

    总结:        在oc中,我们要整出一个类来,首先需要一个.h头文件和一个.m实现文件.一般我们创建的类都继承了根类,因为根类帮我们实现了很多实用的方法,而类里面会有变量(属性) .函数(方法) ...

  6. QT小插件类之QRoundProgressBar

    QRoundProgressBar类 1. 详细描述 QRoundProgressBar类能够实现一个圆形的进度图表,并且有和QProgressBar类似的API接口 1.1 继承关系 #includ ...

  7. HDU4545+计算日期

    /* 计算过了D天后的日期 之前D天的日期 */ #include<stdio.h> int judge_year( int year ){ if( (year%4==0&& ...

  8. 你不了解PHP的10件事情

    看到有人翻译的<10 things you (probably) didn’t know about PHP>,发现在此次之前2.8两条并不知道,1.3虽然熟知但是去没有实际应用. 由于阅 ...

  9. Android再学习-20141111-Android应用的七大件

    Android应用的七大件 应用程序的四大组件: Android的四大组件,使用时需要在程序中注册. Activity: Activity是应用程序的一个界面,可以通过这个界面查看联系人.打电话或者玩 ...

  10. 【转】对ARM堆栈的理解

    对ARM堆栈的理解 堆栈严格来说应该叫做栈,栈(Stack)是限定仅在一端进行插入或删除操作的线性表.因此,对栈来说,可以进行插入或删除操作的一端端称为栈顶(top),相应地,另一端称为栈底(bott ...