LightOJ--1152--Hiding Gold(二分图奇偶建图)(好题)
| Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
Description
You are given a 2D board where in some cells there are gold. You want to fill the board with
2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cover the gold with least number of dominoes.
In the picture, the golden cells denote that the cells contain gold, and the blue ones denote the
2 x 1 dominoes. The dominoes may overlap, as we already said, as shown in the picture. In reality the dominoes will cover the full
2 x 1 cells; we showed small dominoes just to show how to cover the gold with 11 dominoes.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with a row containing two integers m (1 ≤ m ≤ 20) and
n (1 ≤ n ≤ 20) and m * n > 1. Here m represents the number of rows, and
n represents the number of columns. Then there will be m lines, each containing
n characters from the set ['*','o']. A
'*' character symbolizes the cells which contains a gold, whereas an
'o' character represents empty cells.
Output
For each case print the case number and the minimum number of dominoes necessary to cover all gold ('*' entries) in the given board.
Sample Input
2
5 8
oo**oooo
*oo*ooo*
******oo
*o*oo*oo
******oo
3 4
**oo
**oo
*oo*
Sample Output
Case 1: 11
Case 2: 4
Source
题意:有n*m的图,*表示金矿所在地,要用2*1或者1*2的骨牌将金矿全部覆盖,问最少需要多少骨牌
将所有的金矿编号,根据他们的坐标和,坐标和为奇数跟偶数的分别编号,并记录相应的个数oddnum和evennum,因为骨牌只有2*1跟1*2的,这也就是说一个金矿只能跟相邻的金矿相连,现在就从坐标和为奇数的点开始向坐标和为偶数的点建边,求出最大匹配数,金矿总数减去最大匹配数就是要求的
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 500
char s[25][25];
int used[MAXN],pipei[MAXN];
int map[25][25],m,n,oddnum,evennum,k=1;
vector<int>G[MAXN];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void getmap()
{
memset(map,0,sizeof(0));
for(int i=0;i<500;i++)
G[i].clear();
oddnum=evennum=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
map[i][j]=-1;
if(s[i][j]=='*')
{
if((i+j)&1)
map[i][j]=++oddnum;
else
map[i][j]=++evennum;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j]=='*'&&((i+j)&1))
{
for(int k=0;k<4;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(x<0||x>=n||y<0||y>=m)
continue;
if(s[x][y]=='*')
G[map[i][j]].push_back(map[x][y]);
}
}
}
}
}
int find(int x)
{
for(int i=0;i<G[x].size();i++)
{
int y=G[x][i];
if(!used[y])
{
used[y]=1;
if(pipei[y]==-1||find(pipei[y]))
{
pipei[y]=x;
return 1;
}
}
}
return 0;
}
void solve()
{
memset(pipei,-1,sizeof(pipei));
int sum=0;
for(int i=1;i<=oddnum;i++)
{
memset(used,0,sizeof(used));
sum+=find(i);
}
printf("Case %d: %d\n",k++,oddnum+evennum-sum);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(s,'\0',sizeof(s));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
getmap();
solve();
}
return 0;
}
LightOJ--1152--Hiding Gold(二分图奇偶建图)(好题)的更多相关文章
- 4185 Oil Skimming 最大匹配 奇偶建图
题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合 ...
- hdoj--5093--Battle ships(二分图经典建图)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- joj 2453 candy 网络流建图的题
Problem D: Candy As a teacher of a kindergarten, you have many things to do during a day, one of whi ...
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- POJ 2195 一人一房 最小费用流 建图 水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21010 Accepted: 10614 Desc ...
- light oj 1152 Hiding Gold
题目: You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x ...
- [SCOI2007]修车(建图好题)
[SCOI2007]修车 https://www.lydsy.com/JudgeOnline/problem.php?id=1070 Time Limit: 1 Sec Memory Limit: ...
- HDU1853 & 蜜汁建图+KM模板
题意: 给你一个N个点M条边的带权有向图,现在要你求这样一个值:该有向图中的所有顶点正好被1个或多个不相交的有向环覆盖.这个值就是 所有这些有向环的权值和. 要求该值越小越好. SOL: 本来还想ta ...
随机推荐
- dubbo之多协议
(1) 不同服务不同协议 比如:不同服务在性能上适用不同协议进行传输,比如大数据用短连接协议,小数据大并发用长连接协议 consumer.xml <?xml version="1.0& ...
- SLAM:飞行机器人的参数解析-分类
在水电站存在的山区,公路运输效率极低,盘山公路绕行消耗大量时间,使用飞行机器人进行运输是合适的选择. 实现一位长辈在山区飞行的愿望,任重而道远 常见飞行机器人的参数解析:解读飞行机器人的基本类型及技术 ...
- Java:Java 队列的遍历
Java队列到底有没有可以遍历的功能呢?暂且试一下吧 参考链接:stl容器遍历测试 1.LinkedList实现简单遍历 for(Iter =LocTimesSerials.size()-1; iSe ...
- 移动端mui常用方法
本文分享一些用Mui的时候所采的坑 1.mui中上拉刷新事件a标签中的链接.元素onclick事件在手机上点击不了 mui('body').on('tap','a',function(){docume ...
- 团体程序设计天梯赛-练习集-L1-048. 矩阵A乘以B
L1-048. 矩阵A乘以B 给定两个矩阵A和B,要求你计算它们的乘积矩阵AB.需要注意的是,只有规模匹配的矩阵才可以相乘.即若A有Ra行.Ca列,B有Rb行.Cb列,则只有Ca与Rb相等时,两个矩阵 ...
- 【转载】使用 IntelliJ IDEA 新建一个 web项目
IntelliJ IDEA 创建Web项目(全教程) 说明:IntelliJ IDEA 版本为14.JDK 版本为1.7tomcat 版本为apache-tomcat-7.0.70 注:在创建过程 ...
- JVM 性能调优监控工具 jps、jstack、jmap、jhat、jstat、hprof 使用详解
转自: https://my.oschina.net/feichexia/blog/196575 摘要: JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConso ...
- 【JavaScript高级进阶】JavaScript变量/函数提升的细节总结
// 测试1 console.log('----------test1--------------'); console.log(global); // undefined var global = ...
- dubbo-helloword(二)
项目框架搭建 工程目录创建 entity存放业务实体类 interface存放server接口 concumer是服务消费者(依赖interface和entity) provider是服务提供者(依赖 ...
- netty自定义协议 心跳 断线重连源码
https://github.com/aa1356889/NettyHeartbeat