Muddy Fields
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9754   Accepted: 3618

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

  1. 4 4
  2. *.*.
  3. .***
  4. ***.
  5. ..*.

Sample Output

  1. 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

题意:一个row*col的矩阵表示一块田地,'*'表示湿地,'.'表示草地。现在FJ要在田地上铺木板盖掉所有的湿地,露出所有的草地。每块木板的宽度为1,长度为任意长。问FJ最少用几块木板就可以完成任务?
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <cmath>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. vector<int> G[5005];
  10. int match[5005],used[5005];
  11. int n,r,c,cntl,cntr,m;
  12. int nel[55][55],ner[55][55];
  13.  
  14. void add_edge(int u,int v)
  15. {
  16. G[u].push_back(v);
  17. G[v].push_back(u);
  18. }
  19.  
  20. bool dfs(int u)
  21. {
  22. used[u]=1;
  23. for(int i=0;i<G[u].size();i++)
  24. {
  25. int v=G[u][i];
  26. int w=match[v];
  27. if(w<0||!used[w]&&dfs(w))
  28. {
  29. match[u]=v;
  30. match[v]=u;
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36.  
  37. int bipartite_match()
  38. {
  39. memset(match,-1,sizeof(match));
  40. int res=0;
  41. for(int i=1;i<=cntl+cntr;i++)
  42. if(match[i]<0)
  43. {
  44. memset(used,0,sizeof(used));
  45. if(dfs(i)) res++;
  46. }
  47. return res;
  48. }
  49.  
  50. char s[55][55];
  51.  
  52. void build()
  53. {
  54. for(int i=1;i<=cntl+cntr;i++) G[i].clear();
  55. for(int i=1;i<=n;i++)
  56. for(int j=1;j<=m;j++)
  57. if(s[i][j]=='*')
  58. add_edge(nel[i][j],cntl+ner[i][j]);
  59. }
  60.  
  61. int main()
  62. {
  63. while(~scanf("%d %d",&n,&m))
  64. {
  65. cntl=0;cntr=0;
  66. for(int i=1;i<=n;i++)
  67. {
  68. scanf("%s",s[i]+1);
  69. for(int j=1;j<=m;)
  70. {
  71. while(s[i][j]=='.'&&j<=m) j++;
  72. if(j>m) break;
  73. cntl++;
  74. while(s[i][j]=='*')
  75. {
  76. nel[i][j]=cntl;
  77. j++;
  78. }
  79. }
  80. }
  81. for(int j=1;j<=m;j++)
  82. {
  83. for(int i=1;i<=n;)
  84. {
  85. while(s[i][j]=='.'&&i<=n) i++;
  86. if(i>n) break;
  87. cntr++;
  88. while(s[i][j]=='*')
  89. {
  90. ner[i][j]=cntr;
  91. i++;
  92. }
  93. }
  94. }
  95. build();
  96. printf("%d\n",bipartite_match());
  97. }
  98. return 0;
  99. }

  分析:爽啊,这道题算是自己没看题解独立完成的吧,水平开始有点出来了

这道题跟以前做的那道毁灭星球的题目最大的不同就是要让矩阵中的草地都露出来,这就决定了他的

建图方式有些不太一样,建图:首先预处理出每个顶点在行中属于的标号,与在列中属于的标号,标号时对于连续的一段进行合并,这一段连续的*标号成同一个数字(这也是能进行二分匹配的原因,因为能用同一块木板),接下来建图,左边代表行标号,右边代表列标号,根据二分图中最小顶点覆盖等于最大匹配数即可。

然后

TTTTTTTTTTTTTTTTT POJ 2226 草地覆木板 二分匹配 建图的更多相关文章

  1. TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

    Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5004   Accepted: 1444 ...

  2. HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧

    题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...

  3. POJ 2226 Muddy Fields 二分图(难点在于建图)

    题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...

  4. POJ 2226 Muddy Fields(二分匹配 巧妙的建图)

    Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R ...

  5. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

  6. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. poj 1034 The dog task (二分匹配)

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2559   Accepted: 1038   Sp ...

  8. POJ 1466 大学谈恋爱 二分匹配变形 最大独立集

    Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 11694   Accepted: 5230 D ...

  9. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

随机推荐

  1. elasticsearch教程--中文分词器作用和使用

    概述   本文都是基于elasticsearch安装教程 中的elasticsearch安装目录(/opt/environment/elasticsearch-6.4.0)为范例 环境准备 ·全新最小 ...

  2. 01:keepalive高可用集群

    1.1 keepalived高可用软件介绍 1.keepalived--监控检查 注:keepalive软件有两种功能:监控检查.VRRP冗余协议 1. keepalive的作用是检测web服务器的状 ...

  3. 剑指offer-数组中重复的数字-数组-python

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  4. java 问题汇总

    1.自动加载出错 require a bean of .... The injection point has the following annotations: - @org.springfram ...

  5. LintCode 64---合并排序数组

    public class Solution { /* * @param A: sorted integer array A which has m elements, but size of A is ...

  6. linux查看端口被占用情况

    Linux 查看端口占用情况可以使用 lsof 和 netstat 命令. 如果linux中没有这两个命令,则yum安装一下 yum install -y lsof yum install -y ne ...

  7. vue-router实现原理

    vue-router实现原理 近期面试,遇到关于vue-router实现原理的问题,在查阅了相关资料后,根据自己理解,来记录下.我们知道vue-router是vue的核心插件,而当前vue项目一般都是 ...

  8. 使输入框(input  & textarea)变为只可读状态readonly="readonly",禁用输入框disabled="disabled"

    使输入框变为只可读状态 readonly="readonly" <input class="select-city" placeholder=" ...

  9. 多线程编程-- part5.1 互斥锁之公平锁-获取锁

    基本概念 1.AQS:AbstractQueuedSynchronizer类 AQS是java中管理“锁”的抽象类,锁的许多公共方法都是在这个类中实现.AQS是独占锁(例如,ReentrantLock ...

  10. Linux禁止root远程登录及修改默认端口

    1.1 修改SSHD配置,禁止root远程登录 禁止登录之前先穿甲一个可以远程登录的普通用户,以免造成登录不了的情况 [root@jhkj66 ~]# useradd yw001 #创建用户 [roo ...