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 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
题意描述:
输入矩阵,‘*’表示金矿,‘o’表示空地,现在使用2*1或者1*2的材料将这些金矿盖住,问至少需要几块这样的材料。
解题思路:
很容易想到,不管使用一块2*1的材料还是1*2的材料,他们所盖住的两个金矿一定是一个奇数位,一个偶数位,那么采用二分图的思想建模,将所有的金矿进行编号,使用匈牙利算法
进行匹配,求出最大匹配数即可。
AC代码:
#include<stdio.h>
#include<string.h>
char G[][];
int r,c,count,g[][],map[][],book[],match[];
void get_map();
int maxmatch();
int path(int u);
int main()
{
int T,i,j,t=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&r,&c);
for(i=;i<r;i++)
scanf("%s",G[i]);
memset(g,,sizeof(g));
count=;
for(i=;i<r;i++)
for(j=;j<c;j++)
if(G[i][j]=='*')
g[i][j]= ++count;
memset(map,,sizeof(map));
get_map();
/*for(i=1;i<=count;i++)
{
for(j=1;j<=count;j++)
printf("%3d",map[i][j]);
printf("\n");
}*/
//printf("%d\n",maxmatch());
printf("Case %d: %d\n",t++,count-maxmatch());
/*for(i=1;i<=count;i++)
printf("%d和%d匹配\n",i,match[i]);*/
}
return ;
}
void get_map()
{
int i,j,k,tx,ty;
int next[][]={,,,,,-,-,};
for(i=;i<r;i++)
{
for(j=;j<c;j++)
{
if(G[i][j]=='*')
{
for(k=;k<=;k++)
{
tx=i+next[k][];
ty=j+next[k][];
if(tx < || tx >= r || ty < || ty >= c)
continue;
if(G[tx][ty]=='*')
map[g[i][j]][g[tx][ty]]=;
}
}
}
}
}
int maxmatch()
{
int i,res=;
memset(match,,sizeof(match));
for(i=;i<=count;i++)
{
if(!match[i])
{
memset(book,,sizeof(book));
res += path(i);
}
}
return res;//返回最大匹配数
}
int path(int u)
{
int i;
for(i=;i<=count;i++)
{
if(map[u][i] && !book[i])
{
book[i]=;
if(!match[i] || path(match[i]))
{
match[i]=u;
match[u]=i;//避免重复
return ;
}
}
}
return ;
}
light oj 1152 Hiding Gold的更多相关文章
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- Light OJ 1030 - Discovering Gold
题目大意: 给你一个1*N的方格,你初始位置是在1,给你一个骰子,假设你现在的位置是X,你投掷一个骰子掷的点数是y, 那么你的新位置就是 X+y, 并且你可以得到新位置的宝藏.假如X+y > N ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
随机推荐
- JDK8中JVM对类的初始化探讨
在<深入理解Java虚拟机>(第二版,周志明著)中,作者介绍了JVM必须初始化类(或接口)的五种情况,但是是针对JDK7而言的. 那么,在JDK8中,这几种情况有没有变化呢?(我猜测应该会 ...
- 随便说说 post-processing
九月份一篇博都没更新,这段时间一直在unity的坑里爬不起来,感觉真的很绝望啊,仿佛对生活都失去了信心. 渲染问题并没有解决,目前方案只是减轻视觉冲突,降低违和感.项目AR产品也做的越来越艰难,开始经 ...
- js构建函数,点击按钮显示div,再点击按钮或其他区域,隐藏div
这只是一个例子,先看看效果: html代码: <nav> <span class="nav_logo"></span> <h1>云蚂 ...
- Effective Java 第三版——13. 谨慎地重写 clone 方法
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- VS 2017 Web项目需要安装Sql Server 2012 Express LocalDB问题
最近在做mvc5的练习 ,结果到了数据库连接这一阶段就出现了问题,开始我以为<add name="MovieDBContext" connectionString=" ...
- Linux程序包管理rpm与yum
Linux程序包管理 Linux中软件的安装主要有两种形式:一种是直接下载源代码包自行编译后安装,另一种直接获取rpm软件包进行安装. 程序的组成部分: 二进制程序:程序的主体文件,比如我们运行一个l ...
- ABP 框架从源码学习——abp框架启动核心类AbpBootstrapper(2)
在AbpBootstrapper中的两个至关重要的属性:IIocManager 和 IAbpModuleManager public class AbpBootstrapper : IDisposa ...
- celery出现警告或异常的解决方式
做个笔记,记录下使用celery踩过的坑,不定期更新. warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED)) 我用的是Flask,所以在Fl ...
- jquery tips简易使用方法 新手必看
使用jquery1.12.4以上版本 使用jquery插件 tips .beg-pull-right 点击时的选择器 在这里写的是一个类选择器 记得引入jquery $(".beg-pu ...
- (译)ABP之Entities
原文地址:https://aspnetboilerplate.com/Pages/Documents/Entities#DocAuditing 实体是DDD(领域驱动模型)的核心概念之一,Eric E ...