您的位置:首页 > 其它

poj 2226 Muddy Fields(二分图最小点覆盖)

2014-08-11 22:50 337 查看
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 <= 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.

题意:给一张地图,其中有的点有水洼,要用木板把它们覆盖,求最小的木板数量(横着或者纵向)。
思路:

二分图中,选取最少的点数,使这些点和所有的边都有关联(把所有的边的覆盖),叫做最小点覆盖。
把每段连续的横水洼看成集合A中的点,每段连续的纵水洼看成集合B中的点,中间的交点看成连线,
因为每个有水单元必须覆盖,所以所连的2个单元至少有一个被覆盖。
所以求最小点覆盖。思路很巧妙。

而最小点覆盖 = 最大匹配数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
#define maxn 510
int R, C;
int h[maxn][maxn], z[maxn][maxn];
char mp[55][55];
int line[maxn], vis[maxn];
int cnth, cntz, ans;
vector <int> q[maxn];
void init(){
memset(h, 0, sizeof(h));
memset(z, 0, sizeof(z));
memset(q, 0, sizeof(q));
cnth = 1;
for(int i = 1; i <= R; i++){
for(int j = 1; j <= C; j++){
if(mp[i][j] == '*') h[i][j] = cnth;
if(mp[i][j] == '*' && mp[i][j+1] != '*') cnth++;

}
}

cntz = 1;
for(int i = 1; i <= C; i++){
for(int j = 1; j <= R; j++){
if(mp[j][i] == '*') z[j][i] = cntz;
if(mp[j][i] == '*' && mp[j+1][i] != '*') cntz++;
}
}

for(int i = 1; i <= R; i++){
for(int j = 1; j <= C; j++){
if(mp[i][j] == '*'){
q[h[i][j]].push_back(z[i][j]);
}
}
}
}
bool find(int x){
for(int i = 0; i < q[x].size(); i++){
if(!vis[ q[x][i] ] ){
vis[ q[x][i] ] = 1;
if(!line[q[x][i]] || find(line[q[x][i]])){
line[q[x][i]] = x;
return true;
}
}
}
return false;
}
void hungry(){
ans = 0;
memset(line, 0, sizeof(line));
for(int i = 1; i < 510; i++){
memset(vis, 0, sizeof(vis));
if(find(i)) ans++;
}
printf("%d\n", ans);

}
int main(){
while(~scanf("%d%d", &R, &C)){
memset(mp, '-1', sizeof(mp));
for(int i = 1; i <= R; i++){
for(int j = 1; j <= C; j++) cin>>mp[i][j];
}
init();
hungry();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: