poj 2226-二分图的最小顶点覆盖

题目意思:牛要回家,路上遇到泥泞的地方需要主人铺上木板,求出主人至少要铺多少木板???

其中'*'表示泥泞,‘.’表示草坪


Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6967 Accepted: 2578

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



代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAX = 55;
const int MAXN = 1005;
int n, m, k1, k2;
int g[MAXN][MAXN];
int vis[MAXN], yM[MAXN];
char map[MAX][MAX];
int mark1[MAX][MAX];
int mark2[MAX][MAX];
int Search(int u){
	int v;
	for( v = 0; v < k2; v ++) {
		if(g[u][v] && !vis[v]) {
			vis[v] = 1;
			if(yM[v] == -1 || Search(yM[v])) {
				yM[v] = u;
				return 1;
			}
		}
	}
	return 0;
}
int MaxMatch() {
	int u,ret = 0;
	memset(yM, -1, sizeof(yM));
	for(u = 0; u < k1; u ++) {
		memset(vis, 0, sizeof(vis));
		if(Search(u))
			ret ++;
	}
	return ret;
}
void solve() {
	k1 = 0;
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < m; j ++) {
			if(map[i][j] == '*') {
				while(map[i][j] == '*') {
					mark1[i][j] = k1;
					j ++;
				}
				k1 ++;
			}
		}
		k2 = 0;
		for(int i = 0; i < m; i ++)
			for(int j = 0; j < n; j ++)	{
				if(map[j][i] == '*') {
					while(map[j][i] == '*') {
						mark2[j][i] = k2;
						j ++;
					}
					k2 ++;
				}
			}
			for(int i = 0; i < n; i ++)
				for(int j = 0; j < m; j ++) {
					if(map[i][j] == '*') {
						g[mark1[i][j]][mark2[i][j]] = 1;
					}
				}
}
int main() {
	int ans;
	while(scanf("%d%d", &n, &m) != EOF){
		memset(g, 0, sizeof(g));
		memset(mark1, 0, sizeof(mark1));
		memset(mark2, 0, sizeof(mark2));
		for(int i = 0; i < n; i ++)
			scanf("%s", map[i]);
		solve();
		ans = MaxMatch();
		printf("%d\n", ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值