Codevs 1022 覆盖
- 时间限制: 1 s
- 空间限制: 128000 KB
- 题目等级 : 大师 Master
题目描述 Description
有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地。如果要用1×2的矩阵区覆盖(覆盖过程不容许有任何部分重叠)这个陆地,那么最多可以覆盖多少陆地面积。

输入描述 Input Description
输入文件的第一行是两个整数N,M (1<=N,M<=100),第二行为一个整数K(K<=50),接下来的K行,每行两个整数X,Y表示K个水塘的行列位置。(1<=X<=N,1<=Y<=M) 。
输出描述 Output Description
输出所覆盖的最大面积块(1×2面积算一块)。
样例输入 Sample Input
4 4
6
1 1
1 4
2 2
4 1
4 2
4 4
样例输出 Sample Output
4
题解:
对于每个空格,向它的上下左右连一条边,跑一遍匈牙利算法就好。
#include <iostream>
using namespace std;
int n, m, k, ans;
bool g[105][105], lian[10005], used[10005], line[10005][10005];
int hash(int x, int y)
{
return (x - 1) * m + y;
}
bool find(int x)
{
int xl, xh;
if(x % m)
{
xl = x % m;
xh = x / m + 1;
}
else
{
xl = m;
xh = x / m;
}
if(g[xh][xl])
return false;
for (int i = 1; i <= n * m; ++i)
if (line[x][i] && !used[i])
{
used[i] = true;
if(lian[i] == 0 || find(lian[i]))
{
lian[i] = x;
return true;
}
}
return false;
}
int main()
{
int h, l;
cin >> n >> m >> k;
for (int i = 0; i < k; ++i)
{
cin >> h >> l;
g[h][l] = true;
}
int ih, il;
for (int i = 1; i <= n * m; ++i)
{
if(i % m)
{
il = i % m;
ih = i / m + 1;
}
else
{
il = m;
ih = i / m;
}
if (!g[ih][il])
{
if (ih > 1)
if (!g[ih - 1][il])
line[i][i - m] = true;
if (il > 1)
if (!g[ih][il - 1])
line[i][i - 1] = true;
if (ih < n)
if (!g[ih + 1][il])
line[i][i + m] = true;
if (il < m)
if (!g[ih][il + 1])
line[i][i + 1] = true;
}
}
unsigned ans = 0;
for (int i = 1; i <= n * m; ++i)
{
for (int j = 1; j <= n * m; ++j)
used[j] = false;
if (find(i))
++ans;
}
cout << ans / 2;
}
Codevs 1022 覆盖的更多相关文章
- Code[VS] 1022 覆盖 题解
Code[VS] 1022 覆盖 题解 Hungary Algorithm 题目传送门:Code[VS] 1022 题目描述 Description 有一个N×M的单位方格中,其中有些方格是水塘,其 ...
- 【CODEVS】1022 覆盖
[算法]二分图匹配(最大流) [题解]对i+j进行奇偶染色,就可以保证相邻两格异色. 然后就是二分图了,对相邻格子连边跑最大流即可. #include<cstdio> #include&l ...
- 【wikioi】1022 覆盖(匈牙利)
http://www.wikioi.com/problem/1022/ 好不容易来一次1A,,水题啊.. 染色后裸匈牙利orz #include <cstdio> #include < ...
- CodeVS 线段覆盖1~5
#include <bits/stdc++.h> using namespace std; ; struct Info{int l,r;}P[Maxn]; int n,Cnt,F[Maxn ...
- codevs 版刷计划(1000-1099)
Diamond咋都是模板题... 开个坑刷codevs的Master题.巩固一下姿势. 目前AC的题目:1001,1021,1022, 1001.舒适的路线(并查集) 求出无向图s到t路径上的min( ...
- 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)
3589: 动态树 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 405 Solved: 137[Submit][Status][Discuss] ...
- 匈牙利算法实战codevs1022覆盖
1022 覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有一个N×M的单位方格中 ...
- 【经典】Noip贪心
一.经典例题 例一:排队接水 题意:n个人到r个水龙头接水,装满水桶的时间分别是t1,t2,t3.... 接水时间是整数且互不相等,怎样安排顺序使每个人等待的 时间和最小. 题解:排队越靠前计算次数越 ...
- codevs——T1214 线段覆盖
http://codevs.cn/problem/1214/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
随机推荐
- tomcat 部署spring工程乱码解决方案
tomcat的server.xml文件,Connector标签增加useBodyEncodingForURI="true"属性 web.xml如下配置 <filter> ...
- BZOJ 3110 k大数查询 & 树套树
题意: 有n个位置,每个位置可以看做一个集合,现在要求你实现一个数据结构支持以下功能: 1:在a-b的集合中插入一个数 2:询问a-b集合中所有元素的第k大. SOL: 调得火大! 李建说数据结构题能 ...
- gson 简要使用
http://www.cnblogs.com/chenlhuaf/archive/2011/05/01/gson_test.html 发现了google的gson,因为之前对于protocolbuf有 ...
- POJ 2420 A Star not a Tree?(模拟退火)
题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...
- 【BZOJ2223/3524】[Coci 2009]PATULJCI
Description Input Output 10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10 Sample Input ...
- 【JAVA】Spring 数据源配置整理
在Spring中,不但可以通过JNDI获取应用服务器的数据源,也可以直接在Spring容器中配置数据源,此外,还可以通过代码的方式创建一个数据源,以便进行无依赖的单元测试. 配置数据源 ...
- insert into select 与select into from -- sql 批量插入
参考资料:http://www.w3school.com.cn/sql/sql_union.asp UNION:操作符用于合并两个或多个select语句的结果集. ...
- 实现UITableView循环利用
tableViewUITableView循环利用 前言 大家都知道UITableView,最经典在于循环利用,这里我自己模仿UITableView循环利用,写了一套自己的TableView实现方案,希 ...
- Flex与.net进行URL参数传递编码处理
在JS中用到的三种编码方式escape 对应于Flex中是一样的,并且支持相互的解码 var a:String = "超越梦想#"; trace(escape(a)); //%u8 ...
- Log打印日志遇到的问题
Log日志打印出现空指针问题 AndroidRuntime(372): Caused by: java.lang.NullPointerException: println needs a messa ...