Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8881   Accepted: 3300

Description

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

 
题目意思:
一个n*m的图,其中'.'表示平地,'*'水池。现用一些宽为1个单位,长度不限的木板覆盖所有的水池(某个水池可以被覆盖多次)。问所用木板最少为多少。
 
思路:
每个水池可以被横着覆盖也可以被竖着覆盖,那么题目就成为选用一些覆盖策略使得所有点被横着覆盖或者被竖着覆盖,而某些点被横着或竖着覆盖可以影响与其横着连续的点或竖着连续的点。那么横着处理一下图如同题目中Hint中,同理竖着处理一下图。每个点有两个数字,一个是横着状态数字,另一个是竖着状态数字,连边后建图,就是最小点覆盖了。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 50 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n, m;
vector<int>ve[N*N];
int from[N*N];
bool visited[N*N];
char map[N][N];
int map1[N][N];
int map2[N][N]; int march(int u){
int i, v;
for(i=;i<ve[u].size();i++){
v=ve[u][i];
if(!visited[v]){
visited[v]=true;
if(from[v]==-||march(from[v])){
from[v]=u;
return ;
}
}
}
return ;
} main()
{
int i, j, k;
while(scanf("%d %d",&n,&m)==){
for(i=;i<n;i++) scanf("%s",map[i]);
memset(map1,-,sizeof(map1));
memset(map2,-,sizeof(map2));
int num=;
int maxh=;
//横着处理
for(i=;i<n;i++){
for(j=;j<m;j++){
if(map[i][j]=='*'){
if(j==) map1[i][j]=num;
else{
if(map[i][j-]=='*') map1[i][j]=map1[i][j-];
else map1[i][j]=num;
}
}
else {
if(j<m-&&map[i][j+]=='*') num++;
}
}
num++;
maxh=max(maxh,num);
}
//竖着处理
num=;
for(j=;j<m;j++){
for(i=;i<n;i++){
if(map[i][j]=='*'){
if(i==) map2[i][j]=num;
else{
if(map[i-][j]=='*') map2[i][j]=map2[i-][j];
else map2[i][j]=num;
}
}
else{
if(i<n-&&map[i+][j]=='*') num++;
}
}
num++;
maxh=max(maxh,num);
}
//建二分图
for(i=;i<maxh;i++) ve[i].clear();
for(i=;i<n;i++){
for(j=;j<m;j++){
if(map1[i][j]!=-&&map2[i][j]!=-){
ve[map1[i][j]].push_back(map2[i][j]);
}
}
}
//二分匹配
memset(from,-,sizeof(from));
num=;
for(i=;i<maxh;i++){
memset(visited,false,sizeof(visited));
if(march(i)) num++;
}
printf("%d\n",num);
}
}

POJ 2226 最小点覆盖(经典建图)的更多相关文章

  1. poj 2226 Muddy Fields(合理建图+二分匹配)

    /* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...

  2. hdoj--5093--Battle ships(二分图经典建图)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  3. poj--1149--PIGS(最大流经典建图)

    PIGS Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  4. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  5. HDU 3820 Golden Eggs( 最小割 奇特建图)经典

    Golden Eggs Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  7. POJ 2446 最小点覆盖

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14787   Accepted: 4607 Descr ...

  8. zoj 3460 Missile【经典建图&&二分】

    Missile Time Limit: 2 Seconds      Memory Limit: 65536 KB You control N missile launching towers. Ev ...

  9. hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)

    Problem Description Thanks to a certain "green" resources company, there is a new profitab ...

随机推荐

  1. Java语言的特点

    一. 面向对象:其实是现实世界模型的自然延伸.现实世界中任何实体都可以看作是对象.对象之间通过消息相互作用.另外,现实世界中任何实体都可归属于某类事物,任何对象都是某一类事物的实例.如果说传统的过程式 ...

  2. AJAX-----03远古时期的ajax

    用iframe方法实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  3. 图片按钮来代替文件上传控件(Freemaker,JQuery,HTML,CSS,JavaScript)

    CSS样式: <style type="text/css"> .fileInputContainer{ height:70px; width:95px; positio ...

  4. wex5 教程 之 图文讲解 后台管理界面设计与技巧

    视频教程地址:http://v.youku.com/v_show/id_XMTgwOTAyMTkyMA==.html?from=s1.8-1-1.2&spm=a2h0k.8191407.0.0 ...

  5. Dynamics AX 2012 R2 为运行失败的批处理任务设置预警

    我们主要有两种类型的系统监视:环境健康监视和性能监视. 环境健康监视一般对系统性能影响非常小,是为了提醒潜在的问题. 性能监视通常更有侵入性.监视期间,添加一个负载到环境.因此,它可以回答特定的问题或 ...

  6. QT开发编译问题备忘

    编译<Qt及Qt Quick开发实战精解> 的代码,编译出错,提示: Cannot find file: E:\学习资料\QT\<Qt及Qt Quick开发实战精解>代码\sr ...

  7. Locality Sensitive Hash 局部敏感哈希

    Locality Sensitive Hash是一种常见的用于处理高维向量的索引办法.与其它基于Tree的数据结构,诸如KD-Tree.SR-Tree相比,它较好地克服了Curse of Dimens ...

  8. [问题2014A02] 解答二(求和法+拆分法,由张诚纯同学提供)

    [问题2014A02] 解答二(求和法+拆分法,由张诚纯同学提供) 将行列式 \(|A|\) 的第二列,\(\cdots\),第 \(n\) 列全部加到第一列,可得 \[ |A|=\begin{vma ...

  9. CSS布局基础之一设备像素,设备独立像素,设备像素比,css像素之间的关系

    设备像素dp(device pixels) ppi(pixels per inch)表示每英寸所拥有的像素(pixel)数目,数值越高,代表屏幕能以更高的密度显示图像. 计算公式:ppi=像素数量/物 ...

  10. WebSocket实战之————GatewayWorker使用笔记例子

    参考文档:http://www.workerman.net/gatewaydoc/ 目录结构 ├── Applications // 这里是所有开发者应用项目 │ └── YourApp // 其中一 ...