poj 2226 Muddy Fields (二分匹配)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7340 | Accepted: 2715 |
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
//9008K 63MS C++ 1570B 2013-11-10 20:07:57
/* (网摘思路)
题意:给定一个矩阵,其中有一些地方有水,用一些长度任意,宽度为1的木板盖住这些有水的地方,不能挡住草地,木板可覆盖,问至少需要几块板子。 分析:二分图最小点集覆盖。二分图建图方法如下,把每段连续的横向的水洼看成是一个X集合中的点,每段连续的纵向的水洼看成是Y集合中的点。
矩阵每个有水单元看成是一个边,它连接了它所在的横向水洼在X集合中对应的点和它所在的纵向水洼在Y集合中对应的点。(对于一段连续的水洼,
如果要铺木板,一定要铺得尽量长)这样最小点集覆盖的意义就变成了,矩阵中的每个有水的单元(二分图中的每条边)所在的横向和纵向的水洼
(在X集合和Y集合中的两个端点)至少有一个被覆盖。求至少需要选几个点。 */
#include<stdio.h>
#include<string.h>
struct node{
int x;
int y;
}edge[][];
char c[][];
int g[][];
int vis[];
int match[];
int n,m,N,M;
void build() //建图
{
memset(g,,sizeof(g));
N=M=;
for(int i=;i<n;i++) //处理行,作二分图的一集合
for(int j=;j<m;j++){
while(c[i][j]=='.' && j<m) j++;
while(c[i][j]=='*' && j<m){
edge[i][j].x=N;
j++;
}
N++;
}
for(int j=;j<m;j++) //处理列,作二分图的另一集合
for(int i=;i<n;i++){
while(c[i][j]=='.' && i<n) i++;
while(c[i][j]=='*' && i<n){
edge[i][j].y=M;
i++;
}
M++;
}
for(int i=;i<n;i++) //建图
for(int j=;j<m;j++)
if(c[i][j]=='*')
g[edge[i][j].x][edge[i][j].y]=;
}
int dfs(int x)
{
for(int i=;i<M;i++){
if(!vis[i] && g[x][i]){
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
}
int hungary() //匈牙利算法
{
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<N;i++){
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
return ans;
}
int main(void)
{
while(scanf("%d%d%*c",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
scanf("%s",c[i]);
build();
printf("%d\n",hungary());
}
return ;
}
poj 2226 Muddy Fields (二分匹配)的更多相关文章
- 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(最小点覆盖+巧妙构图)
Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= ...
- POJ 2226 Muddy Fields(二分匹配 巧妙的建图)
Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R ...
- poj 2226 Muddy Fields(合理建图+二分匹配)
/* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...
- POJ 2226 Muddy Fields (二分图匹配)
[题目链接] http://poj.org/problem?id=2226 [题目大意] 给出一张图,上面有泥和草地,有泥的地方需要用1*k的木板覆盖, 有草地的地方不希望被覆盖,问在此条件下需要的最 ...
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- poj 2226 Muddy Fields(水二分图)
Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 ...
随机推荐
- android SearchView和ListView简单使用
其实我写代码最担心遇到关于适配器的使用,在我的感觉中适配器是个难度很大的知识点,但是不能因为难而不去学习啊,毕竟现在时间很充裕,可以慢慢学,所以,不会也要写,真所谓,迎难而上啊. 下面是Search ...
- vue入门——组件基础todolist
1. 以下是 todolist 的例子,没有用到组件:下面的3 会通过组件拆分todolist <!DOCTYPE html> <html lang="en"&g ...
- (转)程序员新人怎样在复杂代码中找 bug?
我曾经做了两年大型软件的维护工作,那个项目有10多年了,大约3000万行以上的代码,参与过开发的有数千人,代码checkout出来有大约5个GB,而且bug特别多,open的有上千,即使最高优先级的s ...
- 分享一个PC端六格密码输入框写法
如图.我们一般做商城类的项目不免会用到支付密码输入框,我研究了下并决定发上来,也当作是自己成长路上的一点小小的记录.本次介绍的是基于vue的项目 html: <template> < ...
- YII2.0 用GII创建视图文件后访问404
使用GII的CRUD Generator创建searchModelClass 和控制器类文件,视图文件后,访问控制器地址后出现404的情况. 创建过程如图所示 后来发现是控制器类 Controller ...
- 毕业2年 Summary
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/135 看了下去年写毕业一周年总结的时间:2017-6-16,今天 ...
- 003---设计首页index页面
在项目的urls.py文件添加一条url from django.contrib import admin from django.urls import path, re_path from app ...
- Hyperledger Fabric国密改造
Fabric国密改造是个什么概念?我们来思考以下4个问题: 为什么偏偏是密码算法?(WHY?) 什么是国密算法?(WHAT?) 改造切入点什么?(WHERE?) 如何实现国密支持?(HOW?) 1.为 ...
- BFS 队列
Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants to model thi ...
- 如何在windows“我的电脑”中添加快捷文件夹
如图所示,windows中打开“我的电脑”时,原来有6个默认的文件夹,访问非常便捷,自己想再增加,可以使用“ThisPCTweaker”即可完成 操作如下图,不多解释,简单操作: 文件下载:http: ...