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. 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序

    使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻 ...

  2. ref与out的区别、冒泡排序、普通排序,以及二分法查询

    一.首先我们先讲一下ref与out的区别和使用方法: 1.ref与out的区别: out:需要在使用前声明变量,分配地址但不能赋值,但是需要在使用中的时候需要初始化(进入方法体中的时候需要先赋值在使用 ...

  3. 微信网页授权封装接口——node.js版

    Wechat 网页授权 授权url:(请在微信客户端中打开此链接体验) xxx为config.js中的WECHAT_DOMAIN 1.scope为snsapi_base xxx/?route=auth ...

  4. 【grunt】两小时入门

    目录: 1. 用途和场景 2.Grunt插件 3.相关资源 4.环境安装 5.开始学习 5.1 一个新项目 5.2 生成package.json 5.3 在项目中安装grunt和相关插件 5.4 Gr ...

  5. C# 委托、匿名方法、lambda简介

    在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个First不 ...

  6. tkinter第二章(添加图片,背景图片)

    #插入文件图片import tkinter as tk root = tk.Tk() #创建一个标签类, [justify]:对齐方式textLabel = tk.Label(root,text=&q ...

  7. CentOS7中安装MySQL5.7 (转)

    安装必要的组件 yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio li ...

  8. 201521123001《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1) 1.1 实验总结 答: 在老师的详细 ...

  9. 201521123104《Java程序设计》第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: pu ...

  10. 201521123015 《Java程序设计》第七周学习总结

    1. 本周学习总结 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: public boolean contains(Object ...