Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8881   Accepted: 3300

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

 
题目意思:
一个n*m的图,其中'.'表示平地,'*'水池。现用一些宽为1个单位,长度不限的木板覆盖所有的水池(某个水池可以被覆盖多次)。问所用木板最少为多少。
 
思路:
每个水池可以被横着覆盖也可以被竖着覆盖,那么题目就成为选用一些覆盖策略使得所有点被横着覆盖或者被竖着覆盖,而某些点被横着或竖着覆盖可以影响与其横着连续的点或竖着连续的点。那么横着处理一下图如同题目中Hint中,同理竖着处理一下图。每个点有两个数字,一个是横着状态数字,另一个是竖着状态数字,连边后建图,就是最小点覆盖了。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 50 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n, m;
vector<int>ve[N*N];
int from[N*N];
bool visited[N*N];
char map[N][N];
int map1[N][N];
int map2[N][N]; int march(int u){
int i, v;
for(i=;i<ve[u].size();i++){
v=ve[u][i];
if(!visited[v]){
visited[v]=true;
if(from[v]==-||march(from[v])){
from[v]=u;
return ;
}
}
}
return ;
} main()
{
int i, j, k;
while(scanf("%d %d",&n,&m)==){
for(i=;i<n;i++) scanf("%s",map[i]);
memset(map1,-,sizeof(map1));
memset(map2,-,sizeof(map2));
int num=;
int maxh=;
//横着处理
for(i=;i<n;i++){
for(j=;j<m;j++){
if(map[i][j]=='*'){
if(j==) map1[i][j]=num;
else{
if(map[i][j-]=='*') map1[i][j]=map1[i][j-];
else map1[i][j]=num;
}
}
else {
if(j<m-&&map[i][j+]=='*') num++;
}
}
num++;
maxh=max(maxh,num);
}
//竖着处理
num=;
for(j=;j<m;j++){
for(i=;i<n;i++){
if(map[i][j]=='*'){
if(i==) map2[i][j]=num;
else{
if(map[i-][j]=='*') map2[i][j]=map2[i-][j];
else map2[i][j]=num;
}
}
else{
if(i<n-&&map[i+][j]=='*') num++;
}
}
num++;
maxh=max(maxh,num);
}
//建二分图
for(i=;i<maxh;i++) ve[i].clear();
for(i=;i<n;i++){
for(j=;j<m;j++){
if(map1[i][j]!=-&&map2[i][j]!=-){
ve[map1[i][j]].push_back(map2[i][j]);
}
}
}
//二分匹配
memset(from,-,sizeof(from));
num=;
for(i=;i<maxh;i++){
memset(visited,false,sizeof(visited));
if(march(i)) num++;
}
printf("%d\n",num);
}
}

最新文章

  1. ACM-南京理工大学第八届程序设计竞赛-网络赛(2016.04.17)
  2. More is better(MST)(求无向图中最大集合元素个数)
  3. LeetCode OJ 题解
  4. PHP中Array关于数组的用法
  5. 批处理启动QQ
  6. Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses
  7. 51nod 1109 01组成的N的倍数
  8. 搭建Nginx(负载均衡)+Redis(Session共享)+Tomcat集群
  9. 【优先队列】【最近连STL都写不出来了/(ㄒoㄒ)/~~】hdu_5360/多校#6_1008
  10. Java 并发——多线程基础
  11. eclipse 常用的一些设置
  12. Java学习之路:详细解释Java解析XML四种方法
  13. MySQL学习(二)复制
  14. Linux下gcc和g++的区别
  15. Vue学习小结(一)安装依赖与数据来源
  16. python 文件处理(转载)
  17. fiddler学习总结--fiddler抓包篡改数据请求
  18. Ext.form.field.Picker (ComboBox、Date、TreePicker、colorpick.Field)竖向滚动导致布局错误
  19. C# Owin初探 概念理解(一)
  20. M端计算rem方法

热门文章

  1. Android--Retrofit+RxJava(二)
  2. 创建和导出SVG的技巧(转载)
  3. maven参考文章推荐
  4. Frost R&amp;D
  5. 20145227&amp;20145201 《信息安全系统设计基础》实验二 固件开发
  6. SSI-Server Side Inclued
  7. [课程设计]Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划)
  8. js广告弹窗
  9. (转)MySQL联表查询
  10. Winform开发框架之单据窗体生成(主从表,流水单号)