LightOJ--1152--Hiding Gold(二分图奇偶建图)(好题)
来源:互联网 发布:ajax json servlet 编辑:程序博客网 时间:2024/04/28 16:06
SubmitStatus
Description
You are given a 2D board where in some cells there are gold. You want to fill the board with2 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 the2 x 1 dominoes. The dominoes may overlap, as we already said, as shown in the picture. In reality the dominoes will cover the full2 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) andn (1 ≤ n ≤ 20) and m * n > 1. Here m represents the number of rows, andn represents the number of columns. Then there will be m lines, each containingn 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 500char 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;elsemap[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(二分图奇偶建图)(好题)
- lightoj 1152 - Hiding Gold 【奇偶建二分图 求最大匹配】
- LightOJ 1152 - Hiding Gold【二分图】
- LightOJ - 1152 Hiding Gold(二分图匹配)
- Lightoj 1152 Hiding Gold
- LightOJ - 1152 Hiding Gold /poj 3041 二分匹配@
- LightOJ 1152 Hiding Gold 最大匹配
- Hiding Gold LightOJ
- LightOJ 1030 - Discovering Gold (期望dp)
- LightOJ 1030 - Discovering Gold(dp)
- LightOJ 1057 - Collecting Gold(dp)
- LightOJ 1057 - Collecting Gold (状压dp)
- [LightOJ 1030] Discovering Gold (概率DP)
- lightoj 1030discovering gold(概率dp)
- 【LightOJ 1030】Discovering Gold(期望DP)
- poj 3020 二分图(奇偶图) 最小路径覆盖
- lightoj 1138 (二分)
- LightOJ - 1149 Factors and Multiples(二分图匹配)
- EventBus使用详解(一)——初步使用EventBus
- Android的生成日志文件
- Github如何上传gif图片
- Android中短信发送倒计时方法
- WEB小结(2)——一次提交多个表单
- LightOJ--1152--Hiding Gold(二分图奇偶建图)(好题)
- 在特定的div中增加按钮
- 简单排序算法
- BZOJ 3262 陌上花开、HDU 5618 Jam's problem again(三维偏序、cdq分治 + BIT)
- 通过char **指针改变char*指针的指向
- UESTC 1269 ZhangYu Speech
- 嵌入式Linux之设备驱动程序
- Web服务器的工作原理
- PictureChooser