【HDU - 4185】Oil Skimming (二分图,建图,匈牙利算法)

题干:

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

题目大意:

有一个地图,' . '代表水,' # ' 代表油。每个单元格是10*10的,现用一个'.'或'#'表示,现有10*20的勺子可以提取出水上漂浮的油,问最多可以提取几勺的油; 每次提取的时候勺子放的位置都要是油,不然就被海水污染而没有价值了。

一句话题意:每次恰好覆盖相邻的两个#,不能重复,求最大覆盖次数。

解题报告:

   建模:用模板每次恰好覆盖相邻的两个#,同一个点不能重复覆盖,求最大覆盖次数。

  建图:这题有两种建图方式。一种是奇偶建图,即(i+j)的奇偶数来建图,vis1表示左侧集合,vis2表示右侧。因为木板能且仅能只能放在(i+j)%2==0 和 (i+j)%2==1这两个相邻的位置上,所以我们这么去分集合。然后找匹配就可以了。

             第二种建图方式就是如果是'#'那就++cnt,就是都用同一套体系来给‘#’标号,这样最后匈牙利搞完之后答案/2即可。

下面分别给出代码。

建图方式1的AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,ans;
int vis1[605][605],vis2[605][605],line[605][605];
bool used[605*605];
char maze[605][605];
int tot1,tot2;
int nxt[605*605];
bool find(int x) {for(int i = 1; i<=tot2; i++) {if(line[x][i] && !used[i]) {used[i]=1;if(nxt[i] == 0 || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0 ;
}
int match() {int sum = 0;memset(nxt,0,sizeof nxt);for(int i = 1; i<=tot1; i++) {memset(used,0,sizeof used);if(find(i)) sum++;}return sum;
}
int main() 
{int iCase = 0,t=0;scanf("%d",&t);while(t--) {tot1 = tot2 = 0;scanf("%d",&n);memset(vis1,0,sizeof vis1);memset(vis2,0,sizeof vis2);memset(line,0,sizeof line);for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '#') {if((i+j)&1) vis2[i][j] = ++tot2;else vis1[i][j] = ++tot1;}}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if((i+j)&1) continue;if(maze[i][j] == '#') {if(maze[i][j+1] == '#' && j+1 <= n) line[vis1[i][j]][vis2[i][j+1]]=1;if(maze[i][j-1] == '#' && j-1 >= 1) line[vis1[i][j]][vis2[i][j-1]]=1;if(maze[i+1][j] == '#' && i+1 <= n) line[vis1[i][j]][vis2[i+1][j]]=1;if(maze[i-1][j] == '#' && i-1 >= 1) line[vis1[i][j]][vis2[i-1][j]]=1;}}}printf("Case %d: %d\n",++iCase,match());}return 0 ;
}

建图方式2的AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,ans;
int vis[605][605],line[605][605];
bool used[605*605];
char maze[605][605];
int tot;
int nxt[605*605];
bool find(int x) {for(int i = 1; i<=tot; i++) {if(line[x][i] && !used[i]) {used[i]=1;if(nxt[i] == 0 || find(nxt[i])) {nxt[i] = x;return 1;}}}return 0 ;
}
int match() {int sum = 0;memset(nxt,0,sizeof nxt);for(int i = 1; i<=tot; i++) {memset(used,0,sizeof used);if(find(i)) sum++;}return sum;
}
int main() 
{int iCase = 0,t=0;scanf("%d",&t);while(t--) {
//		tot1 = tot2 = 0;tot=0;scanf("%d",&n);memset(vis,0,sizeof vis);memset(line,0,sizeof line);for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '#') {vis[i][j] = ++tot;}}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == '#') {if(maze[i][j+1] == '#' && j+1 <= n) line[vis[i][j]][vis[i][j+1]]=1;if(maze[i][j-1] == '#' && j-1 >= 1) line[vis[i][j]][vis[i][j-1]]=1;if(maze[i+1][j] == '#' && i+1 <= n) line[vis[i][j]][vis[i+1][j]]=1;if(maze[i-1][j] == '#' && i-1 >= 1) line[vis[i][j]][vis[i-1][j]]=1;}}}printf("Case %d: %d\n",++iCase,match()/2);}return 0 ;
}

总结:

   再好好想想建图!和POJ - 2226这个最小点覆盖的题比较一下,也是写过博客的。这俩题还是有共同之处的。

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python pandas read_csv 迭代器使用方法_pandas.read_csv参数详解(小结)

更多python教程请到友情连接&#xff1a; 菜鸟教程www.piaodoo.com人人影视www.sfkyty.com飞卢小说网www.591319.com韩剧网www.op-kg.com兴化论坛www.yimoge.cn星辰影院www.hhsos.netpandas.read_csv参数整理读取CSV(逗号分割)文件到DataFrame也支持文件的部分导入和选择迭代参…

【HDU - 2398 】Savings Account (水题模拟)

题干&#xff1a; Suppose you open a savings account with a certain initial balance. You will not make any withdrawals or further deposits for a number of years. The bank will compound your balance (add the annual interest) once a year, on the anniversary …

隧道凿岩机器人_隧道凿岩机器人的研制

隧道凿岩机器人的研制隧道、洞室开挖是现代交通、能源、采掘、建筑等大规模基本建设中的一项难度大、耗资耗时多、劳动条件差但又十分重要、十分关键的施工作业。早期的液压凿岩设备全都是由人工操作的液压凿岩钻车&#xff0c;其施工效率和施工精度完全取决于操作人员的熟练程…

【HDU - 1045】Fire Net (dfs 或二分图)

题干&#xff1a; Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to …

html 按钮 按下 状态_第一次按下是启动,第二次按下是停止,俵哥分享2种接线方法...

朋友们大家好我是大俵哥&#xff0c;今天我们来说一下单按钮启停电路。这个电路虽然应用的不多&#xff0c;但是非常的经典&#xff0c;新手朋友们可以拿来练手。今天我们讲2种控制方法&#xff0c;一种用中间继电器控制一种用时间继电器控制&#xff0c;在看电路之前&#xff…

【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造)

题干&#xff1a; You are given a positive integer nn. Let S(x)S(x) be sum of digits in base 10 representation of xx, for example, S(123)1236S(123)1236, S(0)0S(0)0. Your task is to find two integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n, abnabn and S(a)S(…

mysql 异步_MySQL -- 异步I/O

linux上&#xff0c;innodb使用异步IO子系统(native AIO)来对数据文件页进行预读和写请求。行为受到参数innodb_use_native_aio控制。默认是开启的&#xff0c;且只是适用于linux平台&#xff0c;需要libaio库。在其他的类unix平台上&#xff0c;innodb使用的是同步I/O。由于历…

【CodeForces - 633D】Fibonacci-ish (离散化,暴力枚举+STPmap,fib数列收敛性质)

题干&#xff1a; Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elementsf0 and f1 are arbitraryfn  2  fn  1  fn for all n ≥ 0. You …

sql server 迁移 mysql_【转】sql server迁移到mysql

【1】MSSQL2SQLSQL Server转换为MySQL工具&#xff0c;用了一下 感觉蛮不错的。分享上来&#xff0c;同时也以便记录下来以后自用。工具名称&#xff1a;Mss2sql来个操作流程:下载后打开压缩包运行mss2sql默认就是Move to MysQL server directly,选择下一步继续下一步,稍等片刻…

【51Nod - 1268】和为K的组合 (背包 或 dfs)

题干&#xff1a; 给出N个正整数组成的数组A&#xff0c;求能否从中选出若干个&#xff0c;使他们的和为K。如果可以&#xff0c;输出&#xff1a;"Yes"&#xff0c;否则输出"No"。 Input 第1行&#xff1a;2个数N, K, N为数组的长度, K为需要判断的和(…

centos7 mysql tar_CentOS7中mysql-5.7.21-el7-x86_64.tar.gz版MySQL的安装与配置

一、准备阶段通常情况下&#xff0c;MySQL在CentOS下主要使用glibc、rpm、yum等方式进行安装&#xff0c;使用mysql-5.7.21-el7-x86_64.tar.gz包进行安装的很少见&#xff0c;网上资料也较少。通过一上午的摸索&#xff0c;总结出如下安装方法。下载安装包&#xff1a;[rootGee…

【51Nod - 1416】两点 (dfs 或 并查集+dfs)

题干&#xff1a; 福克斯在玩一款手机解迷游戏&#xff0c;这个游戏叫做”两点”。基础级别的时候是在一个nm单元上玩的。像这样&#xff1a; 每一个单元有包含一个有色点。我们将用不同的大写字母来表示不同的颜色。 这个游戏的关键是要找出一个包含同一颜色的环。看上图中4…

linux 源码安装mysql5.7_linux安装mysql5.7.27

一、卸载mysql安装有三种方式&#xff0c;包括二进制包安装(Using Generic Binaries)、RPM包安装、源码安装。一般是前两种比较多二、安装建议路径设置按照写的来将下载的压缩包复制到linux服务器/usr/local/路径下(下载地址https://dev.mysql.com/downloads/mysql/,进去下载默…

【51Nod - 1010 】只包含因子2 3 5的数 (打表,有坑越界)

题干&#xff1a; K的因子中只包含2 3 5。满足条件的前10个数是&#xff1a;2,3,4,5,6,8,9,10,12,15。 所有这样的K组成了一个序列S&#xff0c;现在给出一个数n&#xff0c;求S中 > 给定数的最小的数。 例如&#xff1a;n 13&#xff0c;S中 > 13的最小的数是15&…

c语言可以将负数强制转换成正数吗_C语言笔记(一、概述)

1&#xff0e; C语言的特点 ①语言简洁、紧凑&#xff0c;使用方便、灵活。共有&#xff13;&#xff12;个关键字(也称保留字)&#xff0c;&#xff19;种控制语句。 ②运算符丰富&#xff0c;共有&#xff13;&#xff14;种运算符。 ③数据结构丰富&#xff0c;数据类型有&a…

mysql or全表_mysql or条件可以使用索引而避免全表

在某些情况下&#xff0c;or条件可以避免全表扫描的。1 .where 语句里面如果带有or条件, myisam表能用到索引&#xff0c; innodb不行。1)myisam表&#xff1a;CREATE TABLE IF NOT EXISTS a (id int(1) NOT NULL AUTO_INCREMENT,uid int(11) NOT NULL,aNum char(20) DEFAULT N…

【51Nod - 1163】最高的奖励 (贪心+优先队列 或 妙用并查集)

题干&#xff1a; 有N个任务&#xff0c;每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务&#xff0c;就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的&#xff0c;因为时间上可能会有冲突&#xff0…

mysql varchar java_关于MySQL varchar类型最大值,原来一直都理解错了

写在前面关于MySQL varchar字段类型的最大值计算&#xff0c;也许我们一直都理解错误了&#xff0c;本文从问题出发&#xff0c;经实践验证得出一些实用经验&#xff0c;希望对大家的开发工作有些帮助~背景描述最近同事在做技术方案设计时候&#xff0c;考虑到一个表设计时希望…

【CodeForces - 1027C】Minimum Value Rectangle (数学,公式化简,思维,卡常卡memset)

题干&#xff1a; You have nn sticks of the given lengths. Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can …

mysql数据库业务逻辑_Mysql业务设计(逻辑设计)

逻辑设计数据库设计三大范式数据库设计第一大范式数据库表中所有的字段都只具有单一属性单一属性的列是由基本数据类型所构成设计出来的表都是简单的二维表数据库设计的第二大范式要求表中只有一个业务主键&#xff0c;也就是说符合第二范式的表不能存在非主键列&#xff0c;只…