HDU 4185 Oil Skimming 二分匹配

传送门:Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2828    Accepted Submission(s): 1165


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

题意:有一个10*20的大勺子,现在#代表油田,' . '代表水,油田的为10*10的,现在要求你能舀出几勺不带水的油。

思路:用二分匹配求出最大匹配对数,先对油田进行标号,然后记录他们的关系,这道题因为每个油田最多有4个关系(上下左右),所以不用邻接矩阵,本来g[ i ][ j ]=1;代表i和j有关系,现在用g[ i ][ k ]=j;代表和i有关系的第k个数是j

这样就能减少复杂度

#include<stdio.h>
#include <iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<map>
#define LL long long
#define N 605
using namespace std;
char a[N][N];
int c[N][N];
int b[N*N][4];
int line[N*N];
int vis[N*N];
int find(int v)
{
    for(int i=0;i<4;i++)
    if(b[v][i]&&!vis[b[v][i]])
    {
        int x=b[v][i];
        vis[x]=1;
        if(line[x]==0||find(line[x]))
        {
            line[x]=v;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    int cas=1;
    memset(c,0,sizeof(c));
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int tot=1;
        for(int i=1; i<=n; i++)
        {
            scanf("%s",a[i]+1);
            for(int j=1; j<=n; j++)
                if(a[i][j]=='#')
                    c[i][j]=tot++;
                else
                    c[i][j]=0;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(c[i][j])
                {
                    int k=c[i][j];
                    b[k][0]=c[i-1][j];
                    b[k][1]=c[i][j-1];
                    b[k][2]=c[i+1][j];
                    b[k][3]=c[i][j+1];
                }
            }
        }
        int ans=0;
        for(int i=0;i<tot;i++)
                line[i]=0;
        for(int i=1;i<tot;i++)
        {
            for(int j=0;j<tot;j++)//用memset函数,时间会很长
                vis[j]=0;
            if(find(i)) ans++;
        }
        printf("Case %d: %d\n",cas++,ans/2);
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值