1. 程式人生 > >Oil Skimming (二分圖最大匹配)

Oil Skimming (二分圖最大匹配)

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.InputThe 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.OutputFor 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

題目大概:

在一個n*n的矩形中有很多油,一次可以取1*2矩形內的油(且矩形內必須都是油),問最多取幾次。

思路:

這個雖然也是矩形問題,矩形中也有東西需要操作,但是卻不是用之前的行列為點,焦點為邊的建模法。而是,把所有有油的地方標記下來並且重新編號,然後掃一遍矩形找和它相鄰的點有油的地方連邊,最後跑一遍匈牙利演算法,求最大匹配,因為連的是雙向邊,a掃到b要取,b掃到a也要取,所以答案要/2.。

程式碼:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 660
using namespace std;
char map[MAXN][MAXN];
int ma[MAXN][MAXN];
int G[MAXN][MAXN];
int pipei[MAXN];
bool used[MAXN];
int N,T,M;
void init()
{
    memset(G,0,sizeof(G));

}

void so(int i,int j)
{
    if(ma[i][j+1]&&j+1<N)
    {
        G[ma[i][j]][ma[i][j+1]]=G[ma[i][j+1]][ma[i][j]]=1;
        //printf("%d %d\n",ma[i][j],ma[i][j+1]);
    }
    if(ma[i+1][j]&&i+1<N)
    {
        G[ma[i][j]][ma[i+1][j]]=G[ma[i+1][j]][ma[i][j]]=1;
        //printf("%d %d\n",ma[i][j],ma[i+1][j]);
    }
}
void getMap()
{
    M=1;
    for(int i = 0; i < N; i++)
    {
        scanf("%s",map[i]);
        for(int j=0;j<N;j++)
        {
            if(map[i][j]=='#')
            {
                ma[i][j]=M++;
            }
            else
            {
                ma[i][j]=0;
            }
        }
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(ma[i][j])
            {
                //printf("%d \n",ma[i][j]);
                so(i,j);
            }
        }
    }
}
int find(int x)
{
    for(int i = 1; i <M; i++)
    {
        int y = G[x][i];
        if(y&&!used[i])
        {
            used[i] = true;
            if(pipei[i] == -1 || find(pipei[i]))
            {
                pipei[i] = x;
                return 1;
            }
        }
    }
    return 0;
}
int te=0;
void solve()
{
    /*printf("%d\n",M);
    for(int i=1;i<M;i++)
    {
        for(int j=1;j<M;j++)
        {
            printf("%d ",G[i][j]);
        }
        printf("\n");
    }*/
    int ans = 0;
    memset(pipei, -1, sizeof(pipei));
    for(int i = 1; i <M; i++)
    {
        memset(used, false, sizeof(used));
        ans += find(i);
    }
    printf("Case %d: %d\n",te,ans/2);
}

int main()
{

    scanf("%d",&T);
    while(T--)
    {
        scanf("%d", &N);
        te++;
        init();
        getMap();
        solve();
    }
    return 0;
}