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;
}
posted @ 2019-07-10 14:59  YDDDD  阅读(185)  评论(0编辑  收藏  举报