Muddy Fields
Description
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 经典构图
#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的更多相关文章
- 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 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- 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矩阵.当中*表示泥土,.表示小草.要你用最少的木板把泥土覆盖. 木板长度不限.可是仅仅能水平和竖直. 行列式二分匹配配 ...
- BNUOJ 2345 Muddy Fields
Muddy Fields Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...
- 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 ...
随机推荐
- python进阶学习(一)
同样是<python基础教程(第二版)>的内容,只是后面内容学起来,相比前面会比较有趣,也更加实用,所以,将"基础"改为"进阶". python 电 ...
- 关于i++和++i
初学Java,经常看到i++和++i的一些问题,在此整理一下. 首先看下面一个小程序: public class atest{ public static void main(String args[ ...
- python3 安装及项目管理安装
python3 一.下载安装 地址:https://www.python.org/downloads/ 安装:傻瓜式安装:我的目录如下 二.环境配置 [右键计算机]-->[属性]-->[高 ...
- java语言基础(变量和运算符)
java八大基本数据类型: 整型{ int(整型) short(短整型) long(长整型)} 浮点型{ float(浮点型) double(双精度)} 布尔{boolean} ...
- diff.js 列表对比算法 源码分析
diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...
- SynchronizedMap和ConcurrentHashMap 区别
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt215 SynchronizedMap和ConcurrentHashMap的深 ...
- tkinter第三章(单选和多选)RadioButton CheckButton
最简单的CheckButton多选类 import tkinter as tk #checkButton的内容,多选 root = tk.Tk() v = tk.IntVar()#装整形变量的 #va ...
- Python中os和shutil模块实用方法集锦
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- 201521123107 《Java程序设计》第3周学习总结
第3周作业-面向对象基本概念 1.本周学习总结 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static ...
- 【Alpha阶段】第四次scrum meeting
一.会议照片 二.会议内容 姓名 学号 负责模块 昨日任务完成度 今日任务 杨爱清 099 界面设计和交互功能 完成 百度合适的背景图片 杨立鑫 100 数据库搭建和其他 完成 开始编辑数据库 林 钊 ...