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 ...
随机推荐
- freertos之特点
主要特点:协程(co-routine):任务间的中断通信机制 支持可抢占式/协作式任务调度 .FreeRTOS-MPU 内核对象可以动态或静态分配 ...
- js修改物理返回键功能
preventBack: function(theurl){ var pushState = window.history.pushState; //点击物理返回键时,退出到跳转定义首页 if(pus ...
- Java断点续传(基于socket与RandomAccessFile的简单实现)
Java断点续传(基于socket与RandomAccessFile的简单实现) 这是一个简单的C/S架构,基本实现思路是将服务器注册至某个空闲端口用来监视并处理每个客户端的传输请求. 客户端先获得用 ...
- UVa OJ 494
Kindergarten Counting Game Everybody sit down in a circle. Ok. Listen to me carefully. ``Woooooo, ...
- ThreadPoolExecutor 线程池
TestThreadPoolExecutorMain package core.test.threadpool; import java.util.concurrent.ArrayBlockingQu ...
- Ubuntu系统下配置PHP支持SQLServer 2005
最近在做一个项目,该项目的数据库是微软公司的的SQLserver ,数据库安装在另一台windows服务器上,而项目却部署在ubuntu server上.那么这样就会涉及到项目在linux上如何链接S ...
- checkbox设置复选框的只读效果不让用户勾选
在Web开发中,有时候需要显示一些复选框(checkbox),表明这个地方是可以进行勾选操作的,但是有时候是只想告知用户"这个地方是可以进行勾选操作的"而不想让用户在此处勾选(比如 ...
- Angular JS中变量定义的基本原则
在Angular JS开发中,经常需要定义一些变量,关于这些变量的定义方法及作用域应该注意以下几点: 1. 如果能用局部变量解决问题,尽量不要用全局变量. 2. 需要与界面双向绑定的变量采用$scop ...
- 腾讯AI开放平台的接口调用指南
最近无意发现腾讯AI开放平台上提供了大量好玩的人工智能云服务,而且是完全免费的.只需要用QQ号登录即可.这么好的东西,作为一个程序员,当然要试试了! 从上图可以看出腾讯AI开放平台提供的人工智能服务主 ...
- [SQL]连续三天有销售额
店铺 销售日期 销售额 A 2017-10-11 300 A 2017-10-12 200 B 2017-10-11 400 B 2017-10-12 200 A 2017-10-13 100 A 2 ...