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

相关文章

如何通过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实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

关于Gateway路由匹配规则解读

《关于Gateway路由匹配规则解读》本文详细介绍了SpringCloudGateway的路由匹配规则,包括基本概念、常用属性、实际应用以及注意事项,路由匹配规则决定了请求如何被转发到目标服务,是Ga... 目录Gateway路由匹配规则一、基本概念二、常用属性三、实际应用四、注意事项总结Gateway路由

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个