zoukankan      html  css  js  c++  java
  • hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)

    Problem 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
    Source

    题意:求能够找到最多多少个“##”(横或竖都行)

    解题:关键是建图,在建图时,先用tmp[][]数组将‘#’的数字存起来(tmp=0开始),然后再遍历一遍,如果遇到的是mp[i][j]=='#',则将上下左右是‘#’的标记为cnt[ tmp[][] ][tmp[][] ]=1,最后就是经典的匈牙利算法了。

           犯了个致命的错误,在匈牙利算法时所对应的n应该是num,即重新建图后的‘n’。

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 606
    23 #define inf 1e12
    24 int n;
    25 char mp[N][N];
    26 int tmp[N][N];
    27 int cnt[N][N];
    28 int match[N];
    29 int vis[N];
    30 int tot;
    31 bool dfs(int u){
    32    for(int i=0;i<tot;i++){
    33       if(!vis[i] && cnt[u][i]){
    34          vis[i]=1;
    35          if(match[i]==-1 || dfs(match[i])){
    36             match[i]=u;
    37             return true;
    38          }
    39       }
    40    }
    41    return false;
    42 }
    43 
    44 int solve(){
    45    int ans=0;
    46    memset(match,-1,sizeof(match));
    47    for(int i=0;i<tot;i++){
    48       memset(vis,0,sizeof(vis));
    49       if(dfs(i)){
    50          ans++;
    51       }
    52    }
    53 
    54    return ans;
    55 }
    56 int main()
    57 {
    58    int t,ac=0;;
    59    scanf("%d",&t);
    60    while(t--){
    61       memset(tmp,0,sizeof(tmp));
    62       scanf("%d",&n);
    63       int num=0;
    64       for(int i=0;i<n;i++){
    65          scanf("%s",mp[i]);
    66          for(int j=0;j<n;j++){
    67             if(mp[i][j]=='#'){
    68                tmp[i][j]=num++;
    69             }
    70          }
    71       }
    72       tot=num;
    73       memset(cnt,0,sizeof(cnt));
    74       for(int i=0;i<n;i++){
    75          for(int j=0;j<n;j++){
    76             if(mp[i][j]!='#') continue;
    77             if(i>0 && mp[i-1][j]=='#') cnt[tmp[i][j]][tmp[i-1][j]]=1;
    78             if(i<n-1 && mp[i+1][j]=='#') cnt[tmp[i][j]][tmp[i+1][j]]=1;
    79             if(j>0 && mp[i][j-1]=='#') cnt[tmp[i][j]][tmp[i][j-1]]=1;
    80             if(j<n-1 && mp[i][j+1]=='#') cnt[tmp[i][j]][tmp[i][j+1]]=1;
    81          }
    82       }
    83       printf("Case %d: ",++ac);
    84       int ans=solve();
    85       printf("%d
    ",ans/2);
    86    }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    C++ 构造函数初始化列表
    虚函数
    thread 学习
    vim学习笔记
    Python重载比较运算符
    python使用插入法实现链表反转
    【好文转】Python中yield的理解与使用
    【转】Python中自定义可迭代对象
    linux安装python3.6.6和新建虚拟环境
    【转】Python 并行分布式框架 Celery
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4995593.html
Copyright © 2011-2022 走看看