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. hdu1501 动态规划

    这题有两种解题思路,一个是记忆化搜索,一个是dp. 分别贴代码: 记忆化搜索: #include<iostream> #include<cstdio> #include< ...

  2. this及其作用域(函数外部this变量的调用)

    众所周知的一件麻烦事是函数外部的this变量都不可见,但是我们在编写使用局部函数的方法时,却又很可能需要在某一时刻从内部函数访问this变量. 这种情况下可以通过在this变量中存储一个需要的信息(例 ...

  3. Linux 命令 - sort: 行排序文本文件

    命令格式 sort [OPTION]... [FILE]... 命令参数 -b, --ignore-leading-blanks 忽略开头的空白字符. -d, --dictionary-order 只 ...

  4. Slickflow.NET 开源工作流引擎基础介绍(三) -- 基于HTML5/Bootstrap的Web流程设计器

    1. Slickflow Designer 技术优势 1) 基于HTML5技术,兼容常见浏览器; 2) 纯Javascript / JsPlumb 脚本实现SVG图形化显示; JsPlumb允许您使用 ...

  5. Learn Python The Hard Way学习笔记001

    今天搜索了一下raw_input() 和 input()的区别,引用下原文部分内容 两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收).而对于 ...

  6. 非Page类使用session(Httpcontext.session和page.session区别)

    ASP.NET中Session高级使用技巧 在开发Aspx .NET软件时,有时需要把常用的东西封装到一个非PAGE类中,文章介绍在非Page类中使用Session的方法. 一.PAGE参数法: 1. ...

  7. 119. Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  8. Java使用泛型类来提高方法的可重用性

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3832268.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  9. iOS 非ARC基本内存管理系列 4-autorelease方法和@autoreleasepool

    1.autorelease 基本用法 对象执行autorelease方法时会将对象添加到自动释放池中 当自动释放池销毁时自动释放池中所有对象作release操作 对象执行autorelease方法后自 ...

  10. Call与Apply

    1.前言 ECMAscript中提供了两个方法(call,apply)用于改变对象内部的this指针,它们两个的作用都是一样的,但是传递的参数有点不大相同. 它们的大概语法为: call(this, ...