http://poj.org/problem?id=2226

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7078   Accepted: 2622

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

 
【题解】:
  

题意:给定一个矩阵,其中有一些地方有水,用一些长度任意,宽度为1的木板盖住这些有水的地方,问至少需要几块板子。

分析:二分图最小点集覆盖。二分图建图方法如下,把每段连续的横向的水洼看成是一个X集合中的点,每段连续的纵向的水洼看成是Y集合中的点。矩阵每个有水单元看成是一个边,它连接了它所在的横向水洼在X集合中对应的点和它所在的纵向水洼在Y集合中对应的点。(对于一段连续的水洼,如果要铺木板,一定要铺得尽量长)这样最小点集覆盖的意义就变成了,矩阵中的每个有水的单元(二分图中的每条边)所在的横向和纵向的水洼(在X集合和Y集合中的两个端点)至少有一个被覆盖。求至少需要选几个点。

对这个图分区域
 把行区域的编号和列区域的编号写在两边(相当于二分图的两个点集),在有公共格子的区域内连线
比如2号横区域和4号纵区域(分别在两个图中),第一行第三列的格子有重叠,所以两个连线。
   这样就是在这个二分图中求最小顶点覆盖。
   不知道读者有没有发现,二分图的边数=需要覆盖的格子数,而横一列和纵一列至多相交于一个格子,那么可以理解为,每一条边代表一个格子,然后就是覆盖每一条边,这就成了一个最小顶点覆盖问题。和之前那道题目是类似的。而之前那个题目没有障碍物,显得比较简单,一行一列就是一个区。
   这个理解是把模型建立了以后才这么想,可不知道第一个这么做的人是谁呢?真有才,如果要单纯自己想,而且没有做过类似题目,还真的很难。至于具体还真的感到很玄。
【code】:
 /**
Judge Status:Accepted Memory:36596K
Time:329MS Language:G++
Code Lenght:2165B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm> #define N 55
#define M N*N
using namespace std; int n,ny,nx,m;
int map[M][M];
int cx[M],cy[M],mark[M]; char str[N][N]; int map_x[N][N],map_y[N][N]; int path(int u)
{
int j;
for(j=;j<=ny;j++)
{
if(map[u][j]&&!mark[j])
{
mark[j]=;
if(cy[j]==-||path(cy[j]))
{
cx[u] = j;
cy[j] = u;
return ;
}
}
}
return ;
} int maxMatch() //求最小覆盖
{
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
int i;
int res = ;
for(i=;i<=nx;i++)
{
if(cx[i]==-)
{
memset(mark,,sizeof(mark));
res+=path(i);
}
}
return res;
} void getMap()
{
memset(map,,sizeof(map));
memset(map_x,,sizeof(map_x));
memset(map_y,,sizeof(map_y));
int i,j;
int flag;
nx=ny=;
for(i=;i<n;i++) //横向分区域
{
for(j=;j<m;j++)
{
flag = ;
while(j<m&&str[i][j]=='*')
{
if(!flag) ++nx;
map_x[i][j] = nx;
j++;
flag = ;
}
}
} for(j=;j<m;j++) //纵向分区
{
for(i=;i<n;i++)
{
flag = ;
while(i<n&&str[i][j]=='*')
{
if(!flag) ++ny;
flag = ;
map_y[i][j] = ny;
i++;
}
}
} for(i=;i<n;i++) //横向纵向区域连接
{
for(j=;j<m;j++)
{
int x = map_x[i][j];
int y = map_y[i][j];
if(x&&y)
{
map[x][y] = ; //得到二分map图,然后进行最小覆盖
}
}
}
} int main()
{
int i;
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
{
scanf("%s",str[i]);
}
getMap();
printf("%d\n",maxMatch());
return ;
}

poj 2226 Muddy Fields (转化成二分图的最小覆盖)的更多相关文章

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

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

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

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

  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 (二分匹配)

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

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

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

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

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

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

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

  8. poj 2226 Muddy Fields (二分图)

    大意:给定n*m网格, 每个格子为泥地或草地, 可以用一些长度任意宽度为1的木板盖住泥地, 要求不能盖到草地, 求最少要多少块木板能盖住所有泥地. 最小点覆盖板子题, 建图跑最大匹配即可. #incl ...

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

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

随机推荐

  1. C++STL学习笔记_(4)queue

    10.2.5Queue容器 Queue简介 ²  queue是队列容器,是一种"先进先出"的容器. ²  queue是简单地装饰deque容器而成为另外的一种容器. ²  #inc ...

  2. VSX规划Package文件

    VSX是VS扩展,可以针对不同项目编写插件,虽然接触VSX的时间并不多,但是当了解VSX后深刻感受到VSX的魅力. VSX的材料比较少,配置文件也很繁琐,当初我也走了不少弯路. 这篇文章将帮助您更好的 ...

  3. CXF(2.7.10) - RESTful Services

    1. 定义 JavaBean.注意 @XmlRootElement 注解,作用是将 JavaBean 映射成 XML 元素. package com.huey.demo.bean; import ja ...

  4. 关于IOS的蓝牙(转)

    关于IOS的蓝牙 首先,你要了解你的目的是什么,一般的IOS蓝牙开发有以下三种目的: 1. IOS设备和IOS设备之间交互 好消息是:ios6.0可以把iPhone手机当从设备了,可以两台iPhone ...

  5. Unity出现 error building player exception android (invocation failed)

    今天在编译Android的时候出现这个错误 error building player exception android (invocation failed) 百度谷歌之后,看到xuanyuson ...

  6. Linux multiple open a device

    Linux multiple open a device a device = /dev/wiegand Linux在多次打开同一个设备(/dev/wiegand)的时候,打开结果都是成功,但是在用w ...

  7. 09_rlCoachKin讲解

    在Socket.cpp中Socket::readClient()函数中就是解析读取到的内容的. 对于我们发送的2 0 1.57 0.31 0 0 1.57 0,那么就会进入如下分支: 也就是进入2号处 ...

  8. DEDECMS中,常见全局变量

    全局变量 {dede:global.cfg_cmspath/} 是dedecms 的安装目录,一般就是网站的根目录. {dede:global.cfg_cmsurl/}是当前目录 注意加一根斜线{de ...

  9. [转]PHP错误日志

    对 于PHP开发者来说,一旦某个产品投入使用,应该立即将display_errors选项关闭,以免因为这些错误所透露的路径.数据库连接.数据表等信息 而遭到黑客攻击.但是,任何一个产品在投入使用后,都 ...

  10. Android 源码VecotorDrawable

    1 R.styleable.VectorDrawable_viewportWidth 该资源的名字并非VectorDrawable_viewportWidth 而是 attrs.xml 下的声明 &l ...