Muddy Fields

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

Submit Status

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

经典构图
 #include <cstdio>
#include <cmath>
#include <queue>
#include <string.h>
#include <iostream>
using namespace std;
int head[],linkx[],vi[],q[];
int dx[],dy[];
struct Edge
{
int to,next;
Edge() {};
Edge(int t,int n):to(t),next(n) {}
};
Edge a[];
int an=,nn;
void AddEdge(int u,int v)
{
a[an]=Edge(v,head[u]);
head[u]=an++;
}
bool bfs()
{
int i,u,v,h,t;
h=t=;
bool flag=;
memset(dx,-,sizeof(dx));
memset(dy,-,sizeof(dy));
for(i=; i<=nn; i++)
if(!vi[i])
q[h++]=i,dx[i]=;
while(h!=t)
{
u=q[t++];
for(i=head[u]; ~i; i=a[i].next)
{
if(dy[v=a[i].to]==-)
{
dy[v]=dx[u]+;
if(linkx[v]==-)flag=;
else
{
dx[linkx[v]]=dy[v]+;
q[h++]=linkx[v];
}
}
}
}
return flag;
}
bool dfs(int u)
{
int v,i;
for(i=head[u]; ~i; i=a[i].next)
{
v=a[i].to;
if(dy[v]==dx[u]+)
{
dy[v]=-;
if(linkx[v]==-||dfs(linkx[v]))
{
linkx[v]=u;
return ;
}
}
}
return ;
}
int MaxMatch()
{
int ans=;
memset(vi,,sizeof(vi));
while(bfs())
for(int i=; i<=nn; i++)
if(!vi[i]&&dfs(i))
vi[i]=,ans++;
return ans;
}
int main()
{
int i,j,m,n;
int x[][];
while(~scanf("%d%d",&n,&m))
{
memset(head,-,sizeof(head));
memset(linkx,-,sizeof(linkx));
an=;
getchar();
for(i=; i<=n; i++)
{
for(j=; j<=m; j++)
{
x[i][j]=getchar();
}
getchar();
}
int now='';
for(i=; i<=n; i++)
{
j=;
while(j<=m)
{
while(j<=m&&x[i][j]!='*')j++;
while(j<=m&&x[i][j]=='*')x[i][j++]=now;
now++;
}
}
nn=now;
now='';
for(i=; i<=m; i++)
{
j=;
while(j<=n)
{
while(j<=n&&x[j][i]=='.')j++;
while(j<=n&&x[j][i]!='.')AddEdge(now-'',(int)(x[j++][i]-''));
now++;
}
}
printf("%d\n",MaxMatch());
}
}

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. bzoj 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 最小点覆盖

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

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

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

  6. poj Muddy Fields

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

  7. BNUOJ 2345 Muddy Fields

    Muddy Fields Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

  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. HTML脚本配置Android自动化测试

    说明   在项目配置完基于robotium框架下的自动化测试用例后发现虽然用代码配置测试用例虽然较为灵活,但是如果编写较为全面的测试用例则必然会消耗大量开人员的精力,并且对于用例的后期维护也是很大一部 ...

  2. python 三级联动

       china_map ={  "华南":{   "广东":["广州市","佛山市","深圳市", ...

  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(86)-日程管理-fullcalendar插件用法

    前言 本文分享fullcalendar用法,最后面提供代码下载 说到日程管理,基于JQuery的插件FullCalendar当之无愧,完整的API稳定和调用方式,非常易于扩展!可以用于系统的个人历程管 ...

  4. NET .NET深入体验和实战精要

    在学习.NET之前要充分理解基础,在这里将基础的知识点一一列举. 万丈高楼平地起 1.命名空间 命名空间是一种特殊的分类机制,他将与一个特定功能集有关的所有类型都分到一起,是.避免类名冲突的一种方式 ...

  5. mac系统webstorm快捷键

    WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为"Web前端开发神器"."最强大的HTML5编辑器". ...

  6. 慎用kill -9,kill -15的作用

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt334 Perl语言专家Randal Schwartz在一篇短文里这样写: n ...

  7. 安装python虚拟环境

    虚拟环境: 之前安装python包的命令: sudo pip3 install 包名包的安装路径:/usr/local/lib/python3.5/dist-packages安装同一个包的不同版本,后 ...

  8. 英语学习APP案例分析

    第一部分 调研, 评测 1.上手体验 界面简洁,有常规的词典翻译功能,针对四六级或考研的人有特别的"单词挑战"模块,以及针对口语训练的"我爱说英语"模块,多功能 ...

  9. 结对编程1---四则运算---GUI

    201421123003 黄建英 201421123004 黄美海 题目描述: 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是Window ...

  10. 201521123007《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 作业参考文件下载 1. 代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.jav ...