zoukankan      html  css  js  c++  java
  • 【二分图匹配入门专题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;
    }
  • 相关阅读:
    Docker 网络
    Dockerfile你值得拥有
    Spring boot配置成功后,出现Whitelabel Error Page的解决办法
    ubuntu 创建快捷方式
    Ubuntu Make来安装Visual Studio Code
    ubuntu16.04开机自动挂载Windows分区
    ubuntu下使用罗技Unifying
    酷开电视升级后无法启动
    ArcGIS 10.2安装错误,Error: Internal Error 2753. The File is not marked for installation
    ubuntu16.04下使用wine 运行微信开发小程序
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7353282.html
Copyright © 2011-2022 走看看