Muddy Fields

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2226
64-bit integer IO format: %lld      Java class name: Main

 
Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don't want to get their hooves dirty while they eat.

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

* Line 1: Two space-separated integers: R and C

* 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

* Line 1: A single integer representing the number of boards FJ needs.

 

Sample Input

4 4
*.*.
.***
***.
..*.

Sample Output

4

Hint

OUTPUT DETAILS:

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的更多相关文章

  1. poj 2226 Muddy Fields(最小覆盖点+构图)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  2. poj 2226 Muddy Fields (转化成二分图的最小覆盖)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  3. POJ 2226 Muddy Fields(最小顶点覆盖)

    POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...

  4. Muddy Fields

     Muddy Fields Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...

  5. bzoj 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 最小点覆盖

    链接 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 思路 这就是个上一篇的稍微麻烦版(是变脸版,其实没麻烦) 用边长为1的模板覆盖地图上的没有长草的土地,不能覆盖草地 ...

  6. poj 2226 Muddy Fields (二分匹配)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7340   Accepted: 2715 Desc ...

  7. poj Muddy Fields

    Muddy Fields 原题去我创的专题里找,在文件夹首页. 题目: 给出N*M矩阵.当中*表示泥土,.表示小草.要你用最少的木板把泥土覆盖. 木板长度不限.可是仅仅能水平和竖直. 行列式二分匹配配 ...

  8. POJ Muddy Fields 泥泞的牧场 二分图

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13235   Accepted: 4879 汪星人 ...

  9. POJ2226 Muddy Fields

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10149   Accepted: 3783 Description Rain ...

随机推荐

  1. h5-29-WEB存储-通讯录实战.html

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法

    最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...

  3. PHP多图片上传类推荐

    多文件上传是PHP中的一个基础应用,反正PHPer都会遇到的问题,现在就介绍一个功能完善.强大的多文件上传类给大家吧,能用上这个类的地方会很多. <?php class Upload{ var ...

  4. 消息中间件与RPC的区别

    消息中间件和消息通信与RPC各自具有怎样的优势,如何互补消息中间件主要实现的是异步.弹性消息以及队列,弹性消息有时可以借助于外存从而一定程度上可以实现峰值缓存,有效均衡服务器端压力,同时消息可以进行一 ...

  5. .netcore中使用EFCore连接SQL Server并部署至Ubuntu

    前面一篇记录了如何在windows下开发asp.net core程序,并部署至ubuntu系统中.但仅仅是建立了一个demo项目,项目本身并没有实现多少功能.多数时候,我们的项目是要和数据库打交道.E ...

  6. 11G RAC环境数据库启动和关闭

    一步启动Oracle (1) 启动整个集群 # ./crsctl start cluster -all -all选项启动整个集群. 不加-all选项只能启动本节点的服务. (2) 启动本节点集群 以下 ...

  7. type和isinstance区别

    type只能对类型做直接的判断,主要用于获取未知变量的类型. 在程序中几乎很少用到type. 而isinstance功能比type更强,可以对子类型做出推理判断. isinstance主要用于判断A是 ...

  8. HDU 5778 abs (暴力枚举)

    abs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem De ...

  9. mysql 函数tree状

    // 子节点的查询 CREATE DEFINER = `root`@`%` FUNCTION `getDeptChildList`(rootId BIGINT) RETURNS longtext DE ...

  10. 7-Java-C(冰雹数)

    题目描述: 任意给定一个正整数N, 如果是偶数,执行: N / 2 如果是奇数,执行: N * 3 + 1 生成的新的数字再执行同样的动作,循环往复. 通过观察发现,这个数字会一会儿上升到很高, 一会 ...