POJ 2226 Muddy Fields(二分匹配 巧妙的建图)
Description
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
* 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
Sample Input
4 4
*.*.
.***
***.
..*.
Sample Output
4
Hint
Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
..2.
Board 2 overlaps boards 3 and 4.
Source
题意:农夫John的养牛场,是一个R 行C 列的矩形,一场大雨后,养牛场低洼的地方都有了积水。John 的牛都很娇贵的,他们吃草的时候,不想把他们的蹄子给弄脏了。为了不让牛儿们把它们的蹄子弄脏,John 决定把有水的地方铺上木板。他的木板是宽度为1,长度没有限制的。
他想用最少数目的木板把所有有水的低洼处给覆盖上,前提是木板不能覆盖草地,但是可以重叠。
这道题,构图确实比比较巧妙,如果有连续的低洼处,假设是横排的,那么这几个连续的低洼处可以拿一个板子来覆盖,同样,如果竖排也有连续的低洼处,那么也可以拿一个板子来覆盖。这样,当一个低洼处既可以拿横着的板子,也可以拿竖着的板子覆盖时,就是相交了。那么这个交线就代表了一个低洼处,它既可以被横着盖,也可以被竖着盖。现在我们把所有横排的低洼处作为left(连续的低洼处作为1个板),竖着的也同理,以例题如下表式:
Sample:
4 4
*.*.
.***
***.
..*.
把行里面连在一起的坑连起来视为一个点,即一块横木板,编上序号,Sample则转化为:
1 0 2 0
0 3 3 3
4 4 4 0
0 0 5 0
把这些序号加入X集合,再按列做一次则为:
1 0 4 0
0 3 4 5
2 3 4 0
0 0 4 0
同样加入Y集合,一个坑只能被横着的或者被竖着的木板盖住,将原图的坑的也标上不同的序号,一共九个坑
1 . 2 .
. 3 4 5
6 7 8 .
. . 9 .
图中的2号低洼处既可以拿横着的2号板,也可以拿竖着的4号板来覆盖,那么2号版和四号板之间就有一条边,边实际表示了低洼处,例如2号低洼处的边为{2,4},可以理解为2号低洼处可以由2号板或4号板覆盖。用最少的点把所有的边连起来,这样就是最小点覆盖。
以上来自: http://hi.baidu.com/onlys_c/blog/item/9781e0dd858f2fd28d102919.html
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("in.txt","r",stdin)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
const int maxn =;
typedef long long LL;
int n, m, mp[maxn][maxn], x[maxn][maxn], y[maxn][maxn];
int num1, num2, dfscnt, a[maxn][maxn], vis[maxn], match[maxn];
char tu[maxn][maxn];
int dfs(int rt) {
for (int i = ; i <= num2 ; i++) {
if (mp[rt][i]) {
if (vis[i] != dfscnt) {
vis[i] = dfscnt;
if (!match[i] || dfs(match[i])) {
match[i] = rt;
return ;
}
}
}
}
return ;
}
int main() {
while(~scanf("%d%d", &n, &m)) {
for (int i = ; i <= n ; i++) {
scanf("%s", tu[i]);
for (int j = ; j < m ; j++) {
if (tu[i][j] == '*') a[i][j + ] = ;
else a[i][j+] = ;
}
}
num1 = , num2 = ;
for (int i = ; i <= n ; i++) {
for (int j = ; j <= m ; j++) {
if (a[i][j] == ) {
num1++;
while(j <= m && a[i][j] == ) {
x[i][j] = num1;
j++;
}
}
}
}
for (int j = ; j <= m ; j++) {
for (int i = ; i <= n ; i++) {
if (a[i][j] == ) {
num2++;
while(i <= n && a[i][j] == ) {
y[i][j] = num2;
i++;
}
}
}
}
for (int i = ; i <= n ; i++) {
for (int j = ; j <= m ; j++) {
if (a[i][j] == ) mp[x[i][j]][y[i][j]] = ;
}
}
dfscnt = ;
int ans = ;
for (int i = ; i <= num1 ; i++) {
dfscnt++;
if (dfs(i)) ans++;
}
printf("%d\n", ans);
}
return ;
}
POJ 2226 Muddy Fields(二分匹配 巧妙的建图)的更多相关文章
- poj 2226 Muddy Fields (二分匹配)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7340 Accepted: 2715 Desc ...
- POJ 2226 Muddy Fields(最小顶点覆盖)
POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- poj 2226 Muddy Fields(最小覆盖点+构图)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2226 Muddy Fields (转化成二分图的最小覆盖)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- poj 2226 Muddy Fields(最小点覆盖+巧妙构图)
Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= ...
- POJ 2226 Muddy Fields 二分图(难点在于建图)
题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...
- poj 2226 Muddy Fields(合理建图+二分匹配)
/* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...
- POJ 2226 Muddy Fields (二分图匹配)
[题目链接] http://poj.org/problem?id=2226 [题目大意] 给出一张图,上面有泥和草地,有泥的地方需要用1*k的木板覆盖, 有草地的地方不希望被覆盖,问在此条件下需要的最 ...
随机推荐
- Angualr6访问API
参照 草根专栏- ASP.NET Core + Ng6 实战: https://v.qq.com/x/page/a0769armuui.html 1.environment.ts 添加apiUrlBa ...
- 【Linux 运维】 Centos7.x 系统修复模式
一.linux的运行级别: 运行级别就是来确定系统启动时到底启动那个服务. linux默认有7个运行级别: 0 关机 1 单用户模式,用于系统修复 2 不完全的命令模式,不含NFS服务 3 完全的命令 ...
- 基于深度学习的中文语音识别系统框架(pluse)
目录 声学模型 GRU-CTC DFCNN DFSMN 语言模型 n-gram CBHG 数据集 本文搭建一个完整的中文语音识别系统,包括声学模型和语言模型,能够将输入的音频信号识别为汉字. 声学模型 ...
- HTML5+Bootstrap 学习笔记 1
HTML <header> 标签 <header> 标签定义文档的页眉(介绍信息),是 HTML 5 中的新标签. 参考资料: HTML <header> 标签 h ...
- Fluent Python: @property
Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...
- POJ 2455 Secret Milking Machine(最大流+二分)
Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...
- 简单DP
1.一只小蜜蜂 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input输入数据的第一行是一个整数N,表 ...
- 阿里云服务器 操作实战 部署C语言开发环境(vim配置,gcc) 部署J2EE网站(jdk,tomcat)
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 博客总结 : 设置SecureCRT ...
- 【Docker】- 基本命令
1.docker ps -a 显示所有容器 2.doker ps -l 显示最近一次启动的容器 3.docker ps 显示正在运行的容器 4.docker start [容器ID] 启动 ...
- activity属性设置大全
activity属性设置大全 android:allowTaskReparenting=["true" | "false"] 是否允许activ ...