HDU4185 Oil Skimming

Oil Skimming

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


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
 
题意:这是个油田,有水有油,捞石油的机器只能捞1*2的矩阵,问你最多放多少捞油机
首先要想怎么构图,因为捞油机每次覆盖两个跟二分图匹配是一样的,求的就是最大匹配。构图是个难点。匹配的条件是每次有两个‘#’连在一起,那么我们两个图里边都是所有的‘#’的个数,只要有两个相邻的‘#’那么就连线。我们把每个‘#’都编号,然后看谁是连在一起的,是的话就连线。
这样一来图就变成了
. . . . . .
.12 . . .
.34 . . .
. . . . 5.
. . . . 67
. . . . . .
这样图就构好了,接下来就是匹配了。但有一点需要注意,1和2相连,2和1也是相连的,那么求出来的最大匹配结果就要除以2。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,ans;
char mapp[605][605];
int a[605][605],used[605],pp[605];
int mappp[605][605];
int found(int x)
{
    for(int i=1;i<ans;i++)
    {
        if(mappp[x][i]&&!used[i])
        {
            used[i]=1;
            if(!pp[i]||found(pp[i]))
            {
                pp[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int aa()
{
    int sum=0;
    memset(pp,0,sizeof(pp));
    for(int i=1;i<ans;i++)
    {
        memset(used,0,sizeof(used));
        if(found(i))sum++;
    }
    return sum;
}
int main()
{
    int t,Case=0;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(mappp,0,sizeof(mappp));
        scanf("%d",&n);
        ans=1;
        for(int i=0; i<n; i++)
        {
            scanf("%s",mapp[i]);
            for(int j=0; j<n; j++)
            {
                if(mapp[i][j]=='#')
                    a[i][j]=ans++;//给‘#’编号
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(mapp[i][j]=='#')//查找一个'#'的四周有没有'#’,有的话就连线
                {
                    if(i-1>=0&&mapp[i-1][j]=='#')
                        mappp[a[i-1][j]][a[i][j]]=1;
                    if(j+1<n&&mapp[i][j+1]=='#')
                        mappp[a[i][j+1]][a[i][j]]=1;
                    if(i+1<n&&mapp[i+1][j]=='#')
                        mappp[a[i+1][j]][a[i][j]]=1;
                    if(j-1>=0&&mapp[i][j-1]=='#')
                        mappp[a[i][j-1]][a[i][j]]=1;
                }
            }
        }
        printf("Case %d: %d\n",++Case,aa()/2);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值