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

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 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 
#include 
#include 
#include 
#define MAXN 500
using namespace std;
int n, m;
char str[25][25];
vector 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;
}


你可能感兴趣的:(最大匹配(权值匹配),独立集,团,&&,最小路径,点覆盖)