题目: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的更多相关文章

  1. poj2226Muddy Fields——二分图匹配

    题目:http://poj.org/problem?id=2226 把行连通块作为左部点,列连通块作为右部点,行列连通块有相交的格子就连边: 则问题转化为求最小点覆盖,即最大匹配. 代码如下: #in ...

  2. poj2226-Muddy Fields二分匹配 最小顶点覆盖 好题

    题目 给到一个矩阵,有些格子上是草,有些是水.需要用宽度为1,长度任意的若干块木板覆盖所有的水,并不能覆盖草,木板可以交叉,但只能横竖放置,问最少要多少块板. 分析 经典的矩阵二分图构图和最小点覆盖. ...

  3. POJ 2226 Muddy Fields(最小点覆盖)题解

    题意:一片r*c的地,有些地方是泥地,需要铺地板.这些地板宽1,长无限,但只能铺在泥地上不能压到其他地方,问你铺满所有泥地最少几块 思路:我们把一行中连续的泥地看成整体,并把所有横的整体里的点编成一个 ...

  4. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  5. 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP

    [BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...

  6. poj 3254 Corn Fields

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  7. Corn Fields——POJ3254状态压缩Dp

    Corn Fields Time Limit: 2000MS Memory Limit: 65536K Description Farmer John has purchased a lush new ...

  8. 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 ...

  9. (转)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 ...

随机推荐

  1. python2.7无法安装python-ldap、django-auth-ldap

    1.安装报错信息: error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27 或者fatal ...

  2. 20145303 刘俊谦《网络对抗》逆向及BOF基础实践

    20145303 刘俊谦<网络对抗>逆向及BOF基础实践 1 逆向及Bof基础实践说明 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调 ...

  3. 20145211MSF基础应用实验

    20145211MSF基础应用实验 一.实验博客 ms08_067攻击实验 http://www.cnblogs.com/entropy/p/6690301.html ms12_004漏洞攻击 htt ...

  4. HttpContext.Current and Web Api

    Using HttpContext.Current in WebApi is dangerous because of async HttpContext.Current gets the curre ...

  5. PostMessage与SendMessage的区别

    PostMessage只负责将消息放到消息队列中,不确定何时及是否处理 SendMessage要等到受到消息处理的返回码(DWord类型)后才继续 PostMessage执行后马上返回 SendMes ...

  6. python 复数的数学四则运算

    print(+3j)+(-7j)) print(+3j)-(-7j)) print(+3j)*(-7j)) print(+3j)/(-7j))

  7. 详解java中CAS机制所导致的问题以及解决——内存顺序冲突

    [CAS机制] 指的是CompareAndSwap或CompareAndSet,是一个原子操作,实现此机制的原子类记录着当前值的在内存中存储的偏移地址,将内存中的真实值V与旧的预期值A做比较,如果不一 ...

  8. JSP 文件上传

    JSP 文件上传 JSP可以通过HTML的form表单上传文件到服务器. 文件类型可以是文本文件.二进制文件.图像文件等其他任何文档. 创建文件上传表单 接下来我们使用HTML标签来创建文件上传表单, ...

  9. SpringMVC中的参数绑定总结

    众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在这一篇博文中,我将总结一下springm ...

  10. 直方图及low_value、high_value

    直方图 Histogram是一种特殊的列统计信息,详细描述了目标列的数据分布情况.存储在数据字典基表 histogram$; 专门为了准确评估分布不均匀的目标列的可选择率.结果集的cardianlit ...