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;
    }
  • 相关阅读:
    Android之上下文context
    如果简单的记录,就可以为这个世界创造更多的财富,那么还有什么理由不去写博客呢? — 读<<黑客与画家>> 有感
    【最新最全】为 iOS 和 Android 的真机和模拟器编译 Luajit 库
    【Graphql实践】使用 Apollo(iOS) 访问 Github 的 Graphql API
    【最新】LuaJIT 32/64 位字节码,从编译到使用全纪录
    简陋的swift carthage copy-frameworks 辅助脚本
    【自问自答】关于 Swift 的几个疑问
    【读书笔记】The Swift Programming Language (Swift 4.0.3)
    【读书笔记】A Swift Tour
    【趣味连载】攻城狮上传视频与普通人上传视频:(一)生成结构化数据
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5643957.html
Copyright © 2011-2022 走看看