Lightoj 1152 Hiding Gold(二分图匹配)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/82106662

1152 - Hiding Gold

    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

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

Output for Sample Input

2

5 8

oo**oooo

*oo*ooo*

******oo

*o*oo*oo

******oo

3 4

**oo

**oo

*oo*

Case 1: 11

Case 2: 4

 


PROBLEM SETTER: JANE ALAM JAN

题目大意:n*m的方格中有一些金块,要求用1*2的多米诺骨牌覆盖他们,问最少要多少金块

将那些有金块的点进行标号,标完号以后扫一遍图,对于一个金块的上下左右,只要有金块,就连上一条边,跑一遍匈牙利,最后得出的匹配数还不是答案,首先应该将匹配数除以2,这才是真正匹配到的,然后应该将金块总数减去匹配数(没有除以2的),这就是那些游离的没有被匹配的金块数量,它们还需要一个多米诺

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=30;
const int maxm=1010;
char str[maxn][maxn];
int map[maxn][maxn];
int match[maxm];
bool vis[maxm];
int n,m,iindex;
int dirx[]={0,1,0,-1};
int diry[]={1,0,-1,0};
struct Node
{
	int to;
	int next;
}edge[maxm];
int cnt;
int head[maxm];
void init()
{
    memset(map,0,sizeof(map));
    memset(head,-1,sizeof(head));
    cnt=0;
    return;
}
void add(int u,int v)
{
	edge[cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	return;
}
bool dfs(int node)
{
    for(int i=head[node];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!vis[v])
		{
			vis[v]=true;
			if(match[v]==-1||dfs(match[v]))
			{
				match[v]=node;
				return true;
			}
		}
	}
	return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int test;
    scanf("%d",&test);
    for(int cas=1;cas<=test;cas++)
    {
        init();
        scanf("%d%d",&n,&m);
        getchar();
        iindex=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            for(int j=0;j<m;j++)
			{
				if(str[i][j]=='*')
				{
					map[i][j]=++iindex;
				}
			}
        }
        for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(map[i][j])
				{
					for(int k=0;k<4;k++)
					{
						int nx=i+dirx[k];
						int ny=j+diry[k];
						if(nx>=0&&ny<m&&nx<n&&ny>=0)
						{
							if(map[nx][ny])
							{
								add(map[i][j],map[nx][ny]);
							}
						}
					}
				}
			}
		}
        int ans=0;
        memset(match,-1,sizeof(match));
        for(int i=1;i<=iindex;i++)
        {
            memset(vis,false,sizeof(vis));
            if(dfs(i))
            {
                ans++;
            }
        }
        printf("Case %d: %d\n",cas,iindex-ans+ans/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/82106662