Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7557 | Accepted: 2791 |
Description
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
* 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
Sample Input
4 4
*.*.
.***
***.
..*.
Sample Output
4
Hint
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 ;
}