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. CFF前端沙龙总结

    一. -OOCSS + Sass ——大漠 1. OOCSS 结构<=>皮肤 分离 容器<=>内容 分离 2. Sass 工具.处理器 SCSS(CSS风格)<=> ...

  2. ireport制作小技巧

    ireport制作小技巧 首先ireport中大小写问题: 1.parameter中如果小写,引用也小写 2.$F{},一般都大写 3.子报表中引用父报表中查询出来的值时,只需要小写即可,即在子报表的 ...

  3. Android中使用广播机制退出多个Activity

    谷歌百度一下,Android中退出多个Activity的方法,大家讨论的很多. 在实习的时候,看到公司的项目退出多个Activity,是采用LinkedList方法,毕业设计的时候,也参照了那种方法. ...

  4. System.exit(0)

    表示程序正常退出 System.exit(status) 当status非0时,表示程序为非正常退出. status=0, 关闭当前正在运行的虚拟机. 求质因数分解的程序如下: 两种算法: 一种是用S ...

  5. Drupal 7.31版本爆严重SQL注入漏洞

    今早有国外安全研究人员在Twitter上曝出了Drupal 7.31版本的最新SQL注入漏洞,并给出了利用测试的EXP代码. 在本地搭建Drupal7.31的环境,经过测试,发现该利用代码可成功执行并 ...

  6. 设置Android默认锁定屏幕旋转

    /********************************************************************************** * 设置Android默认锁定屏 ...

  7. .Net中的各种序列化

    我们知道将对象的状态保持在存储媒体中,以便可以在以后重新创建精确的副本这正是数据持久化所要做的.而且,不同应用程序之间的通讯需要相互传输数据.那么序列化和反序列化正是为此而生. 序列化和反序列化 所谓 ...

  8. Zend Framework 入门(3)—错误处理

    undefined 说明脚本中一些对象没有设定 你应在在使用该对象前进行判断 例如: if(typeof(aobject) == "undefined"){   //错误处理 }

  9. MBR与GRUB简介

    在坛子里找到一篇关于grub和mbr工作原理的文章,以前一直都是一头雾水,今天转这文章学习下..哈.. 能正常工作的grub应该包 括一下文件:stage1.stage2.*stage1_5.menu ...

  10. .NET Remoting

    .NET Remoting   .NET Remoting是微软早期的分布式通信技术,虽然微软后来通过WCF通用基础通信框架整合掉了,但是通过回顾学习Remoting,反过来学习理解WCF也是很有帮助 ...