POJ2226Muddy Fields
题目:http://poj.org/problem?id=2226
巧妙建图:以行或列上的联通块作为点,每个泥格子作为边,求最小点覆盖就可以了!
于是用匈牙利算法找最大匹配。注意要对右部点记录每一个左部点的vis!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int n=,N=((n*n)<<);
int r,c,head[N],xnt=,stack[n],top,cnt,col[n][n],pre[N],knt;
char ch[n][n];
bool vis[N];
struct Edge{
int next,to;
Edge(int n=,int t=,int c=):next(n),to(t) {}
}edge[N];
bool check(int cur)
{
for(int i=head[cur],v;i;i=edge[i].next)
if(!vis[v=edge[i].to])
{
vis[v]=;
if(!pre[v=edge[i].to]||check(pre[v]))
{pre[v]=cur;/*printf("cur=%d v=%d\n",cur,v);*/return true;}
}
return false;
}
void cz1(int i)
{
cnt++;
while(ch[i][stack[top]]=='*')col[i][stack[top--]]=cnt;
}
void cz2(int i)
{
cnt++;
while(ch[stack[top]][i]=='*')
{
edge[++xnt]=Edge(head[col[stack[top]][i]],cnt);
// printf("%d %d\n",col[stack[top]][i],cnt);
head[col[stack[top--]][i]]=xnt;
}
}
int main()
{
scanf("%d%d",&r,&c);
for(int i=;i<=r;i++)
{
getchar();
for(int j=;j<=c;j++)
{
scanf("%c",&ch[i][j]);
if(ch[i][j]=='*')stack[++top]=j;
else if(top)cz1(i);
}
if(top)cz1(i);
top=;
}
// for(int i=1;i<=r;i++)
// {
// for(int j=1;j<=c;j++)
// printf("%d",col[i][j]);printf("\n");
// }
int lm=cnt;
for(int i=;i<=c;i++)
{
for(int j=;j<=r;j++)
{
if(ch[j][i]=='*')stack[++top]=j;
else if(top)cz2(i);
}
if(top)cz2(i);
top=;
}
for(int i=;i<=lm;i++)
{
memset(vis,,sizeof vis);//万一同一次踩在同一个未匹配右部点的话
if(check(i))knt++; //右边不会主动匹配左边 ,这个i一定未匹配
}
printf("%d",knt);
return ;
}
POJ2226Muddy Fields的更多相关文章
- poj2226Muddy Fields——二分图匹配
题目:http://poj.org/problem?id=2226 把行连通块作为左部点,列连通块作为右部点,行列连通块有相交的格子就连边: 则问题转化为求最小点覆盖,即最大匹配. 代码如下: #in ...
- poj2226-Muddy Fields二分匹配 最小顶点覆盖 好题
题目 给到一个矩阵,有些格子上是草,有些是水.需要用宽度为1,长度任意的若干块木板覆盖所有的水,并不能覆盖草,木板可以交叉,但只能横竖放置,问最少要多少块板. 分析 经典的矩阵二分图构图和最小点覆盖. ...
- POJ 2226 Muddy Fields(最小点覆盖)题解
题意:一片r*c的地,有些地方是泥地,需要铺地板.这些地板宽1,长无限,但只能铺在泥地上不能压到其他地方,问你铺满所有泥地最少几块 思路:我们把一行中连续的泥地看成整体,并把所有横的整体里的点编成一个 ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- Corn Fields——POJ3254状态压缩Dp
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Description Farmer John has purchased a lush new ...
- CLR via C# 3rd - 07 - Constants and Fields
1. Constants A constant is a symbol that has a never-changing value. When defining a constant ...
- (转)Image Segmentation with Tensorflow using CNNs and Conditional Random Fields
Daniil's blog Machine Learning and Computer Vision artisan. About/ Blog/ Image Segmentation with Ten ...
随机推荐
- 微信小程序:工具配置 project.config.json
微信小程序:工具配置 project.config.json 一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.js ...
- WebCollector2.7爬虫框架——在Eclipse项目中配置
WebCollector2.7爬虫框架——在Eclipse项目中配置 在Eclipse项目中使用WebCollector爬虫非常简单,不需要任何其他的配置,只需要导入相关的jar包即可. Netbea ...
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Educational Codeforces Round 17 C. Two strings 打表二分
C. Two strings time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 51nod 1225 余数的和 数学
1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...
- 使用 Redis
Redis(https://redis.io/),既不像 SQLite 以表的形式存储数据,也不像 MongoDB 允许以嵌套结构存储和查询,它是一种内存数据库结构,即将数据缓存在内存中.它将键—值( ...
- OpenGL超级宝典笔记——遮挡查询 [转]
目录[-] 遮挡查询之前 包围体 遮挡查询 在一个场景中,如果有有些物体被其他物体遮住了不可见.那么我们就不需要绘制它.在复杂的场景中,这可以减少大量的顶点和像素的处理,大幅度的提高帧率.遮挡查询就是 ...
- python学习笔记(日志系统实现)
博主今天在自己的接口自动化框架中添加了日志系统 基于python自带的logging库.包括日志主函数.生成日志文件: # -*- coding: utf-8 -*- # 日志系统 # 时间:2017 ...
- SpringBoot全局异常处理方式
每个项目全局异常处理非常重要, 今天在处理项目架构的时候添加了一个全局异常处理. 大概三种异常情况: 一:在进入Controller之前,譬如请求一个不存在的地址,404错误. 二:在执行@Reque ...
- 微信小程序-TabBar功能实现
要实现tabbar的导航条其实很简单,我们要实现全局的tabbar只需要在app.json文件中定义即可,局部的就在局部的tabbar文件中实现. 来看看app.json代码: { "pag ...