Problem Description
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
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
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

......
.##...
.##...
....#.
....##
......
Sample Output
Case : 
Source

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

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

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

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 606
#define inf 1e12
int n;
char mp[N][N];
int tmp[N][N];
int cnt[N][N];
int match[N];
int vis[N];
int tot;
bool dfs(int u){
for(int i=;i<tot;i++){
if(!vis[i] && cnt[u][i]){
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=u;
return true;
}
}
}
return false;
} int solve(){
int ans=;
memset(match,-,sizeof(match));
for(int i=;i<tot;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
} return ans;
}
int main()
{
int t,ac=;;
scanf("%d",&t);
while(t--){
memset(tmp,,sizeof(tmp));
scanf("%d",&n);
int num=;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<n;j++){
if(mp[i][j]=='#'){
tmp[i][j]=num++;
}
}
}
tot=num;
memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(mp[i][j]!='#') continue;
if(i> && mp[i-][j]=='#') cnt[tmp[i][j]][tmp[i-][j]]=;
if(i<n- && mp[i+][j]=='#') cnt[tmp[i][j]][tmp[i+][j]]=;
if(j> && mp[i][j-]=='#') cnt[tmp[i][j]][tmp[i][j-]]=;
if(j<n- && mp[i][j+]=='#') cnt[tmp[i][j]][tmp[i][j+]]=;
}
}
printf("Case %d: ",++ac);
int ans=solve();
printf("%d\n",ans/);
}
return ;
}

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. Quartz.NET作业调度框架详解

    Quartz.NET作业调度框架详解 http://www.cnblogs.com/lmule/archive/2010/08/28/1811042.html

  2. Spring MVC对象转换说明

    在Spring MVC之前我们需要在Servlet里处理HttpServletRequest参数对象,但这个对象里的属性都是通用类型的对象(如字符串),处理起来很繁琐并且容易出错,而Spring MV ...

  3. nginx+vaadin配置

    nginx+Vaadin的特殊性在于配置WEBSOCKET或LONG_POLLING.网上资料不多,自己多次尝试配置都不成功,后来终于找到这篇说明才得以配置成功,使用效果不错,介绍如下. 1./etc ...

  4. 【贪心】【uva11520】 Fill the Square

    填充正方形(Fill the Square, UVa 11520) 在一个n×n网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如 ...

  5. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  6. stagefright框架(三)-选择Video Decode

    在<Stagefright (1) – Video Playback的流程>中,我们并没有详述Stagefright是如何根据影片档的类型来选择适合的video decoder,现在,就让 ...

  7. margin四个属性的顺序

    margin-top ,margin-right ,margin-bottom ,margin-left .方向为 上右下左,顺时针方向, 值可以是: 百分比(基于父对象总高度或宽度的百分比) 长度值 ...

  8. SQL查询多行合并成一行

    问题描述:无论是在sql 2000,还是在 sql 2005 中,都没有提供字符串的聚合函数,  所以,当我们在处理下列要求时,会比较麻烦:有表tb, 如下:id    value----- ---- ...

  9. C语言-getopt函数

    #include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...

  10. SpringMVC的web.xml配置注意

    web.xml需要放过所有资源文件,这个就看自己的系统中有哪些静态文件.一般的都是.js..css..jpg..png.jpeg等等,但是我还用到一些字体文件资源,所以也要过滤,不然前台会找不到. & ...