Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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 (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) 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 1) and M is the maximum number of scoops of oil that may be extracted.

Sample Input

1 6

……

.##…

.##…

….#.

….##

……

Sample Output

Case 1: 3


解题心得:

  1. 题意很简单,就是要你在图中去截取1x2的方块,问最多能截取多少个。
  2. 就是一个二分匹配问题,还是很简单的,有两种建图的方法
    • 第一种是给每一个坐标编号,查找每一个‘#’的四周图形,然后建一个双向的图,得到的答案除2就可以了。
    • 第二种就更高级了,仔细观察可以观看一个点的行列之和与他四周的四个格子的行列和奇偶性一定是不同的,这样就可以参考国际象棋的棋盘,黑色的方格匹配白色方格,二分匹配。

弱智建图写代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 610;
char maps[maxn][maxn];
bool vis[maxn*maxn];
vector <int> ve[maxn*maxn];
int n,dir[4][2] = {0,1,0,-1,-1,0,1,0},match[maxn*maxn]; void init()
{
scanf("%d",&n);
for(int i=0;i<maxn;i++)
ve[i].clear();
memset(vis,0,sizeof(vis));
memset(maps,0,sizeof(maps));
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++)
scanf("%s",maps[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(maps[i][j] == '#')
for(int k=0;k<4;k++)
{
int x = i + dir[k][0];
int y = j + dir[k][1];
if(maps[x][y] == '#')
ve[i*n+j].push_back(x*n+y);//如果四周是‘#’就建立联系
} } bool dfs(int x)//match
{
for(int i=0;i<ve[x].size();i++)
{
int v = ve[x][i];
if(!vis[v])
{
vis[v] = true;
if(match[v] == -1 || dfs(match[v]))
{
match[v] = x;
return true;
}
}
}
return false;
} int solve()
{
int ans = 0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(maps[i][j] == '#')
{
memset(vis,0,sizeof(vis));
if(dfs(i*n+j))
ans++;
}
return ans;
} int main()
{
int t;
scanf("%d",&t);
int cas = 1;
while(t--)
{
init();
int ans = solve();
printf("Case %d: %d\n",cas++,ans/2);//因为建立的是双向图,所以一定要除2
}
return 0;
}

HDU:4185-Oil Skimming的更多相关文章

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

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

  2. hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)

    Problem Description Thanks to a certain "green" resources company, there is a new profitab ...

  3. HDU 4185 Oil Skimming

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

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

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

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

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

  6. 匈牙利算法求最大匹配(HDU-4185 Oil Skimming)

    如下图:要求最多可以凑成多少对对象 大佬博客: https://blog.csdn.net/cillyb/article/details/55511666 https://blog.csdn.net/ ...

  7. Oil Skimming HDU - 4185(匹配板题)

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU4185:Oil Skimming(二分图最大匹配)

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. Hdu4185 Oil Skimming

    Oil Skimming Problem Description Thanks to a certain "green" resources company, there is a ...

随机推荐

  1. 牛客网Java刷题知识点之什么是代码块、普通代码块、静态代码块、同步代码块、构造代码块以及执行顺序

    不多说,直接上干货! 这种形式的程序段我们将其称之为代码块,所谓代码块就是用大括号({})将多行代码封装在一起,形成一个独立的数据体,用于实现特定的算法.一般来说代码块是不能单独运行的,它必须要有运行 ...

  2. [转]eclipse启动tomcat无法访问的解决方法

    这篇文章介绍了eclipse启动tomcat无法访问的解决方法,有需要的朋友可以参考一下 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080 ...

  3. Java字节码分析

    目录 Java字节码分析 查看字节码详细内容 javap 实例分析 Java字节码分析 对于源码的效率,但从源码来看有时无法分析出准确的结果,因为不同的编译器版本可能会将相同的源码编译成不同的字节码, ...

  4. 【翻译转载】【官方教程】Asp.Net MVC4入门指南(3):添加一个视图

    3. 添加一个视图 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-vi ...

  5. Redis hash(哈希)

    Redis hash可储存多个键值对,适合储存对象的属性. 1.hset key fieldName fileValue    //hset即hash set,set这里是设置的意思.往hash中添加 ...

  6. JVM类加载机制二

    类加载器与双亲委派模型 类加载器 类加载的操作不是有虚拟机完成的,而是由类加载器完成的,这样可以让程序定义决定加载哪个类. 类加载器的分类: 从虚拟机的角度有两种加载器,一种是启动类加载器Bootst ...

  7. 织梦修改“dedecms提示信息”

    1.根目录下include文件夹,找到common.func.php: 2.根目录下dede文件夹(管理目录默认dede),找到sys_data_done.php: 3.打开以上2个.php文件,把“ ...

  8. Ubuntu 11.04 安装 cuda5.0

    由于实验需要,于2016年10月15日再Ubuntu11.04安装cuda5.0,但是从网上查找Ubuntu11.04 只有对应的支持的cuda4 版本,cuda 5.0前面版本不支持IDE nisg ...

  9. SQL Server扩展事件system_health会话总结

    system_health会话概念 我们知道扩展事件(Extended Events)是从SQL Server 2008开始引入的.system_health会话是SQL Server默认包含的扩展事 ...

  10. 关于JavaScript的变量和函数提升

    第一种理解方式:let和const不能被使用,直到他们被声明 对于var定义的变量,解析器会提升其到作用域顶部. // Outputs: undefined console.log(x); var x ...