POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12565 | Accepted: 4651 |
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.
//F - Muddy Fields POJ - 2226
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
const int N=maxn*maxn/;
int G[N][N],vis[N],link[N];
int n;
bool Find(int u){
for(int i=;i<=n;i++){
if(G[u][i]&&!vis[i]){
vis[i]=;
if(link[i]==-||Find(link[i])){
link[i]=u;
return true;
}
}
}
return false;
}
int solve(){
int cnt=;
memset(link,-,sizeof(link));
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(Find(i))cnt++;
}
return cnt;
}
char mat[maxn][maxn];
int vis1[maxn][maxn],vis2[maxn][maxn];
int main(){
int m;
for(int i=;i<=;i++)
mat[][i]=mat[i][]='.';
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
cin>>mat[i][j];
}
int tot1=,tot2=;
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mat[i][j]=='*'){
if(!vis1[i][j-])vis1[i][j]=tot1++;
else vis1[i][j]=vis1[i][j-];
if(!vis2[i-][j])vis2[i][j]=tot2++;
else vis2[i][j]=vis2[i-][j];
}
}
}
memset(G,,sizeof(G));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mat[i][j]=='*')
G[vis1[i][j]][vis2[i][j]]=;
}
}
n=max(tot1,tot2);
printf("%d\n",solve());
}
return ;
}
POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)的更多相关文章
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- poj 2226 Muddy Fields (二分图)
大意:给定n*m网格, 每个格子为泥地或草地, 可以用一些长度任意宽度为1的木板盖住泥地, 要求不能盖到草地, 求最少要多少块木板能盖住所有泥地. 最小点覆盖板子题, 建图跑最大匹配即可. #incl ...
- POJ 2226 Muddy Fields 二分图(难点在于建图)
题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...
- POJ 2226 Muddy Fields(最小顶点覆盖)
POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- poj 2226 Muddy Fields(最小点覆盖)
题意: M*N的矩阵,每个格不是*就是#. *代表水坑,#代表草地. 农民要每次可以用一块宽为1,长不限的木板去铺这个矩阵.要求这块木板不能覆盖草地.木板可以重复覆盖(即一块木板与另一块木板有 ...
- poj 2226 Muddy Fields (转化成二分图的最小覆盖)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ - 2226 Muddy Fields (最小顶点覆盖)
*.*. .*** ***. ..*. 题意:有一个N*M的像素图,现在问最少能用几块1*k的木条覆盖所有的 * 点,k为>=1的任意值. 分析:和小行星那题很像.小行星那题是将一整行(列)看作 ...
- poj 2226 Muddy Fields(最小覆盖点+构图)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)
题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...
随机推荐
- SDWebImage实现原理(怎么实现图片缓存器)
入口 setImageWithURL:placeholderImage:options: 会先把 placeholderImage 显示,然后 SDWebImageManager 根据 URL 开始处 ...
- Android记事本08
昨天: Anr问题异常的原因和解决方案. 今天: Activity数据传递之通用方式. Activity数据传递之静态变量. Activity数据传递之全局变量. 遇到的问题: 无.
- DOM关于高度宽度位置的获取
假设 obj 为某个 HTML 控件. obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算上侧位置,整型,单位像素. obj.offsetLeft ...
- php 值传递和引用传递的区别
值传递:函数范围内对值的任何改变在函数外部都会被忽略 引用传递:函数范围内对值的任何改变在函数外部也能反映出这些修改 A:按值传递时,php必须复制值.特别是对于大型的字符串和对象来说,这将会是一个代 ...
- GCD 开发详情
目录 一.简介 二.dispatch Queue - 队列 三.dispatch Groups - 组 四.dispatch Semaphores - 信号量 五.dispatch Barriers ...
- 【bzoj4419】[Shoi2013]发微博 STL-set
题目描述 刚开通的SH微博共有n个用户(1..n标号),在短短一个月的时间内,用户们活动频繁,共有m条按时间顺序的记录: ! x 表示用户x发了一条微博: + x y 表示用户x和用户y成为了好友 ...
- 三十道DP练习(持续更新)(pw:DP)
前言: 话说DP这种纯考思维的题目,总是让我很伤脑筋,一些特别简单的DP我都常常做不出来,所以革命从现在(2018-05-01)开始,努力多刷点DP的练习-. 1.顺序对齐(align) 时间:201 ...
- [hdu6434]Problem I. Count
题目大意:$T(T\leqslant 10^5)$组数据,每组数据给你$n(n\leqslant 2\times 10^7)$,求$\sum\limits_{i=1}^n\sum\limits_{j= ...
- BZOJ 3932: [CQOI2015]任务查询系统 | 主席树练习题
题目: 洛谷也能评测 题解: De了好长时间BUG发现是自己sort前面有一行for没删,气死. 题目询问第x秒时候前k小的P值之和. 朴素想法: 我们可以把P值离散化,然后对于每个时刻建一棵定义域是 ...
- freemarker 前端 判读 遍历 取值
<#if content?length gt 100> ${content[0..100]}... <#else> ${content} </#if> freema ...