Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7557   Accepted: 2791

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

 
每个点所在的行连通块对点所在的列连通快连一条边,求二分图最大匹配即可。
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector> using namespace std; #define maxn 55
#define maxnode 2505 char s[maxn][maxn];
int un,vn,r,c;
int match[maxnode];
bool vis[maxnode];
vector<int> G[maxnode];
int row[maxn][maxn],col[maxn][maxn]; bool dfs(int u) {
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(vis[v]) continue;
vis[v] = ;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
return true; }
} return false;
} void build() {
un = vn = ; for(int i = ; i <= r; ++i) {
for(int j = ; j < c; ++j) {
if(s[i][j] == '*') {
row[i][j] = s[i][j - ] == '*' ?
row[i][j - ] : ++un;
} }
} for(int i = ; i < c; ++i) {
for(int j = ; j <= r; ++j) {
if(s[j][i] == '*') {
col[j][i] = s[j - ][i] == '*' ?
col[j - ][i] : ++vn;
}
}
} for(int i = ; i <= r; ++i) {
for(int j = ; j < c; ++j) {
if(s[i][j] == '*') {
// printf("%d %d\n",row[i][j],col[i][j]);
G[ row[i][j] ].push_back(col[i][j]);
}
}
} } void solve() { build(); int ans = ; for(int i = ; i <= vn; ++i) match[i] = -; for(int i = ; i <= un; ++i) {
memset(vis,,sizeof(vis));
if(dfs(i)) ++ans;
} printf("%d\n",ans);
} int main() {
freopen("sw.in","r",stdin); scanf("%d%d",&r,&c); for(int i = ; i <= r; ++i) {
scanf("%s",s[i]);
//puts(s[i]);
} solve(); return ;
}

POJ 2226的更多相关文章

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

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

  2. POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12565   Accepted: 4651 Des ...

  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 (转化成二分图的最小覆盖)

    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 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

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

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

  7. poj 2226(最小覆盖)

    题目链接:http://poj.org/problem?id=2226 思路:将连续的横向水洼看成X集合中的一个点,连续的纵向水洼看成Y集合中的一个点,而将每个水点看成一条边,它连接了所在的X集合中的 ...

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

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

  9. POJ 2226 缩点建图+二分图最大匹配

    这个最小覆盖但不同于 POJ 3041,只有横或者竖方向连通的点能用一块板子覆盖,非连续的,就要用多块 所以用类似并查集方法,分别横向与竖向缩点,有交集的地方就连通,再走一遍最大匹配即可 一开始还有点 ...

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

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

随机推荐

  1. Linux 删除文件后空间不释放磁盘空间

    在Linux操作系统下,有个没用的文件占用了400G,于是删除掉. [root@yoon log]# rm -rf messages [root@yoon log]# df -hFilesystem ...

  2. R中统计量的中英文解释

    Intercept————截距 formula————公式   Residual standard error残差标准差: 1.319 on 10 degrees of freedom 自由度为10 ...

  3. BASE64与单向加密算法MD5&SHA&MAC

    言归正传,这里我们主要描述Java已经实现的一些加密解密算法,最后介绍数字证书.     如基本的单向加密算法: BASE64 严格地说,属于编码格式,而非加密算法 MD5(Message Diges ...

  4. 基于Elasticsearch进行地理检索,计算距离值

      实现步骤: 1.定义属性     [Serializable]     public class Coordinate     {         public double Lat { get; ...

  5. Entity Framework 学习第二天 续

    今天来写一点不一样的删除,修改,查询 下面只写了几个方法 /// <summary> /// 根据删除条件进行删除 /// </summary> /// <param n ...

  6. Entity Framework学习笔记(一)

    请注明转载地址:http://www.cnblogs.com/arhat 哈哈!老魏回来了,4月份的内容开始更新了,由于3月份时间都在做项目,没有时间写了,那么4月份老魏会尽可能的多写点东西的.那么4 ...

  7. Unity3d之Animation(动画系统)

    1,动画系统配置,2,代码控制动画 原文地址: http://blog.csdn.net/dingkun520wy/article/details/51247487 1,动画系统配置 创建游戏对象并添 ...

  8. 在windows下使用vs2013编译和调试mysql源代码

    1. 准备工作 1)OS:win10 + VS2013 2)mysql 源码(windows版):mysql-5.6.25.zip 3)perl tool:ActivePerl-5.16.3.1604 ...

  9. Android -- 资源使用和总结经验分享

    颜色资源                                                                                       颜色XML文件格式 ...

  10. 20145120 《Java程序设计》第10周学习总结

    20145120 <Java程序设计>第10周学习总结 教材学习内容总结 转自:http://www.cnblogs.com/springcsc/archive/2009/12/03/16 ...