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升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...
随机推荐
- 路由vue-router基础
目录 1. 基本例子 2. 动态路由匹配 3. 嵌套路由 4. 编程式导航 5. 命名路由 6. 命名视图 7. 重定向和别名 8. 向路由组件传递props 9. HTML5 History模式 官 ...
- Java中String类new创建和直接赋值字符串的区别
转自:https://blog.csdn.net/a986410589/article/details/52454492 方式一:String a = “aaa” ; 方式二:String b = n ...
- Spring 各种注解备注
Spring 各种注解备注 felix_feng 关注 2016.12.28 10:34* 字数 2092 阅读 790评论 0喜欢 6 转载 (http://blog.csdn.net/sudilu ...
- php 升级php5.5 、php7
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm yum install php55w php55w-opcache yum install ...
- HTML5标签学习笔记
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="con ...
- python3知识点之---------列表的介绍
1.列表是什么? 它是由一系列特定顺序排序的元素组成.元素可以表示一切任何的事物,元素之间可以没有任何关系.列表用方括号[ ] 表示,元素之间由逗号隔开. 例如表示一系列数字的列表: numbe ...
- shell之netstat命令
语 法:netstat [-acCeFghilMnNoprstuvVwx] [-A<网络类型>][--ip] 补充说明:利用netstat指令可让你得知整个Linux系统的网络情况. ...
- 删除maven仓库中的lastUpdate文件
使用idea时导入hibernate 5.1.0的jar包,然后发现本地仓库中找不到该版本的jar 然后手贱 alt+enter 发现提示 update maven indices 然后以为更新就会好 ...
- Python字符串相关
#字符串的相关操作 #基本操作 #+ 字符串连接操作 str1 = '来是come走是go' str2 = '点头yes摇头no' result = str1 + str2 print(result) ...
- [hdu6434]Problem I. Count
题目大意:$T(T\leqslant 10^5)$组数据,每组数据给你$n(n\leqslant 2\times 10^7)$,求$\sum\limits_{i=1}^n\sum\limits_{j= ...