(最小点覆盖) poj 2226

H - Muddy Fields

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 2226

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.

建图是关键

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
char s[55][55];
int n,m,t1,t2,g1[55][55],g2[55][55],mp[1010][1010],ans,link[1010],mark[1010];
void init()
{
    t1=0,t2=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            while(s[i][j]==‘*‘)
            {
                g1[i][j]=t1;
                j++;
            }
            t1++;
        }
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            while(s[j][i]==‘*‘)
            {
                g2[j][i]=t2;
                j++;
            }
            t2++;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(s[i][j]==‘*‘)
            {
                mp[g1[i][j]][g2[i][j]]=1;
            }
        }
    }
}
bool dfs(int x)
{
    for(int i=0;i<t2;i++)
    {
        if(mark[i]==-1&&mp[x][i])
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        memset(link,-1,sizeof(link));
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        init();
        for(int i=0;i<t1;i++)
        {
            memset(mark,-1,sizeof(mark));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-10-11 18:19:51

(最小点覆盖) poj 2226的相关文章

poj 2226 Muddy Fields(二分图最小点覆盖)

B - Muddy Fields Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2226 Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <

poj 2226 二分图 最小点覆盖 , 最大流

题目就是问如何用最小的板覆盖所有的草地.可以横着放,也可以竖着放,允许一个草地放多个点. 建图方法就是 每个横向的草地作为X,纵向连续的草地作为Y.     X连接Y的边表示,  这里有他们的公共点.. 很显然,覆盖所有草地,就是覆盖所有的边 ,二分图中,最小点覆盖 = 最大匹配 = =其实如果存在一条边未被选中的节点覆盖,则必然存在一条对应的增广路径 //tpl //ipqhjjybj_tpl.h //header.h #include <cstdio> #include <cstdl

POJ - 2226 Muddy Fields 二分图 最小点覆盖

题目大意:有一个n * m 大的牧场,牧场的土地分为两种,一种是很泥泞的,一种是相对比较干燥的 因为牧场里面的动物不喜欢泥泞的地方,所以牧场主想用些东西把这些泥泞的地方盖起来. 牧场主用一种宽度为1,长度不限的材料来覆盖这些泥泞的地方 问至少需要使用多少次这种材料,才可以把所有泥泞的地方盖起来 解题思路:刚开始没审对题目,上面所写的就是我刚开始认为的题意,所以我定势思维,以为这题和POJ - 3041 Asteroids一样,所以WA了两发 后来仔细看了一下,看漏了一个条件,那就是不能把相对干燥

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

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

POJ 2226 最小点覆盖(经典建图)

Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8881   Accepted: 3300 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, t

poj 2226 Muddy Fields(最小点覆盖)

题意: M*N的矩阵,每个格不是*就是#.     *代表水坑,#代表草地. 农民要每次可以用一块宽为1,长不限的木板去铺这个矩阵.要求这块木板不能覆盖草地.木板可以重复覆盖(即一块木板与另一块木板有交叉重叠的部分). 问农民最少需要操作多少次可以覆盖所有的水坑. 思路 : 与Battle Ships那题非常像,代码也几乎一样. 对于每一行,可以分成一段一段的水坑,将其视为一个一个点,作为左部X集合中的点. 对于每一列同理. 对于每一个水坑,将其看作一条线,将其在左部X集合中的位置和在右部Y集合

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int

POJ 3041 Asteroids (二分图最小点覆盖)

题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行(左边)和列(右边)用障碍物联系起来,比如(2,3)有个障碍物,那么左边的2和右边的3连边.边的个数就是障碍物的个数,点的个数就是次数,所以问题就变成了用少的点覆盖全部的边,也就是最小点覆盖问题.二分图中,最小点覆盖=最大匹配数. 1 //最小点覆盖 = 最大匹配 2 #include <iostre

poj 3041 Asteroids(最小点覆盖)

http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17543   Accepted: 9548 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <