http://poj.org/problem?id=2226

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7078   Accepted: 2622

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

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.

Source

 
【题解】:
  

题意:给定一个矩阵,其中有一些地方有水,用一些长度任意,宽度为1的木板盖住这些有水的地方,问至少需要几块板子。

分析:二分图最小点集覆盖。二分图建图方法如下,把每段连续的横向的水洼看成是一个X集合中的点,每段连续的纵向的水洼看成是Y集合中的点。矩阵每个有水单元看成是一个边,它连接了它所在的横向水洼在X集合中对应的点和它所在的纵向水洼在Y集合中对应的点。(对于一段连续的水洼,如果要铺木板,一定要铺得尽量长)这样最小点集覆盖的意义就变成了,矩阵中的每个有水的单元(二分图中的每条边)所在的横向和纵向的水洼(在X集合和Y集合中的两个端点)至少有一个被覆盖。求至少需要选几个点。

对这个图分区域
 把行区域的编号和列区域的编号写在两边(相当于二分图的两个点集),在有公共格子的区域内连线
比如2号横区域和4号纵区域(分别在两个图中),第一行第三列的格子有重叠,所以两个连线。
   这样就是在这个二分图中求最小顶点覆盖。
   不知道读者有没有发现,二分图的边数=需要覆盖的格子数,而横一列和纵一列至多相交于一个格子,那么可以理解为,每一条边代表一个格子,然后就是覆盖每一条边,这就成了一个最小顶点覆盖问题。和之前那道题目是类似的。而之前那个题目没有障碍物,显得比较简单,一行一列就是一个区。
   这个理解是把模型建立了以后才这么想,可不知道第一个这么做的人是谁呢?真有才,如果要单纯自己想,而且没有做过类似题目,还真的很难。至于具体还真的感到很玄。
【code】:
 /**
Judge Status:Accepted Memory:36596K
Time:329MS Language:G++
Code Lenght:2165B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm> #define N 55
#define M N*N
using namespace std; int n,ny,nx,m;
int map[M][M];
int cx[M],cy[M],mark[M]; char str[N][N]; int map_x[N][N],map_y[N][N]; int path(int u)
{
int j;
for(j=;j<=ny;j++)
{
if(map[u][j]&&!mark[j])
{
mark[j]=;
if(cy[j]==-||path(cy[j]))
{
cx[u] = j;
cy[j] = u;
return ;
}
}
}
return ;
} int maxMatch() //求最小覆盖
{
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
int i;
int res = ;
for(i=;i<=nx;i++)
{
if(cx[i]==-)
{
memset(mark,,sizeof(mark));
res+=path(i);
}
}
return res;
} void getMap()
{
memset(map,,sizeof(map));
memset(map_x,,sizeof(map_x));
memset(map_y,,sizeof(map_y));
int i,j;
int flag;
nx=ny=;
for(i=;i<n;i++) //横向分区域
{
for(j=;j<m;j++)
{
flag = ;
while(j<m&&str[i][j]=='*')
{
if(!flag) ++nx;
map_x[i][j] = nx;
j++;
flag = ;
}
}
} for(j=;j<m;j++) //纵向分区
{
for(i=;i<n;i++)
{
flag = ;
while(i<n&&str[i][j]=='*')
{
if(!flag) ++ny;
flag = ;
map_y[i][j] = ny;
i++;
}
}
} for(i=;i<n;i++) //横向纵向区域连接
{
for(j=;j<m;j++)
{
int x = map_x[i][j];
int y = map_y[i][j];
if(x&&y)
{
map[x][y] = ; //得到二分map图,然后进行最小覆盖
}
}
}
} int main()
{
int i;
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
{
scanf("%s",str[i]);
}
getMap();
printf("%d\n",maxMatch());
return ;
}

poj 2226 Muddy Fields (转化成二分图的最小覆盖)的更多相关文章

  1. poj 2226 Muddy Fields(水二分图)

    Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 ...

  2. POJ 2226 Muddy Fields(最小顶点覆盖)

    POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...

  3. poj 2226 Muddy Fields(最小覆盖点+构图)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  4. poj 2226 Muddy Fields (二分匹配)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7340   Accepted: 2715 Desc ...

  5. POJ 2226 Muddy Fields (二分图匹配)

    [题目链接] http://poj.org/problem?id=2226 [题目大意] 给出一张图,上面有泥和草地,有泥的地方需要用1*k的木板覆盖, 有草地的地方不希望被覆盖,问在此条件下需要的最 ...

  6. [POJ] 2226 Muddy Fields(二分图最小点覆盖)

    题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

  7. POJ 2226 Muddy Fields 二分图(难点在于建图)

    题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...

  8. poj 2226 Muddy Fields (二分图)

    大意:给定n*m网格, 每个格子为泥地或草地, 可以用一些长度任意宽度为1的木板盖住泥地, 要求不能盖到草地, 求最少要多少块木板能盖住所有泥地. 最小点覆盖板子题, 建图跑最大匹配即可. #incl ...

  9. POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)

    题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...

随机推荐

  1. Laravel_Elixir_gulp任务利器安装

    目录 说明 安装 1安装gulp 2安装Elixir 3Elixir快速入门 4合并cssjs 5版本控制version 6复制copy 7方法串联 1.说明 详细说明暂时省略,后期补充.小白的角度理 ...

  2. webstorm 配置node babel编译es6

  3. PL/SQL Developer连接远程Oracle数据库

    转自:http://zhengdu.net/archives/152 一.首先看远程端oracle服务是否启动 如果没有启动,请启动oracle服务 ps:创建或者删除oracle监听 二.远程端or ...

  4. 【转载】Kafka High Availability

    http://www.haokoo.com/internet/2877400.html Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则 ...

  5. ASP.NET二级域名站点共享Session状态

    我的前面一篇文章提到了如何在使用了ASP.NET form authentication的二级站点之间共享登陆状态, http://www.cnblogs.com/jzywh/archive/2007 ...

  6. 每天一道LeetCode--169.Majority Elemen

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. android结束进程、退出application的方法

    1.finish()方法 finish是Activity的类,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理:调用finish()方 ...

  8. String str 与 String str=new String("") 区别

    1.当使用String str="abc",这种方式时,先去内存的Heap中找是否存在"abc"这个字符串,若存在,则将地址引用.若不存在则创建. 2.当使用S ...

  9. [SWF]在线预览文档下载

    写本文的缘由:领导有些项目文档需要审阅,网站上的文档只能在线预览,没有提供下载.开始用截屏的方式,可想而知这将会是多大的重复性劳动.所以研究了一下,发现可以曲线救国,所以在这里分享一下. 问题描述:这 ...

  10. Unity3D设置字体颜色大小,用于游戏分数显示设置等,

    最近在学unity3d,慢慢的学会了许多unity的东西,今天记录下unity3d的Label字体大小及颜色的代码,下面是显示游戏中分数的代码,, public static int Score = ...