HDU 4185 Oil Skimming(二分图最大匹配)

本文探讨了一种解决油田采油最优方案的方法,通过构建二分图并求解最大匹配问题,实现计算最大数量的采油区域覆盖。通过对给定地图的解析,运用算法逻辑有效地确定了最优采油策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:有个N*N的字符矩阵,你必须用竖直或水平的1*2小矩阵去覆盖字符矩阵中相邻的两个”#”字符. 且你用的1*2小矩阵不能重叠且只能覆盖”#”字符. 问你最多能用多少个1*2的小矩阵?

思路把原字符矩阵的所有”#”都编号看成一个一个的节点.如果有两个”#”相邻,那么就在它们之间连接一条边. 那么我们就得到了一个二分图 要求这个二分图的最大匹配边数,找出尽量多的匹配边. 那么这些匹配边就是一个个不重叠覆盖了”#”字符的1*2矩阵.


#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=1000+5;

struct Max_Match
{
    int n;
    vector<int> g[maxn];
    bool vis[maxn];
    int left[maxn];

    void init(int n)
    {
        this->n=n;
        for(int i=1; i<=n; ++i) g[i].clear();
        memset(left,-1,sizeof(left));
    }

    bool match(int u)
    {
        for(int i=0;i<g[u].size();++i)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                vis[v]=true;
                if(left[v]==-1 || match(left[v]))
                {
                    left[v]=u;
                    return true;
                }
            }
        }
        return false;
    }

    int solve()
    {
        int ans=0;
        for(int i=1; i<=n; ++i)
        {
            memset(vis,0,sizeof(vis));
            if(match(i)) ++ans;
        }
        return ans;
    }
}MM;
int cas=1;
int mapp[maxn][maxn];
int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int num = 0;
		memset(mapp,0,sizeof(mapp)); 
		for (int i = 1;i<=n;i++)
			for (int j = 1;j<=n;j++)
			{
				char temp;
				scanf(" %c",&temp);
				if (temp == '#')
					mapp[i][j]=++num;
				else
					mapp[i][j]=0;
			}
		MM.init(num);
		for (int i = 1;i<=n;i++)
			for (int j = 1;j<=n;j++)
				if (mapp[i][j]>0)
				{
					if (mapp[i+1][j]>0)
						MM.g[mapp[i][j]].push_back(mapp[i+1][j]);
					if (mapp[i-1][j] > 0)
						MM.g[mapp[i][j]].push_back(mapp[i-1][j]);
					if (mapp[i][j+1]>0)
						MM.g[mapp[i][j]].push_back(mapp[i][j+1]);
					if (mapp[i][j-1]>0)
						MM.g[mapp[i][j]].push_back(mapp[i][j-1]);
				}

        printf("Case %d: %d\n",cas++,MM.solve()/2);
    }
    return 0;
}

Description

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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值