HDU-4185-Oil Skimming(最大匹配)

链接:

https://vjudge.net/problem/HDU-4185

题意:

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.

思路:

给每个#号标记编号,在对每个#号周围选择相邻的配对,最大匹配。
答案除2.

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN = 1e3+10;
const int INF = 1<<30;
int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

char Map[MAXN][MAXN];
int Dis[MAXN][MAXN];
vector<int> G[MAXN*MAXN];
int Linked[MAXN], Vis[MAXN];
int n, cnt;

bool Dfs(int x)
{
    for (int i = 0;i < G[x].size();i++)
    {
        int node = G[x][i];
        if (Vis[node])
            continue;
        Vis[node] = 1;
        if (Linked[node] == -1 || Dfs(Linked[node]))
        {
            Linked[node] = x;
            return true;
        }
    }
    return false;
}

int Solve()
{
    memset(Linked, -1, sizeof(Linked));
    int sum = 0;
    for (int i = 1;i <= cnt;i++)
    {
        memset(Vis, 0, sizeof(Vis));
        if (Dfs(i))
            sum++;
    }
    return sum;
}

int main()
{
    int t, times = 0;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
            scanf("%s", Map[i]+1);
        cnt = 0;
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= n;j++)
                if (Map[i][j] == '#')
                    Dis[i][j] = ++cnt;
        }
        for (int i = 1;i <= cnt;i++)
            G[i].clear();
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= n;j++)
                if (Map[i][j] == '#')
                {
                    for (int k = 0;k < 4;k++)
                    {
                        int tx = i+Next[k][0];
                        int ty = j+Next[k][1];
                        if (tx < 1 || tx > n || ty < 1 || ty > n)
                            continue;
                        if (Map[tx][ty] == '#')
                            G[Dis[i][j]].push_back(Dis[tx][ty]);
                    }
                }
        }
        int sum = Solve();
        printf("Case %d: %d\n", ++times, sum/2);
    }

    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/11163979.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值