poj 2226 Muddy Fields(最小点覆盖)

题意:

M*N的矩阵,每个格不是*就是#。     *代表水坑,#代表草地。

农民要每次可以用一块宽为1,长不限的木板去铺这个矩阵。要求这块木板不能覆盖草地。木板可以重复覆盖(即一块木板与另一块木板有交叉重叠的部分)。

问农民最少需要操作多少次可以覆盖所有的水坑。

思路 :

与Battle Ships那题非常像,代码也几乎一样。

对于每一行,可以分成一段一段的水坑,将其视为一个一个点,作为左部X集合中的点。

对于每一列同理。

对于每一个水坑,将其看作一条线,将其在左部X集合中的位置和在右部Y集合中的位置连起来,代表这个水坑可以被那两块木板覆盖。

求二分图的最小点覆盖即可。(最小点覆盖:用最少的点【可以是左部的点,也可以是右部的点】,使得所有的边都至少有一个端点是被覆盖的)

代码:

int T,m,n;
char mapp[55][55];
char temp[55];
int row[55][55], col[55][55];
int cc1,cc2;
int cx[2505], cy[2505];
bool bmask[2505];
vector<int> graph[2505];

int findPath(int u){
    int L=graph[u].size();
    rep(i,0,L-1){
        int v=graph[u][i];
        if(!bmask[v]){
            bmask[v]=true;
            if(cy[v]==-1||findPath(cy[v])){
                cy[v]=u;
                cx[u]=v;
                return 1;
            }
        }
    }
    return 0;
}
int MaxMatch(){
    int ans=0;
    rep(i,1,cc1) cx[i]=-1;
    rep(i,1,cc2) cy[i]=-1;
    rep(i,1,cc1) if(cx[i]==-1){
        mem(bmask,false);
        ans+=findPath(i);
    }
    return ans;
}

int main(){
    //freopen("test.in","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF){
        rep(i,0,m-1) scanf("%s",mapp[i]);
        cc1=0, cc2=0;
        mem(row,0);  mem(col,0);
        rep(i,0,m-1){
            if(mapp[i][0]==‘*‘) row[i][0]=++cc1;
            rep(j,1,n-1) if(mapp[i][j]==‘*‘){
                if(mapp[i][j-1]==‘*‘) row[i][j]=cc1; else row[i][j]=++cc1;
            }
        }

        rep(j,0,n-1){
            if(mapp[0][j]==‘*‘) col[0][j]=++cc2;
            rep(i,1,m-1) if(mapp[i][j]==‘*‘){
                if(mapp[i-1][j]==‘*‘) col[i][j]=cc2; else col[i][j]=++cc2;
            }
        }
        rep(i,1,cc1) graph[i].clear();
        rep(i,0,m-1) rep(j,0,n-1) if(mapp[i][j]==‘*‘) graph[row[i][j]].push_back(col[i][j]);

        int dd=MaxMatch();
        printf("%d\n",dd);
    }
    //fclose(stdin);
}
时间: 2024-08-02 12:20:16

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

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 Muddy Fields(合理建图+二分匹配)

1 /* 2 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 3 思路: 4 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1,然后让我们通过选择一行,或者 5 是一列将其所在行的或者所在列的 1全部删掉,求出最少需要几步? 6 7 这道题的思路就是:将行标 和 列标值为1的建立一条边!通过匈牙利算法可以得到这个二分图的最大匹配数 8 最大匹配数==最小顶点覆盖数!最小顶点覆盖就是用最少的点覆盖了这个二分图

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 二分图 最小点覆盖

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

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

题目链接:http://poj.org/problem?id=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 co

POJ 1463 Strategic game 最小点覆盖集(树形dp)

点击打开链接 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6105   Accepted: 2808 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is v

POJ 3401 Asteroids 求最小点覆盖集

把行和列都看做是点,小行星看成是边的话,那么这个显然就是求一个最小点覆盖集的问题. 最小点覆盖 == 最大匹配 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #inc

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

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

poj 1325 Machine Schedule 最小点覆盖

题目链接:http://poj.org/problem?id=1325 As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satis