Problem Description
  1. Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.
Input
  1. The input starts with an integer K ( <= K <= ) indicating the number of cases. Each case starts with an integer N ( <= N <= ) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.
Output
  1. For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from ) and M is the maximum number of scoops of oil that may be extracted.
Sample Input
  1.  
  2. ......
  3. .##...
  4. .##...
  5. ....#.
  6. ....##
  7. ......
Sample Output
  1. Case :
Source

题意:求能够找到最多多少个“##”(横或竖都行)

解题:关键是建图,在建图时,先用tmp[][]数组将‘#’的数字存起来(tmp=0开始),然后再遍历一遍,如果遇到的是mp[i][j]=='#',则将上下左右是‘#’的标记为cnt[ tmp[][] ][tmp[][] ]=1,最后就是经典的匈牙利算法了。

       犯了个致命的错误,在匈牙利算法时所对应的n应该是num,即重新建图后的‘n’。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. #define PI acos(-1.0)
  17. #define max(a,b) (a) > (b) ? (a) : (b)
  18. #define min(a,b) (a) < (b) ? (a) : (b)
  19. #define ll long long
  20. #define eps 1e-10
  21. #define MOD 1000000007
  22. #define N 606
  23. #define inf 1e12
  24. int n;
  25. char mp[N][N];
  26. int tmp[N][N];
  27. int cnt[N][N];
  28. int match[N];
  29. int vis[N];
  30. int tot;
  31. bool dfs(int u){
  32. for(int i=;i<tot;i++){
  33. if(!vis[i] && cnt[u][i]){
  34. vis[i]=;
  35. if(match[i]==- || dfs(match[i])){
  36. match[i]=u;
  37. return true;
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43.  
  44. int solve(){
  45. int ans=;
  46. memset(match,-,sizeof(match));
  47. for(int i=;i<tot;i++){
  48. memset(vis,,sizeof(vis));
  49. if(dfs(i)){
  50. ans++;
  51. }
  52. }
  53.  
  54. return ans;
  55. }
  56. int main()
  57. {
  58. int t,ac=;;
  59. scanf("%d",&t);
  60. while(t--){
  61. memset(tmp,,sizeof(tmp));
  62. scanf("%d",&n);
  63. int num=;
  64. for(int i=;i<n;i++){
  65. scanf("%s",mp[i]);
  66. for(int j=;j<n;j++){
  67. if(mp[i][j]=='#'){
  68. tmp[i][j]=num++;
  69. }
  70. }
  71. }
  72. tot=num;
  73. memset(cnt,,sizeof(cnt));
  74. for(int i=;i<n;i++){
  75. for(int j=;j<n;j++){
  76. if(mp[i][j]!='#') continue;
  77. if(i> && mp[i-][j]=='#') cnt[tmp[i][j]][tmp[i-][j]]=;
  78. if(i<n- && mp[i+][j]=='#') cnt[tmp[i][j]][tmp[i+][j]]=;
  79. if(j> && mp[i][j-]=='#') cnt[tmp[i][j]][tmp[i][j-]]=;
  80. if(j<n- && mp[i][j+]=='#') cnt[tmp[i][j]][tmp[i][j+]]=;
  81. }
  82. }
  83. printf("Case %d: ",++ac);
  84. int ans=solve();
  85. printf("%d\n",ans/);
  86. }
  87. return ;
  88. }

hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)的更多相关文章

  1. HDU 4185 ——Oil Skimming——————【最大匹配、方格的奇偶性建图】

    Oil Skimming Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  2. HDU4185 Oil Skimming 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8231146.html 题目传送门 - HDU4185 题意概括 每次恰好覆盖相邻的两个#,不能重复,求最大覆盖次 ...

  3. HDU 4185 Oil Skimming

    题目大意:在一个N*N的矩阵里寻找最多有多少个“##”(横着竖着都行).     题目分析:重新构图,直接以相邻的两个油井算中间算以条边,然后进行匹配,看看两两之间最多能匹配多少对. #include ...

  4. HDU 4185 Oil Skimming 【最大匹配】

    <题目链接> 题目大意: 给你一张图,图中有 '*' , '.' 两点,现在每次覆盖相邻的两个 '#' ,问最多能够覆盖几次. 解题分析: 无向图二分匹配的模板题,每个'#'点与周围四个方 ...

  5. hdoj--5093--Battle ships(二分图经典建图)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  6. poj--1149--PIGS(最大流经典建图)

    PIGS Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  7. 4185 Oil Skimming 最大匹配 奇偶建图

    题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合 ...

  8. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  9. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

随机推荐

  1. Maximum Subarray 解答

    Question Find the contiguous subarray within an array (containing at least one number) which has the ...

  2. poj 1020 Anniversary Cake(切正方形蛋糕+搜索)

                                                                                                         ...

  3. PB C/S轉B/S ODBC方式連接數據庫

    PB C/S轉B/S ODBC方式連接數據庫,DSN需要建為系統而不是使用者DSN,否則連不上數據庫.

  4. BorderLayout布局,修改各个区域大小办法

    摘自http://blog.csdn.net/zcsearching/article/details/50808446 BorderLayout控件大小的设置 使用BorderLayout时,中间的面 ...

  5. 【转】RTSP流理解

    rtsp是使用udp还是tcp,是跟服务器有关,服务器那边说使用udp,那就使用udp,服务器说使用tcp那就使用tcp rtsp客户端的创建: 1.建立TCP socket,绑定服务器ip,用来传送 ...

  6. UGUI Toggle控件

    今天我们来看看Toogle控件, 它由Toogle + 背景 + 打勾图片 + 标签组成的. 它主要用于单选和多选 属性讲解: Is On: 代表是否选中. Toogle Transition: 在状 ...

  7. paip.sql2k,sql2005,sql2008,sql2008 r2,SQL2012以及EXPRESS版本的区别

    paip.sql2k,sql2005,sql2008,sql2008 r2,SQL2012以及EXPRESS版本的区别 作者Attilax ,  EMAIL:1466519819@qq.com  来源 ...

  8. Ffmpeg和SDL创建线程(转)

    Spawning Threads Overview Last time we added audio support by taking advantage of SDL's audio functi ...

  9. Servlet页面间对象传递的方法

    Servlet页面间对象传递的方法 1.request 2.session 3.application 4.cookie 5.其它的

  10. rhel5.8安装mysql测试

    MySQL-rhel5.8 安装:在Linux下安装MySQL有三种方式:第一种以rpm的二进制文件分个安装,第二种是自己编译源码后安装,最后一种是以二进制tar.gz文件来安装(这种安装方式下载安装 ...