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

Muddy Fields POJ - 2226

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
HintOUTPUT DETAILS:

Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
…2.
Board 2 overlaps boards 3 and 4.

题意:
在这里插入图片描述
题解:

与POJ3041十分相似
POJ3041的建图方式是行为一个集合,列为一个集合;但是这种方法在这里是不适用的.因为这道题加了一个限制:
木板不能够覆盖到草,所以整行消除或整列消除的方式在这里失效了,因为这样可能会把草也给消除了.通过改变建图方式,可以解决这个问题.
POJ3041:
对于每个目标点,消除的方式要么是整行消除,要么是整列消除.
建图方式:行为一个集合,列为一个集合.
这道题中:
对于每个目标点(泥地)),消除的方式要么是和连在一起的横向泥地一起被消除,要么是和连在一起的纵向泥地一起被消除.
建图方式:横向连续泥地块为一个集合,纵向连续泥地块为一个集合.

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAX_V = 2555 * 2;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];
int R, C, V;

void add_edge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
}

bool dfs(int v) {
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++) {
        int u = G[v][i], w = match[u];
        if ( w < 0 || !used[w] && dfs(w)) {
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}

int bipartite_match() {
    int res = 0;
    memset(match, -1, sizeof(match));
    for (int v = 0; v < V; v++) {
        if (match[v] < 0) {
            memset(used, 0, sizeof(used));
            if(dfs(v)) {
                res++;
            }
        }
    }
    return res;
}

int main() {
    char mp[55][55];
    scanf("%d%d", &R, &C);
    V = R * C + 2500;
    for(int i = 1; i <= R; i++) {
        getchar();
        for(int j = 1; j <= C; j++) {
            scanf("%c", &mp[i][j]);
        }
    }
    for(int i = 1; i <= R; i++) {
        for(int j = 1; j <= C; j++) {
            if(mp[i][j] == '*') {
                int s = i, t = j;
                while(s > 0 && mp[s - 1][j] == '*') {
                    s--;
                }
                while(t > 0 && mp[i][t - 1] == '*') {
                    t--;
                }
                add_edge(s * 50 + j, t + i * 50 + 2500);
            }
        }
    }
    printf("%d\n", bipartite_match());
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值