zoukankan      html  css  js  c++  java
  • 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
    ", ++times, sum/2);
        }
    
        return 0;
    }
    
  • 相关阅读:
    为初次使用linux设置 root密码
    linux如何改为汉化环境
    Linux 标准目录结构
    常用linux terminal 命令
    jquery 获取及设置input各种类型的值
    使用$.getJSON实现跨域ajax请求
    react 异步取数据
    PHP 全局变量
    PHP保存本地日志文件
    全端开发——css(选择器)
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11163979.html
Copyright © 2011-2022 走看看