POJ 2226 Muddy Fields

题目链接

题意:给定一个图,要求用纸片去覆盖'*'的位置。纸片能够重叠。可是不能放到'.'的位置,为最少须要几个纸片

思路:二分图匹配求最小点覆盖。和放车那题基本一样。就是注意要预处理一下行列,把连续横的'*'当成一行,竖的'*'当成一列,建图跑最小点覆盖就可以

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; const int N = 55;
const int M = 1505; int n, m, tox[N][N], toy[N][N], xn, yn;
char str[N][N];
vector<int> g[M]; int left[M], vis[M]; bool dfs(int u) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (vis[v]) continue;
vis[v] = 1;
if (left[v] == -1 || dfs(left[v])) {
left[v] = u;
return true;
}
}
return false;
} int hungary() {
int ans = 0;
memset(left, -1, sizeof(left));
for (int i = 0; i < xn; i++) {
memset(vis, 0, sizeof(vis));
if (dfs(i)) ans++;
}
return ans;
} int main() {
while (~scanf("%d%d", &n, &m)) {
int cnt = 0;
for (int i = 0; i < n; i++) {
scanf("%s", str[i]);
int flag = 0;
for (int j = 0; j < m; j++) {
if (str[i][j] == '*') {
tox[i][j] = cnt;
flag = 1;
} else if (str[i][j] == '.' && flag) {
g[cnt].clear();
cnt++;
flag = 0;
}
}
if (flag) {
g[cnt].clear();
cnt++;
}
xn = cnt;
}
cnt = 0;
for (int i = 0; i < m; i++) {
int flag = 0;
for (int j = 0; j < n; j++) {
if (str[j][i] == '*') {
toy[j][i] = cnt;
flag = 1;
} else if (str[j][i] == '.' && flag) {
cnt++;
flag = 0;
}
}
if (flag)
cnt++;
yn = cnt;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (str[i][j] == '*') {
g[tox[i][j]].push_back(toy[i][j]);
}
}
}
printf("%d\n", hungary());
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 2226 Muddy Fields(最小顶点覆盖)的更多相关文章

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

    *.*. .*** ***. ..*. 题意:有一个N*M的像素图,现在问最少能用几块1*k的木条覆盖所有的 * 点,k为>=1的任意值. 分析:和小行星那题很像.小行星那题是将一整行(列)看作 ...

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

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

  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(最小覆盖点+构图)

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. MySQL 全角转换为半角

    ​序言:       用户注冊时候,录入了全角手机号码,所以导致短信系统依据手机字段发送短信失败.如今问题来了,怎样把全角手机号码变成半角手机号码? 1.手机号码全角转换成半角先查询出来全角半角都存在 ...

  2. SRM 219 Div II Level One: WaiterTipping,小心约分

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...

  3. A Game of Thrones(4) - Eddard

    The visitors poured(倾泻:流出) through the castle gates in a river of gold and silver and polished steel ...

  4. Linux C语言写的超级简单port扫描器

    这个本来曾经也写过的,今天无聊复习下 再写一遍.简单的一塌糊涂,写的不咋地大家见谅哦!有空再加强 嘿嘿! #include <stdio.h> #include <stdlib.h& ...

  5. 关于多线程的一个例子(UI实时显示)

    在开发Window应用程序的时候,经常需要在界面上显示出已经执行到什么步骤了,拿一个简单例子来说,创建一个Winform程序,在窗体上访一个Button和一个Label,点击Button时做100次循 ...

  6. org.jsoup.select.Selector

    org.jsoup.select.Selector CSS-like element selector, that finds elements matching a query. Selector ...

  7. crm工作机会实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages; public class Opport ...

  8. hdu1664 Different Digits

    求出n的倍数m,要求m使用的不同数字最少,且最小. 一开始不知道怎么搜,因为不知道m由多少个不同的数字组成. 然后百度了一下,看到和数论有关. m可能使用的数字的个数可能为一个或者两个 a,aa,aa ...

  9. POJ培训计划2253_Frogger(最短/floyd)

    解决报告 意甲冠军: 乞讨0至1所有最大的道路值的最小数量. 思维: floyd. #include <iostream> #include <cstdio> #include ...

  10. Python多线程的threading Event

    Python threading模块提供Event对象用于线程间通信.它提供了一组.拆除.等待用于线程间通信的其他方法. event它是沟通中最简单的一个过程之中,一个线程产生一个信号,号.Pytho ...