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;
    }
  • 相关阅读:
    (转载)学校搭建使用nginx同时编译rtmp-module进行直播的技术文档
    Python3 字典无has_key()方法,调用报AttributeError: 'dict' object has no attribute 'has_key'错误
    用CSS样式画横线和竖线的方法
    wireshark 包分析命令
    设置windows密码只存在NTLM-Hash下
    修改默认3389远程连接-注册表
    ipc$爆破密码
    windows server 2008/2012 无法安装AD域解决方法记录
    Android UiAutomator 自动化测试一些代码实例---新手3
    linux 添加防火墙开放端口
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5643957.html
Copyright © 2011-2022 走看看