zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【网络文摘】一位36岁程序员的困惑
    【网络文摘】大龄程序员怎样渡过中年危机?
    【问题与解决】showModalDialog is not defined 的解决方案
    【ASP.NET 问题】ASP.NET 网站404页面返回200,或者302的解决办法
    IIS 网站 HTTP 转 HTTPS
    ECharts JS应用:图表页面实现
    【问题与解决】怎么删除TFS云端上的项目
    【问题与解决】Github 上传代码报错(error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version)
    【问题与解决】Mac OS通过 npm 安装 React Native 报错(checkPermissions Missing write access to /usr/local/lib/node_modules)
    【JavaScript 插件】图片展示插件 PhotoSwipe 初识
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5643957.html
Copyright © 2011-2022 走看看