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;
}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章