HDU 4185 Oil Skimming(最大二分匹配)

题目链接

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的矩形去覆盖只有“#”的点,问最多能用多少个矩形。

题解:首先这个题,我们能够想到要用匈牙利算法算法,这是一个平面,我们用匈牙利算法算法很难用两个数组,所以我们要将图中每个“#”离散化保存。然后我们保存每个“#”点所能匹配的坐标,最后二分匹配就好了,细节看代码。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=6e2+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define mid (l+r)/2
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
typedef long long ll;
using namespace std;
char maps[maxn][maxn];
bool vis[maxn];
int head[maxn*maxn],len,n;
int maps1[maxn][maxn];
int d[maxn*maxn];
struct node
{
    int v,next;
} a[maxn*maxn];
void init()
{
    me(head,-1);
    len=0;
}
void add_edge(int u,int v)
{
    a[len].v=v;
    a[len].next=head[u];
    head[u]=len++;
}
int check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=n||maps[x][y]=='.')
        return 0;
    return 1;
}
void Creat()///将能够构成1*2的全部存起来
{
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
            if(maps[i][j]=='.')
                continue ;
            int u=maps1[i][j];
            for(int k=0; k<4; k++)
            {
                int i1=i+dir[k][0];
                int j1=j+dir[k][1];
                if(check(i1,j1))
                {
                    int v=maps1[i1][j1];
                    add_edge(u,v),add_edge(v,u);
                }
            }
        }
}
int found(int u)
{
    for(int i=head[u]; i!=-1; i=a[i].next)
    {
        int v=a[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            if(d[v]==-1||found(d[v]))
            {
                d[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungry(int m)
{
    int sum=0;
    me(d,-1);
    for(int i=0; i<m; i++)
    {
        me(vis,0);
        sum+=found(i);
    }
    return sum/2;///每条边加了两次,结果要除以2.
}
int main()
{
    int t,Case=1;
    scanf("%d",&t);
    while(t--)
    {
        int temp=0;///将是“#”的点离散化,用一位数表示二维坐标。
        scanf("%d",&n);
        init();
        for(int i=0; i<n; i++)
        {
            scanf("%s",maps[i]);
            for(int j=0; j<n; j++)
                if(maps[i][j]=='#')
                    maps1[i][j]=temp++;
        }
        Creat();
        printf("Case %d: %d\n",Case++,hungry(temp));
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值