lightoj 1152 - Hiding Gold 【奇偶建二分图 求最大匹配】

1152 - Hiding Gold
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 nrepresents 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的地图上,有两种字符* o ,其中*代表金矿、o代表空。现在要求你用最少的 1*2 (或者 2*1)的材料盖住所有的金矿。


思路:按坐标和划分奇偶集,构造二分图。求出所有坐标和为奇数的金矿数目oddnum,求出坐标和为偶数的金矿数目evennum。在构建的二分图上求最大匹配ans,结果就是oddnum + evennum - ans。


AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define MAXN 500
using namespace std;
int n, m;
char str[25][25];
vector<int> G[MAXN];
bool used[MAXN];
int match[MAXN];
int oddnum, evennum;//奇即元素个数 偶集元素个数
int Map[25][25];
bool judge(int x, int y){
    return  x >= 0 && x < n && y >= 0 && y < m;
}
void getMap()
{
    scanf("%d%d", &n, &m);
    oddnum = evennum = 0;
    for(int i = 0; i < n; i++)
    {
        scanf("%s", str[i]);
        for(int j = 0; j < m; j++)
        {
            Map[i][j] = -1;
            if(str[i][j] == '*')
            {
                if((i+j) & 1)
                    Map[i][j] = ++oddnum;
                else
                    Map[i][j] = ++evennum;
            }
        }
    }
    for(int i = 1; i <= oddnum; i++)
        G[i].clear();
    int Move[4][2] = {0,1, 0,-1, 1,0, -1,0};
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(str[i][j] == '*' && ((i+j) & 1))
            {
                for(int k = 0; k < 4; k++)
                {
                    int next_x = i + Move[k][0];
                    int next_y = j + Move[k][1];
                    if(!judge(next_x, next_y)) continue;//越界
                    if(str[next_x][next_y] == '*')
                        G[Map[i][j]].push_back(Map[next_x][next_y]);
                }
            }
        }
    }
}
int DFS(int u)
{
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(!used[v])
        {
            used[v] = true;
            if(match[v] == -1 || DFS(match[v]))
            {
                match[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int k = 1;
void solve()
{
    int ans = 0;
    memset(match, -1, sizeof(match));
    for(int i = 1; i <= oddnum; i++)
    {
        memset(used, false, sizeof(used));
        ans += DFS(i);
    }
    printf("Case %d: %d\n", k++, evennum + oddnum - ans);
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        getMap();
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值