• 【二分图匹配入门专题1】I


    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 mrepresents the number of rows, and n represents the number of columns. Then there will be m lines, each containing ncharacters 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才是实际的最大匹配值(因为重复),用黄金总数减去最大匹配值,就是我们需要的多米诺骨牌数。

    这次专题真的太锻炼自己的耐性了,前面8道水题写的我头疼,感觉好无聊啊~~还要因为题意理解不到位出错,这才是最耻辱的好吗。不过好在我从这道题身上看到了希望,之后的9道题应该很有特点。

    #include<stdio.h>
    #include<string.h>
    #define N 450
    char map[25][25];
    int e[N][N],flag[25][25];
    int book[N],match[N],next[4][2]={0,1,1,0,-1,0,0,-1};
    int n,m,count;
    
    void dfs()
    {
        int i,j,x,y,k;
        int tx,ty,start,end;
        for(i = 1; i <= n; i ++)
            for(j = 1; j <= m; j ++)
            {
                if(map[i][j]=='*')
                {
                    start = flag[i][j];
                    for(k = 0; k < 4; k ++)
                    {
                        tx = i + next[k][0];
                        ty = j + next[k][1];
                        if(tx < 1||ty < 1||tx > n||ty > m)
                            continue;
                        if(map[tx][ty] == '*')
                        {
                            end = flag[tx][ty];
                            e[start][end] = 1;//连接两个相邻的黄金编号 
                        }
                    }
                }
            }
        return ;
    }
    
    int find(int u) 
    {
        int i;
        for(i = 1; i <= count; i ++)
        {
            if(!book[i]&&e[u][i])
            {
                book[i] = 1;
                if(!match[i]||find(match[i]))
                {
                    match[i] = u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main()
    {
        int t,i,j,sum;
        scanf("%d",&t);
        int t2 = 0;
        while(t --)
        {
            t2 ++;
            memset(e,0,sizeof(e));
            memset(flag,0,sizeof(flag));
            scanf("%d%d",&n,&m);
            for(i = 1; i <= n; i ++)
                scanf("%s",map[i]+1);
                
            count = 0;
            for(i = 1; i <= n; i ++)
                for(j = 1; j <= m; j ++)
                    if(map[i][j]== '*')
                    {
                        flag[i][j] = ++count;//对黄金进行编号 
                        
                    }
            dfs();//寻找四周相邻的黄金        
            memset(match,0,sizeof(match));
            sum = 0;
            for(i = 1; i <= count; i ++)//匈牙利算法 
            {
                memset(book,0,sizeof(book));
                if(find(i))
                    sum ++;
            }
            printf("Case %d: %d
    ",t2,count-sum/2);
        }
        return 0;
    }
  • 相关阅读:
    容器环境的JVM内存设置最佳实践
    K8S基于ingress-nginx实现灰度发布
    基于ambassador实现K8S灰度发布
    Nginx配置中一个不起眼字符"/"的巨大作用,失之毫厘谬以千里
    springboot之jackson的两种配置方式
    CentOS7清理磁盘空间
    主机磁盘满了,却查不到大文件占用
    【数据库-MySql】开启事件 event_scheduler
    Jenkins自动化构建vue项目然后发布到远程服务器
    CentOS 7 使用NVM管理nodejs
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7353282.html
Copyright © 2020-2023  润新知