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. ED/EP系列6《扩展应用》

    包括:电子钱包复合应用:电子钱包灰锁应用. 1. 复合应用模式 Ø INITIALIZE FOR CAPP PURCHASE(复合应用消费初始化): Ø UPDATE CAPP DATA CACHE( ...

  2. EXCLE使用宏生成目录

    宏代码: Sub mu() Dim i As Integer Dim ShtCount As Integer Dim SelectionCell As Range ShtCount = Workshe ...

  3. oracle 表空间和表 read only迁移后不再read only

    DB :  11.2.0.3.0 1.将tablespace read only , 不允许再对表进行update.insert操作,测试dmp到另一个用户.表空间后是否可以update.insert ...

  4. Helloworld模块之内核makefile详解

    Hello World 模块以及对应的内核makefile详解 hello.c: #include <linux/module.h> //所有模块都需要的头文件 #include < ...

  5. Iframe跨域Session丢失的问题

    很久之前做的一个使用插件实现了图片批量上传,是通过IFrame加载上传面板的,使用google的chrome上传成功了就没怎么理了,最近同事测试时(使用的是360安全浏览器)老是出现上传不了图片的问题 ...

  6. scjp考试准备 - 5 - 重载和重写

    如下代码,在所指示的位置插入代码能够正常编译: class Alpha{ public void bar(int... x){}; public void bar(int x){}; } public ...

  7. bootstrap API地址

    http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#pagination-table

  8. VBA 一些用法

    另存为txt格式: Sheets().Activate ActiveWorkbook.SaveAs Filename:="E:\etl_folder\", FileFormat:= ...

  9. 桥接模式和NAT模式

    linux的目录结构 配置ip三种 模式 桥接模式 nat模式(VMnet8) 配置网关 DHCP动态分配IP设置 linux的目录结构 配置ip三种 模式 桥接模式 (1) 设置主系统的" ...

  10. 使用C语言在Win控制台中实现指定位置输出

    在古老的Turbo C中有个GotoXY可以让你在指定坐标中输出文字,可恨的是我看过的C语言书籍,有一半都是关于它的.我现在用着Windows系统,不可能还让我去写着DOS程序啊,起码也得从Win控制 ...