• HDU 4185 Oil Skimming (二分匹配)


    Oil Skimming

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

     Description
    Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.
     
    Input
    The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.
     
    Output
    For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.
     
    Sample Input
    1
    6
    ......
    .##...
    .##...
    ....#.
    ....##
    ......
    Sample Output
    Case 1: 3
     
    题意:有一个油田,有些地方有油,用‘#’表示;有些是水,用‘.’表示,每个格子大小为10m,每个铲子是10*20的,然后铲子不能碰到水,问最多能放几个铲子。
    分析:将所有的有油的格子编号,如果它的周围有其它有油的格子,就连一条边,然后求最大匹配就行了。注意是无向图的匹配,要除以2.
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    
    const int MAXN = 700;
    int uN,vN;
    int G[MAXN][MAXN];
    int linker[MAXN];
    bool used[MAXN];
    
    bool dfs(int u)
    {
        for(int v=1;v<=vN;v++){
            if(G[u][v]&&!used[v]){
                used[v]=true;
                if(linker[v]==-1||dfs(linker[v])){
                    linker[v]=u;
                    return true;
                }
            }
        }
        return false;
    }
    
    int Hungary()
    {
        int res=0;
        memset(linker,-1,sizeof(linker));
        for(int u=1;u<=uN;u++){
            memset(used,false,sizeof(used));
            if(dfs(u)) res++;
        }
        return res;
    }
    
    char mp[MAXN][MAXN];
    int m[MAXN][MAXN];
    
    int main()
    {
        int K,n;
        int iCase=0;
        scanf("%d",&K);
        while(K--){
           int cnt=0;
           iCase++;
           scanf("%d",&n);
           for(int i=0;i<n;i++){
              scanf("%s",mp[i]);
              for(int j=0;j<n;j++)
                if(mp[i][j]=='#') m[i][j]=++cnt;
           }
           memset(G,0,sizeof(G));
           uN=vN=cnt;
           for(int i=0;i<n;i++){
               for(int j=0;j<n;j++){
                    if(mp[i][j]!='#') continue;
                    if(i!=n-1&&mp[i+1][j]=='#') G[m[i][j]][m[i+1][j]]=1;
                    if(i!=0&&mp[i-1][j]=='#') G[m[i][j]][m[i-1][j]]=1;
                    if(j!=0&&mp[i][j-1]=='#') G[m[i][j]][m[i][j-1]]=1;
                    if(j!=n-1&&mp[i][j+1]=='#') G[m[i][j]][m[i][j+1]]=1;
               }
           }
           printf("Case %d: %d
    ",iCase,Hungary()/2);
        }
        return 0;
    }
  • 相关阅读:
    2.1.1 Speed Limit
    2.1.2 骑自行车的最短时间
    1.3.1提高实数精度的范例
    1.2.2一个数可以有多少种用连续素数之和表示
    求二倍关系的个数 1.2.1
    求平均值
    原生JS 购物车及购物页面的cookie使用
    基于Jquery的商城商品图片的放大镜效果(非组件)
    商城商品购买数量增减的完美JS效果
    弹性布局各种坑爹兼容
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5643957.html
Copyright © 2020-2023  润新知