HDU 4185 Oil Skimming (最大匹配)

本文介绍了一种使用特殊飞机从水面收集石油的方法,并提供了一个解决该问题的算法实现。该问题涉及在一个N*N的网格中寻找最大的可以完全覆盖的10m*20m矩形区域数量,每个矩形必须完全位于标记为油的单元格上。

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



Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2522    Accepted Submission(s): 1041


Problem 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
 

Source


题意:

一块区域n*n,'#'表示油,'.'表示水,每次能挖1*2的矩形(横竖任意),且不能挖到水,而且不能交叉挖,问最多可以挖几下。


思路:

经典二分图模型,这里的突破点是, 不能交叉挖到。。一个矩形只挖两块, 也就是说,某一块挖了,只能在他四个方向选一个挖, 这一块不能再被挖, 通过这构造二分图了~枚举每一个#,跟他周围四个是#的连边, 这样就构造了一个无向图,然后匈牙利最大匹配,因为是无向的,所以/2;


以后在矩形里的问题,而且有明显互斥性,都可以想想二分图~,或者说凡是有互斥性的题目,都可以想一下~


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 605;
char maze[maxn][maxn];
vector<int> v[maxn];
int book[maxn], match[maxn], id[maxn][maxn], dir[4][2] = {0,1,0,-1,1,0,-1,0};
int Find(int x)
{
    for(int i = 0; i < v[x].size(); i++)
    {
        int to = v[x][i];
        if(book[to]) continue;
        book[to] = 1;
        if(!match[to] || Find(match[to]))
        {
            match[to] = x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int t, n, ca = 1;
    cin >> t;
    while(t--)
    {
        memset(match, 0, sizeof(match));
        for(int i = 0; i < maxn; i++)
            v[i].clear();
        int cnt = 1;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                scanf(" %c", &maze[i][j]);
                if(maze[i][j] == '#') id[i][j] = cnt++;
            }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                if(maze[i][j] == '.') continue;
                for(int k = 0; k < 4; k++)
                {
                    int tx = i + dir[k][0];
                    int ty = j + dir[k][1];
                    if(tx < n && tx >= 0 && ty < n && ty >= 0 && maze[tx][ty] == '#')
                        v[id[i][j]].push_back(id[tx][ty]);
                }
            }
        int ans = 0;
        for(int i = 1; i <= cnt; i++)
        {
            memset(book, 0, sizeof(book));
            ans += Find(i);
        }
//        cout << ans << endl;
        printf("Case %d: %d\n", ca++, ans/2);
    }
    return 0;
}


Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2522    Accepted Submission(s): 1041


Problem 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
 

Source

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值