HDU 4185 Oil Skimming(匈牙利算法)

Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1822    Accepted Submission(s): 754


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
 
题意:
给你n*n的网格,#代表油,。代表海,让你一次取出一个1*2 或 2*1的油田问最多取出多少个。

思路:
可以转换成二分图的最大匹配数,用匈牙利算法。
先把油田一个一个进行编号,然后发现油田相邻的话,两两相连,匹配即可!
最后除以2.因为双向的代表1个嘛!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 600 + 10;
vector<int>g[maxn];
int from[maxn],ans,n;
bool use[maxn];
int mp[maxn][maxn];
bool match(int x){
	int len = g[x].size();
	for (int i = 0; i < len; ++i)
	if (!use[g[x][i]]){
		use[g[x][i]] = true;
		if (from[g[x][i]] == -1 || match(from[g[x][i]])){
			from[g[x][i]] = x;
			return true;
		}
	}
	return false;
}
int hungary(){
	ans = 0;
	memset(from, 255 ,sizeof(from));
	for (int i = 1; i <= n; ++i){
		memset(use,0,sizeof(use));
		if (match(i))++ans;
	}
	return ans;
}
int main(){
	int T,cnt=0,m;
	memset(mp,0,sizeof(mp));
	scanf("%d",&T);
	while(T--){
		for (int i = 0; i < maxn; ++i)g[i].clear();
		n=0;
		scanf("%d",&m);
		for (int i = 1; i <= m; ++i){
			getchar();
			for (int j = 1; j <= m; ++j){
				if (getchar() == '#')mp[i][j] = ++n;
				else mp[i][j] = 0;
			}
		}
		for (int i = 1; i <= m; ++i){
			for (int j = 1; j <= m; ++j)if (mp[i][j]){
				int u = mp[i][j];
				if (mp[i-1][j])g[u].push_back(mp[i-1][j]);		
				if (mp[i+1][j])g[u].push_back(mp[i+1][j]);
				if (mp[i][j-1])g[u].push_back(mp[i][j-1]);
				if (mp[i][j+1])g[u].push_back(mp[i][j+1]);
			}
		}
		hungary();
		printf("Case %d: %d\n",++cnt,ans/2);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值