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

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9022   Accepted: 3348

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.
 
题目大意:用宽度为1长度不限的木板将水洼‘*’盖住而不盖住草‘.'
 
这道题,构图确实比比较巧妙,如果有连续的水洼,假设是横排的,那么这几个连续的水洼可以拿一个板子来覆盖,同样,如果竖排也有连续的水洼,那么也可以拿一个板子来覆盖。这样,当一个水洼既可以拿横着的板子,也可以拿竖着的板子覆盖时,就是相交了。那么这个交线就代表了一个水洼,它既可以被横着盖,也可以被竖着盖。现在我们把所有横排的水洼作为1个水洼需要1个木板,竖着的也同理
 
有两种覆盖的方法,一种为横着盖一种为竖着盖覆盖后可转化为下图形式
横着盖:(图中数字表示用的是第几块木板)
1.2.
.333
444.
. . 5.
将这些点加入到X序列中
 
竖着盖:
1.2.
.324
532.
. . 2.
将这些点加入到Y序列中
 
将图中水洼进行编号一共有九个水洼
1.2.
.345
678.
. . 9.
 
9号水洼(5, 2)表示九号水洼可由横着的5号木板覆盖也可以由竖着的2号木板覆盖,5号木板和2号木板之间就有一条线
这样就组成一个二分图
X        Y
1        1
2        2
3        3
4        4
5        5
用最少的点把所有的边连起来就是最小覆盖点
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define N 1100 int G[N][N], vis[N], used[N];
char maps[N][N];
int m, n, x, y; bool Find(int u)
{
int i;
for(i = ; i <= y ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
} void Build()//构图
{
int i, j, a[N][N] , b[N][N];
x = y = ;
memset(a, , sizeof(a));
memset(b, , sizeof(b));
for(i = ; i <= m ; i++)
{
for(j = ; j <= n ; j++)
{
if(maps[i][j] == '*')
{
if(maps[i][j - ] == '*')
a[i][j] = a[i][j - ];
else
a[i][j] = ++x;
}
}
}//木板横着放
for(i = ; i <= m ; i++)
{
for(j = ; j <= n ; j++)
{
if(maps[i][j] == '*')
{
if(maps[i - ][j] == '*')
b[i][j] = b[i - ][j];
else
b[i][j] = ++y;
G[a[i][j]][b[i][j]] = ;
}
}
}//木板竖着放
} int main()
{
int i, j, ans;
while(~scanf("%d%d", &m, &n))
{
ans = ;
memset(G, , sizeof(G));
for(i = ; i <= m ; i++)
{
getchar();
for(j = ; j <= n ; j++)
{
scanf("%c", &maps[i][j]);
}
}
Build();
memset(used, , sizeof(used));
for(i = ; i <= x ; i++)//X集合中的点与Y集合中的点找最大匹配
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", ans);
}
return ;
}

poj 2226 Muddy Fields(最小覆盖点+构图)的更多相关文章

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

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

  2. poj 2226 Muddy Fields (转化成二分图的最小覆盖)

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

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

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

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

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

  5. poj 2226 Muddy Fields(最小点覆盖+巧妙构图)

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

  6. POJ 2226 Muddy Fields(二分匹配 巧妙的建图)

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

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

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

  8. poj 2226 Muddy Fields(合理建图+二分匹配)

    /* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...

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

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

随机推荐

  1. BZOJ2226: [Spoj 5971] LCMSum

    题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...

  2. Java Web编程的主要组件技术——Struts核心组件

    参考书籍:<J2EE开源编程精要15讲> Struts配置文件struts-config.xml Struts核心文件,可配置各种组件,包括Form Beans.Actions.Actio ...

  3. AI 状态机

    by AKara 2010-11-11 @ http://blog.csdn.net/akara @ akarachen(at)gmail.com @weibo.com/akaras 一个简单横版游戏 ...

  4. phpDoc 注释案例说明

    <?php /** * start page for webaccess * * PHP version 5 * * @category PHP * @package PSI_Web * @au ...

  5. 《C++ Primer 4th》读书笔记 第12章-类

    原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936473.html

  6. 是时候学习Android分屏开发了

    今年Google发布了Android N,Android N新增了不少功能,最受关注的自然就是分屏了. 这一功能对国内的很多手机用户并不陌生,其实很多第三方系统早已经实现了这一功能,如EMUI,Fly ...

  7. Delphi 编译错误信息表

    ; not allowed before ELSE ElSE前不允许有“;” <clause> clause not allowed in OLE automation section 在 ...

  8. hisi平台mii网络模式和rmii网络模式的uboot制作

    MII网络uboot编译说明 一:编译生成默认的uboot1. 进入到uboot目录a. cd /home/satan/Hi3518_SDK_V1.0.7.0/osdrv/uboot2. 新建临时文件 ...

  9. 使用SQL语句清空数据库所有表的数据

    使用SQL语句清空数据库所有表的数据 近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进 ...

  10. Oracle中错误代码ORA-02292 违反了完整性约束条件解决

    百度处理: A表被B表引用,删除A表的时候提示ORA-02292,A表的主键被引用了,虽然已经把B表的数据全部删除掉,但仍然删除不了A表的数据.解决办法: 用禁用约束语句把A表的主键约束给禁用掉.1. ...