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

来源:互联网 发布:es5 sham.js 编辑:程序博客网 时间:2024/05/01 22:23

题意:有个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;elsemapp[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

16.......##....##.......#.....##......
 

Sample Output

Case 1: 3
 


0 0