POJ 2226

Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7557   Accepted: 2791

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.

Source

 
每个点所在的行连通块对点所在的列连通快连一条边,求二分图最大匹配即可。
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector> using namespace std; #define maxn 55
#define maxnode 2505 char s[maxn][maxn];
int un,vn,r,c;
int match[maxnode];
bool vis[maxnode];
vector<int> G[maxnode];
int row[maxn][maxn],col[maxn][maxn]; bool dfs(int u) {
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(vis[v]) continue;
vis[v] = ;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
return true; }
} return false;
} void build() {
un = vn = ; for(int i = ; i <= r; ++i) {
for(int j = ; j < c; ++j) {
if(s[i][j] == '*') {
row[i][j] = s[i][j - ] == '*' ?
row[i][j - ] : ++un;
} }
} for(int i = ; i < c; ++i) {
for(int j = ; j <= r; ++j) {
if(s[j][i] == '*') {
col[j][i] = s[j - ][i] == '*' ?
col[j - ][i] : ++vn;
}
}
} for(int i = ; i <= r; ++i) {
for(int j = ; j < c; ++j) {
if(s[i][j] == '*') {
// printf("%d %d\n",row[i][j],col[i][j]);
G[ row[i][j] ].push_back(col[i][j]);
}
}
} } void solve() { build(); int ans = ; for(int i = ; i <= vn; ++i) match[i] = -; for(int i = ; i <= un; ++i) {
memset(vis,,sizeof(vis));
if(dfs(i)) ++ans;
} printf("%d\n",ans);
} int main() {
freopen("sw.in","r",stdin); scanf("%d%d",&r,&c); for(int i = ; i <= r; ++i) {
scanf("%s",s[i]);
//puts(s[i]);
} solve(); return ;
}
上一篇:LINUX信息安全系统设计基础第一周学习总结


下一篇:Vue中发送ajax请求——axios使用详解