题目:

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的更多相关文章

  1. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

  2. Light OJ 1030 - Discovering Gold

    题目大意: 给你一个1*N的方格,你初始位置是在1,给你一个骰子,假设你现在的位置是X,你投掷一个骰子掷的点数是y, 那么你的新位置就是 X+y, 并且你可以得到新位置的宝藏.假如X+y > N ...

  3. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  4. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  5. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  6. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  7. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  8. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  9. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

随机推荐

  1. Nginx 错误处理方法: bind() to 0.0.0.0:80 failed

    Nginx 错误处理方法: bind() to 0.0.0.0:80 failed 今天启动window上的nginx总是报错 错误信息是bind() to 0.0.0.0:80 failed (10 ...

  2. js 深入理解原型模式

    我们创建每一个函数都有一个prototype(原型)属性,这个属性是一个指针,指向一个对象.使用原型的好处是可以让所有对象共享它所包含的属性和方法. function Person(){ } Pers ...

  3. ES6 let和const命令(4)

    const声明的常量只在当前代码块有效.如果想设置跨模块的常量,可以采用下面的写法. //constants.js模块 export const A = 1; export const B = 3; ...

  4. Elasticsearch索引自动删除

    简介 脚本分2部分,1部分查找符合条件的索引名,2脚本调用1脚本,进行删除操作 脚本 查找符合条件的,默认大于30天 # coding:utf-8 __author__ = 'Jipu FANG' f ...

  5. Android短视频SDK转码实践

    一. 前言 一些涉及的基本概念: 转码:一般指多媒体文件格式的转换,比如分辨率.码率.封装格式等: 解复用(demux):从某种封装中分离出视频track和音频track,然后交给后续模块进行处理: ...

  6. Android测试:Building Local Unit Tests

    原文:https://developer.android.com/training/testing/unit-testing/local-unit-tests.html 如果你的单元测试没有依赖或者只 ...

  7. java递归实现文件夹文件的遍历输出

    学习java后对一个面试小题(今年年初在团结湖面试的一个题目) 的习题的编写. ''给你一个文件,判断这个文件是否是目录,是目录则输入当前目录文件的个数和路径,''' /** * @author li ...

  8. Python sort方法

    官方文档: sort(*, key=None, reverse=False) This method sorts the list in place, using only < comparis ...

  9. SVG 入门——理解viewport,viewbox,preserveAspectRatio

    工欲善其事必先利其器,没有真正搞懂SVG里的viewport,viewbox, preserveAspectRatio这三个属性,就很容易遇到坑,最近写项目用到svg这三个属性被我一眼就略过 ,后来发 ...

  10. Emmet for Dreamweaver 整理分享

    我是一名技术不是很到位的前端,每次做项目总要写大量的HTML和CSS,耳边经常听到的是快.快点.再快点!我真想说快你妹!但是,我不得不承认的是:我只有两只手... 后来,在群里看到有人分享了一个连接大 ...