POJ 2226 Muddy Fields————最小点覆盖

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.

 

思路:

先对泥地进行预处理,即 只放横着的板子 和 只放竖着的板子 的最优情况,如下图:

只横着放:

1.2.

.333

444.

..5.

只竖着放:

1.4.

.345

234.

..4.

然后,对于每个泥地' * ',要么横着放,要么竖着放。所以建一个二分图,左边是横着放板子的编号,右边是竖着板子编号:

然后,对于第一行第一列的泥地‘*’,选横板就是选横板1号,选竖板就是竖板1号。那么先链接 1,1。

换句话说,如果要覆盖这个第一行第一列的泥地‘*’,要么选左一,要么选右一。

再换句话说,就是不管怎么选,只要能选这根线连的端点之一就行。

继续看下一个泥地,第一行第三列的泥地,要么选横板2,要么选择竖板4。所以就连接 2, 4。

只要选择这条线的端点之一就可以完成对这个泥地的覆盖。

如此,考虑完所有点之后:

接下来的任务就是求最小点覆盖问题。

那么最小点覆盖 = 二分图最大匹配。

套二分图最大匹配板子就可以。

 

AC代码:

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>

#define MAXN 505
#define INF 0x3f3f3f3f

using namespace std;

int r, c, n;
int xx[MAXN][MAXN], yy[MAXN][MAXN], match[MAXN * MAXN];
char a[MAXN][MAXN];
bool edge[MAXN][MAXN], vis[MAXN * MAXN];

int getxy() {
	int tx = 1, ty = 1;
	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c+1; j++) {
			if (a[i][j] == '*') {
				xx[i][j] = tx;
			}
			else if (a[i][j - 1] == '*') {
				tx++;
			}
		}
	}
	for (int i = 1; i <= c; i++) {
		for (int j = 1; j <= r + 1; j++) {
			if (a[j][i] == '*') {
				yy[j][i] = ty;
			}
			else if (a[j-1][i] == '*') {
				ty++;
			}
		}
	}
	return max(tx, ty);
}

int dfs(int u) {
	for (int i = 1; i <= n; i++) {
		if (vis[i] == false && edge[u][i] == true) {
			vis[i] = true;
			if (match[i] == 0 || dfs(match[i])) {
				match[i] = u;
				return 1;
			}
		}
	}
	return 0;
}

int main() {
	memset(a, '.', sizeof(a));
	scanf("%d%d", &r, &c);
	getchar();
	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c; j++) {
			scanf("%c", &a[i][j]);
		}
		getchar();
	}
	n = getxy();
	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c; j++) {
			edge[xx[i][j]][yy[i][j]] = true;
		}
	}
	int sum = 0;
	for (int i = 1; i <= n; i++) {
		memset(vis, false, sizeof(vis));
		if (dfs(i)) {
			sum++;
		}
	}
	printf("%d\n", sum);

	return 0;
}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值