题意
农夫的养牛场,是一个R 行C 列的矩形,一场大雨后,养牛场低洼的地方都有了积水。农夫的牛都很娇贵的,他们吃草的时候,不想把他们的蹄子给弄脏了。为了不让牛儿们把它们的蹄子弄脏,农夫决定把有水的地方铺上木板。他的木板是宽度为1,长度没有限制的。
他想用最少数目的木板把所有有水的低洼处给覆盖上,木板不能覆盖草地,但是可以重叠。
题解
找到每行连续的’*’,对每一块编号。再找到每列连续的,编号。每行的和每列的连边跑匈牙利即可
常数巨大的丑陋代码
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <algorithm>
using namespace std;
# define IL inline
# define RG register
# define UN unsigned
# define ll long long
# define rep(i, a, b) for(RG int i = a; i <= b; i++)
# define per(i, a, b) for(RG int i = b; i >= a; i--)
# define mem(a, b) memset(a, b, sizeof(a))
# define max(a, b) (((a) > (b)) ? (a) : (b))
# define min(a, b) (((a) < (b)) ? (a) : (b))
# define Swap(a, b) a ^= b, b ^= a, a ^= b;
IL ll Get(){
char c = '!'; ll z = 1, num = 0;
while(c != '-' && (c < '0' || c > '9')) c = getchar();
if(c == '-') z = -1, c = getchar();
while(c >= '0' && c <= '9') num = num * 10 + c - '0', c = getchar();
return num * z;
}
const int MAXN = 501;
int map[MAXN][MAXN], vis[MAXN], girl[MAXN], n, m, m1[MAXN][MAXN], m2[MAXN][MAXN];
char land[MAXN][MAXN];
IL int Dfs(RG int u){
rep(v, 1, m) if(map[u][v] && !vis[v]){
vis[v] = 1;
if(girl[v] == -1 || Dfs(girl[v])){
girl[v] = u;
return 1;
}
}
return 0;
}
IL int Hungarian(){
RG int ans = 0;
mem(girl, -1);
rep(i, 1, n) mem(vis, 0), ans += Dfs(i);
return ans;
}
int main(){
RG int x = Get(), y = Get();
rep(i, 1, x) rep(j, 1, y) scanf(" %c", &land[i][j]);
rep(i, 1, x) rep(j, 1, y) if(land[i][j] == '*'){
n++;
while(j <= y && land[i][j] == '*') m1[i][j++] = n;
}
rep(i, 1, y) rep(j, 1, x) if(land[j][i] == '*'){
m++;
while(j <= x && land[j][i] == '*') m2[j++][i] = m;
}
rep(i, 1, x) rep(j, 1, y) if(land[i][j] == '*') map[m1[i][j]][m2[i][j]] = 1;
printf("%d
", Hungarian());
return 0;
}