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 ...
随机推荐
- 在Linux下如何创建LVM及LVM创建过程
Linux LVM创建过程:(我用的是Centos6.8) 一.准备工作: LVM可以的创建可以在系统安装的过程中创建,也可以在安装完系统之后再创建建,都是可以的,我的是在系统安装完之后创建的,具体的 ...
- 对于IE6及以下版本的处
判断IE6 CSS 通过判断浏览器类型而加载不同的css样式 所有的IE都起作用: <!--[if IE]> <link rel="stylesheet" typ ...
- MySQL整数类型说明 int(11) vs int(20)
整数类型后面跟的是显示的宽度.M指示最大显示宽度.最大有效显示宽度是255.显示宽度与存储大小或类型包含的值的范围无关. 实践出真知: mysql> create table test2 ( a ...
- python 使用 tweepy 案例: PS4
First, make sure Python and Tweepy installed well, and the network setup well. Then, you go to http: ...
- dojo.io.script
dojo.io.script 定义: 跨域访问数据,可以动态的将script标签插入到网页当中. 局限: 1.只支持get方式访问: 2.只支持异步调用. 使用: 1.dojo.io.script.g ...
- ASP.NET MVC 常用内置验证特性
1.[Required] : 必须输入 [Required(ErrorMessage = "请输入用户名")] 2.[StringLength] : 限制字符串长度 [String ...
- Unique Binary Search Trees In JAVA
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Oracle查询指定某一天数据,日期匹配
在做一个功能的时候,需要在oracle数据库中查询指定某一天的数据. 如果是简单的当前日期前后几天,也好办 AND TO_CHAR(Rct.Creation_Date, 'YYYY-MM-DD')=t ...
- hdu2399GPA
Problem Description Each course grade is one of the following five letters: A, B, C, D, and F. (Note ...
- css基础之 联网使用bootstrap
在<head></head>中添加 <meta charset="utf-8"> <meta http-equiv="X-UA- ...