BNUOJ 2345 Muddy Fields
Muddy Fields
This problem will be judged on PKU. Original ID: 2226
64-bit integer IO format: %lld Java class name: Main
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
解题:求最小点覆盖,也就是求最大匹配。将每行的连续水块标上号,作为一个顶点集合的,将每一列连续的水块标上号,作为一个顶点集合的,然后每个水格以其所在的行块标号和列块标号为端点,建立边。二分图建立,然后求最大匹配即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
char mp[][];
int row,col;
int r[][],c[][];
vector<int>g[];
int lik[],cnt;
bool used[];
void init(){
int i,j;
char pre = '-';
cnt = ;
memset(r,,sizeof(r));
memset(c,,sizeof(c));
for(i = ; i < row; i++){
for(j = ; j < col; ){
if(mp[i][j] == '*'){
while(j < col && mp[i][j] == '*') {r[i][j] = cnt;j++;}
cnt++;
}else j++;
}
}
for(i = ; i < col; i++){
for(j = ; j < row; ){
if(mp[j][i] == '*'){
while(j < row && mp[j][i] == '*'){c[j][i] = cnt;j++;}
cnt++;
}else j++;
}
}
}
bool dfs(int u){
for(int i = ; i < g[u].size(); i++){
if(!used[g[u][i]]){
used[g[u][i]] = true;
if(lik[g[u][i]] == - || dfs(lik[g[u][i]])){
lik[g[u][i]] = u;
return true;
}
}
}
return false;
}
int main(){
int i,j,ans;
while(~scanf("%d%d",&row,&col)){
for(i = ; i < row; i++)
scanf("%s",mp[i]);
for(i = ; i < ; i++){
g[i].clear();
lik[i] = -;
}
init();
for(i = ; i < row; i++){
for(j = ; j < col; j++){
if(mp[i][j] == '*'){
g[r[i][j]].push_back(c[i][j]);
g[c[i][j]].push_back(r[i][j]);
}
}
}
for(ans = ,i = ; i < cnt; i++){
memset(used,false,sizeof(used));
if(dfs(i)) ans++;
}
cout<<(ans>>)<<endl;
}
return ;
}
BNUOJ 2345 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(最小顶点覆盖)
POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- Muddy Fields
Muddy Fields Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- bzoj 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 最小点覆盖
链接 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 思路 这就是个上一篇的稍微麻烦版(是变脸版,其实没麻烦) 用边长为1的模板覆盖地图上的没有长草的土地,不能覆盖草地 ...
- poj 2226 Muddy Fields (二分匹配)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7340 Accepted: 2715 Desc ...
- poj Muddy Fields
Muddy Fields 原题去我创的专题里找,在文件夹首页. 题目: 给出N*M矩阵.当中*表示泥土,.表示小草.要你用最少的木板把泥土覆盖. 木板长度不限.可是仅仅能水平和竖直. 行列式二分匹配配 ...
- POJ Muddy Fields 泥泞的牧场 二分图
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13235 Accepted: 4879 汪星人 ...
- POJ2226 Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10149 Accepted: 3783 Description Rain ...
随机推荐
- SPRING-BOOT系列之SpringBoot快速入门
今天 , 正式来介绍SpringBoot快速入门 : 可以去如类似 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/refer ...
- 学好Mac常用命令,助力iOS开发
原文出处: Jack_lin(@Jack_Lin_IOS ) 厚重·技术 序言 在iOS开发的过程中,更多地注重iOS开发的效率,熟练使用Mac终端操作的常用命令,可以让你更好的游刃于iOS繁重的开发 ...
- node入门(三)——gulp运用实例
在上一篇<node入门(二)——gulpfile.js初探>中,我们知道了(看懂入门二及其参考资料)怎么运用gulp来更高效的开发,现在来示范一下. 在package.json里面配置好d ...
- LN : leetcode 733 Flood Fill
lc 733 Flood Fill 733 Flood Fill An image is represented by a 2-D array of integers, each integer re ...
- checkbox:click事件触发文本框显示隐藏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- coursera网站中的VTT字幕的使用
coursera网站中的VTT字幕的使用 1.https://www.coursera.org/learn/os-virtsecurity/lecture/xuWgP/1-3-cao-zuo-xi-t ...
- iOS圆形图片裁剪,原型图片外面加一个圆环
/** * 在圆形外面加一个圆环 */ - (void)yuanHuan{ //0.加载图片 UIImage *image = [UIImage imageNamed:@"AppIcon1 ...
- MyBatis学习(三)
前言 感觉学习进度还是比较慢啊,一整天的学习效率不是很高,一会看电视,一会喝茶,对自己的要求不严格...今天就说说关联表数据的插入以及别名的使用. 正文 1.关联插入 之前,我在数据库中已经创建了一张 ...
- Sql Server 2008R2升级 Sql Server 2012 问题
环境: Windows server 2008 r2 Standard +SqlServer2008R2 内网环境需要升级为SQL server 2012 升级安装时提示版本不支持 网上查询相关问题 ...
- 在DLL中创建窗口时一个值得注意的地方 — UnregisterClass
背景描述: 今天要测试一份注入代码,拿以前写的创建窗口的DLL来做测试. 第一次注入时一切正常,窗口被成功创建并显示,但在第二次加载时窗口没有显示出来. 经过研究发现在第二次加载DLL时Registe ...