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.

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

这道题是要求能在已知的图中得到多少10x20的格子,这道题我们可以这样考虑,将每一个位#的格子编号,如果他的上下左右也是#的话,那就把他们当做一条边。接下来就是匈牙利算法了。

#include<cstdio>
#include<cstring>
using namespace std;
#define mmm(a,b) memset(a,b,sizeof(a))
const int N=605;
char s[N][N];
int e[N][N];
int g[N][N];
int n;
int temp;
int book[N];
int match[N];
int Next[4][2]= {1,0,-1,0,0,1,0,-1};
int dfs(int u)
{
    for(int i=1; i<=temp; i++)
    {
        if(g[u][i]&&!book[i])
        {
            book[i]=1;
            if(!match[i]||dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int tt;
    int cas=1;
    scanf("%d",&tt);
    while(tt--)
    {
        temp=0;
        mmm(e,0);
        mmm(g,0);
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%s",s[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                if(s[i][j]=='#')
                    e[i][j]=++temp;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(s[i][j]=='#')
                {
                    for(int k=0; k<4; k++)
                    {
                        int x=Next[k][0]+i;
                        int y=Next[k][1]+j;
                        if(x>=0&&x<n&&y<n&&y>=0&&s[x][y]=='#')
                            g[e[i][j]][e[x][y]]=1;
                    }
                }
            }
        }
        mmm(match,0);
        int ans=0;
        for(int i=1; i<=temp; i++)
        {
            mmm(book,0);
            if(dfs(i)) ans++;
        }
        printf("Case %d: %d\n",cas++,ans/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40510246/article/details/81364907