HDU4185 Oil Skimming(二分图匹配,匈牙利算法)

2023-10-05 23:42

本文主要是介绍HDU4185 Oil Skimming(二分图匹配,匈牙利算法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

思路

有一个石油大亨要在一片 NN 区域里面打捞石油,它的网能网住的区域面积是 1×2 #代表这个地方是石油,.代表这个地方是海水,问在这一片水域,最多能打捞多少石油(不能石油和海水一起捞)。

先给石油编上号码,编号为:1 2 3 4 5…这种,然后我们遍历这个区域,如果在 1×2 的区域有石油,那么就给他们的编号加一条边,建立二分图,最后求这个二分图的最大匹配就是答案。

因为建立的是双向边,所以要最后结果除以2

代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int N=1000+20;
int e[N][N],vis[N],match[N],n,temp[N][N],num;
char s[N][N];
int dfs(int u)
{for(int i=1; i<=num; i++){if(e[u][i]&&!vis[i]){vis[i]=1;if(!match[i]||dfs(match[i])){match[i]=u;return 1;}}}return 0;
}
int query()
{mem(match,0);int sum=0;for(int i=1; i<=num; i++){mem(vis,0);if(dfs(i))sum++;}return sum;
}
int main()
{int t,q=1;scanf("%d",&t);while(t--){mem(temp,0);mem(e,0);num=0;scanf("%d",&n);for(int i=1; i<=n; i++){scanf("%s",s[i]+1);for(int j=1; j<=n; j++)if(s[i][j]=='#')temp[i][j]=++num;}for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)if(s[i][j]=='#'){if(i!=1&&s[i-1][j]=='#') e[temp[i][j]][temp[i-1][j]]=1;if(j!=1&&s[i][j-1]=='#') e[temp[i][j]][temp[i][j-1]]=1;if(i!=n&&s[i+1][j]=='#') e[temp[i][j]][temp[i+1][j]]=1;if(j!=n&&s[i][j+1]=='#') e[temp[i][j]][temp[i][j+1]]=1;}printf("Case %d: %d\n",q++,query()/2);}return 0;
}

这篇关于HDU4185 Oil Skimming(二分图匹配,匈牙利算法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/152308

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为