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个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
随机推荐
- ES6 Proxy和Reflect(下)
construct() construct方法用于拦截new命令. var handler = { construct (target, args) { return new target(...ar ...
- vue基础入门
Hello World <body> <!-- 在angularJS中用ng-model --> <!-- {{mseeage?message:11}}支持三元表达式 ...
- c#的关键字
abstract as base bool break byte case catch char checked decimal default delegate continue double do ...
- linux下新建svn项目
1.新建项目svnadmin create /mnt/fbdisk/svn/newproject 2.会在svn下面建立newproject目录total 24drwxr-xr-x 2 root ro ...
- SVN添加用户权限
点击properties
- 关于vs2010下水晶报表的使用入门
关于vs2010下使用水晶报表了解情况记录如下: 1.首先vs2010不再自带水晶报表控件了,需要下载安装vs2010配套的水晶报表控件:CRforVS_13_0.这个控件安装很简单,基本上都选择默认 ...
- leetcode — copy-list-with-random-pointer
import java.util.*; /** * * Source : https://oj.leetcode.com/problems/copy-list-with-random-pointer/ ...
- socket对象放在一个datagridview的row的tag里面在拿出来 为什么是已释放
socket对象放在一个datagridview的row的tag里面在拿出来 为什么是已释放
- Oracle死锁情况
ORACLE EBS操作某一个FORM界面,或者后台数据库操作某一个表时发现一直出于"假死"状态,可能是该表被某一用户锁定,导致其他用户无法继续操作 复制代码 代码如下: --锁表 ...
- JDK中AbstractQueuedSynchronizer应用解析
这个类首先是一个抽象类,定义了一个模板,很多java同步相关的类(ReetrantLock.Semaphore.CountDownLatch等)都是基于AbstractQueuedSynchroniz ...