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;
    }
    
  • 相关阅读:
    stylus入门教程,在webstorm中配置stylus
    转载 IDEA/Pycharm使用总结
    Python中itertools.groupby分组的使用
    flex:1和flex:auto详解
    JAVA中的四种JSON解析方式详解
    idea中Entity实体中报错:cannot resolve column/table/...解决办法。
    springmvc之静态资源访问不到 -记一次惨痛的经历
    三款免费好用的Gif录屏神器
    设置ItelliJ IDEA里修改jsp不重启tomcat
    Java中List, Integer[], int[]的相互转换
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11163979.html
Copyright © 2011-2022 走看看