O - Muddy Fields
来源poj2226
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.
有点像小行星,但是它值能覆盖连续的泥池,所以要构图,将横着放的板子和纵放的板子编号,然后连起来算最小点覆盖
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1017;
int pre[N];
int visit[N],line[N][N],line1[N][N],line2[N][N];
char Map[N][N];
int n,m,y,x;
int cas,cas1;
bool find(int x)
{
rep(i,1,cas1+1)
{
if(line[x][i]&&visit[i]==0)
{
visit[i]=1;
if(pre[i]==0||find(pre[i]))
{
pre[i]=x;
return true;
}
}
}
return false;
}
void deal()
{
cas=0;
rep(i,1,n+1)
{
rep(j,1,m+1)
{
if(Map[i][j]=='*')
{
cas++;
while(j<=m&&Map[i][j]=='*')
{
line1[i][j]=cas;
j++;
}
}
}
}
cas1=0;
rep(j,1,m+1)
{
rep(i,1,n+1)
{
if(Map[i][j]=='*')
{
cas1++;
while(i<=n&&Map[i][j]=='*')
{
line2[i][j]=cas1;
i++;
}
}
}
}
rep(i,1,n+1)
rep(j,1,m+1)
if(Map[i][j]=='*')
line[line1[i][j]][line2[i][j]]=1;
}
int main()
{
while(~sf("%d%d",&n,&m))
{
mm(Map,'0');
mm(line,0);
mm(line2,0);
mm(line1,0);
mm(pre,0);
for(int i = 1; i <=n; i++)
{
for(int j = 1; j <= m; j++)
{
cin>>Map[i][j];
}
}
deal();
int ans=0;
rep(i,1,cas+1)
{
mm(visit,0);
if(find(i))
ans++;
}
pf("%d\n",ans);
}
return 0;
}
O - 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 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- Muddy Fields
Muddy Fields Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- 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 ...
随机推荐
- JSOUP 乱码处理
JSOUP 支持在请求的时候,传入URL 对象,然后设置编码.如下方式才是正解,设置编码为GBK . doc = Jsoup.parse(new URL(url).openStream(), &qu ...
- java.lang.NumberFormatException: multiple points错误问题
最近项目一直会出现时间转换报错,一直不知道是什么问题??? java.lang.NumberFormatException: multiple points at sun.misc.Float ...
- vim8.0模式详解
pattern pattern.txt For Vim version 8.0. 最近更新: 2017年8月 VIM 参考手册 by Bram Moolenaar 译者: lang2 http://v ...
- Spark2.3(四十二):Spark Streaming和Spark Structured Streaming更新broadcast总结(二)
本次此时是在SPARK2,3 structured streaming下测试,不过这种方案,在spark2.2 structured streaming下应该也可行(请自行测试).以下是我测试结果: ...
- C# 调用线程并行上下文穿透-ILogicalThreadAffinative+CallContext
na mespace System.Runtime.Remoting.Messaging { /// <summary>Provides a set of properties that ...
- [转]The Production Environment at Google
A brief tour of some of the important components of a Google Datacenter. A photo of the interior o ...
- 30天自制操作系统 - 来一个hello world
helloos.nas 源码: ; hello-os ; TAB= ; 以下这段是标准的FAT12格式软盘专用代码 DB 0xeb, 0x4e, 0x90 DB "HELLOIPL" ...
- 生产环境CPU过高问题定位
问题描述: 生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 解决过程: 1.根据top命令,发现 ...
- Introducing DataFrames in Apache Spark for Large Scale Data Science(中英双语)
文章标题 Introducing DataFrames in Apache Spark for Large Scale Data Science 一个用于大规模数据科学的API——DataFrame ...
- 当使用 SelfHost 的 OWIN 承载 SignalR 时,重启 OWIN 后,SignalR 不能正常工作
需要在初始化时,重新对 Resolver 赋值一个新的实例: public class Startup { public void Configuration(IAppBuilder app) { v ...