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

思路:先给每个’#‘定义值,然后创建连接,能够构成20*10方格的全部建立邻接表。(这题的看起来大,后台应该是很水的


#include<algorithm>
#include<string.h>
#include<stdio.h>
#define M 610
#define FOR(i,n) for(int i=0;i<n;i++)
using namespace std;
char str[M][M];
int head[M*M],carry,vis[M*M],dis[M*M],n,id[M][M],cut;
int f[4][2]= {1,0,0,1,-1,0,0,-1};
struct node
{
    int r,nextt;
} a[M*M*4];
void add(int x,int y)
{
    a[carry].r=y;
    a[carry].nextt=head[x];
    head[x]=carry++;
}
void init()
{
    carry=1,cut=1;
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    FOR(i,n)
    {
        scanf("%s",str[i]);
        FOR(j,n)
        {
            if(str[i][j]=='#')
                id[i][j]=cut++;
        }
    }
    FOR(i,n) FOR(j,n)
    {
        if(str[i][j]=='#')
        {
            for(int k=0; k<4; k++)
            {

                int x=i+f[k][0];
                int y=j+f[k][1];
                if(x>=0&&x<n&&y>=0&&y<n&&str[x][y]=='#')
                {
                    add(id[i][j],id[x][y]);
                }
            }
        }
    }
    return ;
}
int found(int u)
{
    int v=head[u];
    while(v!=-1)
    {
        int r=a[v].r;
        if(!vis[r])
        {
            vis[r]=1;
            if(!dis[r]||found(dis[r]))
            {
                dis[r]=u;
                dis[u]=r;
                return true;
            }
        }
        v=a[v].nextt;
    }
    return false;

}
int xyl()
{
    memset(dis,0,sizeof(int)*cut);
    int ans=0;
    FOR(i,cut)
    {
        if(!dis[i])
        {
            memset(vis,0,sizeof(int)*cut);
            if(found(i))
            {
                ans++;
            }
        }
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    FOR(ph,t)
    {
        init();
        int ans=xyl();
        printf("Case %d: %d\n",ph+1,ans);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值