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.
The input starts with an integer K ( <= K <= ) indicating the number of cases. Each case starts with an integer N ( <= N <= ) 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.
For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from ) and M is the maximum number of scoops of oil that may be extracted.
......
.##...
.##...
....#.
....##
......
Case :
题意:求能够找到最多多少个“##”(横或竖都行)
解题:关键是建图,在建图时,先用tmp[][]数组将‘#’的数字存起来(tmp=0开始),然后再遍历一遍,如果遇到的是mp[i][j]=='#',则将上下左右是‘#’的标记为cnt[ tmp[][] ][tmp[][] ]=1,最后就是经典的匈牙利算法了。
犯了个致命的错误,在匈牙利算法时所对应的n应该是num,即重新建图后的‘n’。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 606
#define inf 1e12
int n;
char mp[N][N];
int tmp[N][N];
int cnt[N][N];
int match[N];
int vis[N];
int tot;
bool dfs(int u){
for(int i=;i<tot;i++){
if(!vis[i] && cnt[u][i]){
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=u;
return true;
}
}
}
return false;
} int solve(){
int ans=;
memset(match,-,sizeof(match));
for(int i=;i<tot;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
} return ans;
}
int main()
{
int t,ac=;;
scanf("%d",&t);
while(t--){
memset(tmp,,sizeof(tmp));
scanf("%d",&n);
int num=;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<n;j++){
if(mp[i][j]=='#'){
tmp[i][j]=num++;
}
}
}
tot=num;
memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(mp[i][j]!='#') continue;
if(i> && mp[i-][j]=='#') cnt[tmp[i][j]][tmp[i-][j]]=;
if(i<n- && mp[i+][j]=='#') cnt[tmp[i][j]][tmp[i+][j]]=;
if(j> && mp[i][j-]=='#') cnt[tmp[i][j]][tmp[i][j-]]=;
if(j<n- && mp[i][j+]=='#') cnt[tmp[i][j]][tmp[i][j+]]=;
}
}
printf("Case %d: ",++ac);
int ans=solve();
printf("%d\n",ans/);
}
return ;
}
hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)的更多相关文章
- HDU 4185 ——Oil Skimming——————【最大匹配、方格的奇偶性建图】
Oil Skimming Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- HDU4185 Oil Skimming 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8231146.html 题目传送门 - HDU4185 题意概括 每次恰好覆盖相邻的两个#,不能重复,求最大覆盖次 ...
- HDU 4185 Oil Skimming
题目大意:在一个N*N的矩阵里寻找最多有多少个“##”(横着竖着都行). 题目分析:重新构图,直接以相邻的两个油井算中间算以条边,然后进行匹配,看看两两之间最多能匹配多少对. #include ...
- HDU 4185 Oil Skimming 【最大匹配】
<题目链接> 题目大意: 给你一张图,图中有 '*' , '.' 两点,现在每次覆盖相邻的两个 '#' ,问最多能够覆盖几次. 解题分析: 无向图二分匹配的模板题,每个'#'点与周围四个方 ...
- hdoj--5093--Battle ships(二分图经典建图)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- poj--1149--PIGS(最大流经典建图)
PIGS Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- 4185 Oil Skimming 最大匹配 奇偶建图
题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合 ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- HDU 1083 网络流之二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...
随机推荐
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- Permutation Sequence 解答
Question The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...
- 【转】__attribute__机制介绍
1. __attribute__ GNU C的一大特色(却不被初学者所知)就是__attribute__机制. __attribute__可以设置函数属性(Function Attribute).变量 ...
- linux系统如何限制远程登录ip
在Linux系统上限制远程登录的IP,使用系统自带的配置文件. /etc/hosts.allow /etc/hosts.deny 匹配原则 先allow 后deny. 要求: 只允许 192.168 ...
- swfobject.js 2.2简单使用方法
swfobject.js 2.2简单使用方法 官方网址介绍http://code.google.com/p/swfobject/wiki/documentation 用法:html部分<div ...
- 清理SQL数据库日志
Use DBSelect NAME,size From sys.database_files ALTER DATABASE DB SET RECOVERY SIMPLE WITH NO_WAIT AL ...
- JS 不定函数参数argument的用法
本篇文章只要是对js的隐含参数(arguments,callee,caller)使用方法进行了介绍. arguments arguments 该对象代表正在执行的函数和调用它的函数的参数. [func ...
- Citrix 服务器虚拟化之十一 Xenserver管理vApps
Citrix 服务器虚拟化之十一 Xenserver管理vApps vApps是把几个业务相关的虚拟机作为一个单一实体管理,把vApps中的虚拟机的称为Application.启动vApps时其中包 ...
- 微信JS-SDK说明文档及常见问题处理
概述 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微 ...
- 导出kettle数据转换设置
在本地测试好的数据清洗配置,要转移到服务器上,就要用到导出数据转换设置. 一直是这使用是在导入设置的 时候总是提示不是kettle的文档格式.后来才发现这是资源库的配置,而不是数据转 换的配置. 导出 ...