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. 今天学习css一些动画效果

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  2. backboneJs 导图

  3. Six important .NET concepts 【Turn】

    Introduction This article will explain six important concepts: stack, heap, value types, reference t ...

  4. Ehcache(2.9.x) - API Developer Guide, Cache Loaders

    About Cache Loaders A CacheLoader is an interface that specifies load() and loadAll() methods with a ...

  5. Android通过LIstView显示文件列表

    [绥江一百]http://www.sj100.net                                                  欢迎,进入绥江一百感谢点击[我的小网站,请大家多 ...

  6. Oracle 硬解析查询

    -- 硬解析的 parse count (hard) select * from v$sysstat where name like '%parse%'; select a.value,b.name ...

  7. UEditor的使用方法

    文本编辑器程序员都会用到,今天无意中发现UEditor是个好东西,特奉献给大家,并提供下载. 这个编辑器的亮点是可以显示当前输入多少字符,还可以输入多少字符. 新创建的MVC项目,在layout页面里 ...

  8. node.js笔记——gulp

    1.全局安装 npm install gulp -g 2.安装到具体目录,并安装相应的自动化插件 npm install -save-dev gulp gulp-concat gulp-minify- ...

  9. MyEclipse导入ant项目——Java编程思想

    北门煎饼东门串儿: <JAVA编程思想(Think in Java)>一书中提供了大量源代码,可是项目是用ant构建的.对于用惯了eclipse,netbeans等IDE的同学们可能有些手 ...

  10. 对整站的a链接进行监控,对匹配规则进行指定页面的跳转

    项目中有个需求,就是将非本站的链接跳转到过渡页(提示即将离开本站的那种页面).这个时候想起了腾讯邮箱,不安全链接会有新的页面提示,如下图: 本以为ASP.NET中有全局的方法获取到点击或者跳转的链接, ...