HDU4185 【二分图最大匹配】

来源:互联网 发布:第三方数据平台 编辑:程序博客网 时间:2024/06/14 00:40
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

和POJ3020类似

题意:给一张图,问最大的连续两个格子数目有多少。

建图:用横纵坐标和为偶数为x边,对该点想上下左右寻找(上下左右点必为奇数为y边),建二分图。用匈牙利算法就行啦。


#include<cstring>#include<cstdio>#include<iostream>#include<algorithm>#include<vector>#include<queue>using namespace std;const int INF=0x3f3f3f3f;char mp[666][666];vector<int> g[1999];int n,m,mm,my[1999],ct[666][666];int dx[]={0,0,-1,1},dy[]={1,-1,0,0};bool vis[1999];int dfs(int x){    for(int i=0;i<g[x].size();i++)    {        int y=g[x][i];        if(vis[y])continue;        vis[y]=true;        if(my[y]==-1||dfs(my[y]))        {            my[y]=x;            return 1;        }    }    return 0;}int maxmacth(){    int ret=0,x,y;    memset(my,-1,sizeof(my));    for(int i=1;i<=m;i++)    {        memset(vis,false,sizeof(vis));        if(dfs(i))ret++;    }    return ret;}int main(){    int tat,x,y,cas=1;    scanf("%d",&tat);    while(tat--)    {        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf(" %s",mp[i]+1);        }        m=mm=0;         for(int i=1;i<=n;i++)         {             for(int j=1;j<=n;j++)             {                 if((i+j)%2)                    ct[i][j]=++m;                else                    ct[i][j]=++mm;             }         }         for(int i=1;i<=m;i++)         {             g[i].clear();         }         for(int i=1;i<=n;i++)         {             for(int j=1;j<=n;j++)             {                  if((i+j)%2)continue;                 if(mp[i][j]=='#')                 for(int k=0;k<4;k++)                 {                     x=i+dx[k];                     y=j+dy[k];                     if(x<1||y<1||x>n||y>n)continue;                     if(mp[x][y]!='#')continue;                     g[ct[i][j]].push_back(ct[x][y]);                 }             }         }       /*  for(int i=1;i<=m;i++)         {             for(int j=0;j<g[i].size();j++)                cout<<i<<"  "<<g[i][j]<<endl;         }         */         printf("Case %d: %d\n",cas++,maxmacth());    }    return 0;}


原创粉丝点击