HDU 3360 National Treasures 奇偶匹配的最低点覆盖
标题来源: pid=3360">HDU 3360 National Treasures
意甲冠军:假设a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 假设相应位是1 说明在那里放一个守卫能够看住a[i][j]位置上的这个东西
思路:明显死最小点覆盖 奇偶匹配建图
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 55;
int vis[maxn*maxn];
int y[maxn*maxn];
vector <int> G[maxn*maxn];
int n, m;
int a[maxn][maxn];
int dir[12][2] = {-1, -2, -2, -1, -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, 0, 0, 1, 1, 0, 0, -1};
bool dfs(int u)
{
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(vis[v])
continue;
vis[v] = true;
if(y[v] == -1 || dfs(y[v]))
{
y[v] = u;
return true;
}
}
return false;
}
int match()
{
int ans = 0;
memset(y, -1, sizeof(y));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if((i+j)%2)
{
memset(vis, 0, sizeof(vis));
if(dfs(i*m+j))
ans++;
}
}
}
return ans;
} int main()
{
int cas = 1;
while(scanf("%d %d", &n, &m) && (n||m))
{
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf("%d", &a[i][j]);
for(int i = 0; i < n*m; i++)
G[i].clear();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
int x = a[i][j];
if(x == -1)
continue;
for(int k = 0; k < 12; k++, x >>= 1)
{
if(!x)
break;
if(!(x&1))
continue; int xx = i + dir[k][0];
int yy = j + dir[k][1];
if(xx < 0 || xx >= n || yy < 0 || yy >= m)
continue;
if(a[xx][yy] == -1)
continue;
if((i+j)%2)
G[i*m+j].push_back(xx*m+yy);
else
G[xx*m+yy].push_back(i*m+j);
}
}
}
printf("%d. %d\n", cas++, match());
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
HDU 3360 National Treasures 奇偶匹配的最低点覆盖的更多相关文章
- HDU 3360 National Treasures(二分匹配,最小点覆盖)
National Treasures Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3360 National Treasures(最小点覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3360 题目大意: 在一个n*m的格子中,每个格子有一个数值,-1表示空,其余表示财宝.每个财宝的数值转 ...
- HDU 3360 National Treasures
题目大意:大厅每个位置都有一个文物或者一个守卫,文物是安全的前提是: 关键位置上必须有一个守卫,或者文物本身的位置上有一个守卫.求保证每个文物是安全的守卫的最少数量. #include <cst ...
- HDU 1083 网络流之二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- hdu 2255 二分图最大权匹配 *
题意:说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房 ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- HDU(1853),最小权匹配,KM
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...
- HDU(3605),二分图多重匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others) ...
随机推荐
- css3-10 如何控制元素的显示和隐藏(display和visibility的区别是什么)
css3-10 如何控制元素的显示和隐藏(display和visibility的区别是什么) 一.总结 一句话总结:使用的时候直接在元素的样式中设置display和visibility属性即可.推荐使 ...
- 《iOS Human Interface Guidelines》——Edit Menu
编辑菜单 用户能够显示一个编辑菜单来在文本视图.网页视图和图像视图运行诸如剪切.粘贴和选择的操作. 你能够调整一些菜单的行为来在你的app中给用户给多的内容控制.比方你能够: 指定哪一个标准菜单命令对 ...
- [Docker] Create a Volume
We can create volumn to keep the data, even we stop the container and restart again, the data won't ...
- [Git] How to rename your remote branch
Rename your local foo branch with bar: git branch -m foo bar Remember this will add the new branch w ...
- 【转】A* A星 算法 C语言 实现代码
http://blog.csdn.net/shanshanpt/article/details/8977512 关于A*算法,很早就想写点什么,可是貌似天天在忙活着什么,可事实又没有做什么,真是浮躁啊 ...
- springMVC返回json数据乱码问题及@RequestMapping 详解
原文地址:https://blog.csdn.net/u010127245/article/details/51774074 一.@RequestMapping RequestMapping是一个用来 ...
- Ehcache配置
http://blog.csdn.net/lwx2615/article/details/5624388 http://www.cnblogs.com/hoojo/archive/2012/07/12 ...
- Linux中vim中出现H不能正常编辑的问题
使用Linux中,由于是远程操作,我使用crt,由于有的文档有乱码,我就设置了一下session的字符... vim出现问题,下方出现H,导致不能正常编辑... 耗费一下午的时间,在高人的指点之下,终 ...
- hibernate基本配置与简单增删改查
ORM(Object Relation Mapping)是对象关系映射,是一个思想,它的作用是在关系数据库与对象之间做一个自动映射,将数据库中的表格映射到一个类,也就是持久化类,数据表中每行映射为对象 ...
- zookeeper 客户端操作
代码 /** * 创建zk客户端 * 实现循环监听的两个必要条件:1.程序不能结束2.递归调用监听器 * @author tele * */ public class Demo { ; //多个节点用 ...