Hdu 4185 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

思路:

因为只要两块田相邻就符合条件,很容易想到如果两块油田相邻就建立边,给每个油田编号,建边之后利用匈牙利算法求最大匹配数ans,然后ans/2就是答案。

本来想这题用匈牙利会超时,结果竟然过了。

代码如下:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=6*1e2+5;
int a[maxn][maxn];
int id[maxn][maxn];
int head[maxn*maxn];
int k,n;
int cnt,num;
int pos[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct edge
{
    int to;
    int next;
}edge[maxn*maxn*2];
int vis[maxn*maxn];
int match[maxn*maxn];
void add (int id,int u,int v)
{
    edge[id].to=v;
    edge[id].next=head[u];
    head[u]=id;
}
bool Find(int x)
{
    for (int i=head[x];i!=-1;i=edge[i].next)
    {
        int u=edge[i].to;
        if(!vis[u])
        {
            vis[u]=1;
            if(match[u]==-1||Find(match[u]))
            {
                match[u]=x;
                return true;
            }
        }
    }
    return false;
}
int algor ()
{
    memset (match,-1,sizeof(match));
    int ans=0;
    for (int i=1;i<=cnt;i++)
    {
        memset (vis,0,sizeof(vis));
        if(Find(i)) ans++;
    }
    return ans;
}
int main()
{
    scanf("%d",&k);
    for (int l=1;l<=k;l++)
    {
        memset (head,-1,sizeof(head));
       scanf("%d",&n);
       getchar();
       cnt=0,num=0;
       for (int i=0;i<n;i++)
       {
           for (int j=0;j<n;j++)
           {
               scanf("%c",&a[i][j]);
               if(a[i][j]=='#') id[i][j]=++cnt;
           }
           getchar();
       }
       for (int i=0;i<n;i++)
       {
           for (int j=0;j<n;j++)
           {
               if(a[i][j]=='#')
               {
                   for (int m=0;m<4;m++)
                   {
                       int tx=i+pos[m][0];
                       int ty=j+pos[m][1];
                       if(tx>=0&&tx<n&&ty>=0&&ty<n&&a[tx][ty]=='#')
                       {
                           add(++num,id[i][j],id[tx][ty]);
                           add(++num,id[tx][ty],id[i][j]);
                       }
                   }
               }
           }
       }
       printf("Case %d: %d\n",l,algor()/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/88356524
今日推荐