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 ...
随机推荐
- BFS HDOJ 1242 Rescue
题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...
- 泛型generic
- HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简
http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...
- Linux在线安装pip和numpy
最近写Python需要用到numpy包 运行pip install numpy就会自动安装 一.因此需要先安装pip 1.如果安装的是Python>=2.7.9或者Python>=3.4, ...
- 代码审查的艺术:Dropbox 的故事
Dropbox 的 iOS 应用中的每一行代码,都是开始于被添加到 Maniphest 中的一个 bug 或者功能任务,Maniphest 是我们的任务管理系统.当一位工程师在上面接受一个任务,那么在 ...
- Java-学完一个月总结(javaSe学习路线)
JavaSe的一个月 第一周 0410 基本数据类型:数据类型的转换:运算符:导入删除项目0411 分支结构if else:switch case ;while0412 do while ;for / ...
- TextView、EditText
1.TextView 显示文本信息 常用属性: layout_width/height 控件的宽/高 width/heigth 文本区域的宽/高 text 显示的文本 textSi ...
- COGS 495. 窗口
★☆ 输入文件:window.in 输出文件:window.out 简单对比时间限制:2 s 内存限制:256 MB [问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左 ...
- Android(java)学习笔记174:服务(service)之混合方式开启服务
1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService: oncreate() ---> onstartCommand() ---& ...
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
A(By talker): 题意分析:以a(int) op b(int)形式给出两个整数和操作符, 求两个整数是否存在操作符所给定的关系 ,有则输出true,无则输出false: 思路:由于无时间复杂 ...