Oil Skimming——最大二分匹配

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

   
   
1 6 ...... .##... .##... ....#. ....## ......

Sample Output

   
   
Case 1: 3
这道题的大意是给你一个图,#代表油污,用2*1的板子将相邻的两个#覆盖,最多需要多少个板子,其中#不能被重复覆盖

思路:

这道题的将模型抽象化之后就是一个二分匹配,其实这道题的难点就是将模型抽象出来,也就是建一个二分图,建图方法自己看代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int ma[700][700],ss[700][700];
char f[700][700];
int n;
int a[700],b[700];
bool use[700];
int sum;
int fin(int id)
{
    int i;
    for(i=1;i<sum;i++)
    {
        if(use[i]==false&&ma[id][i]==1)
        {
            use[i]=true;
            if(fin(a[i])||a[i]==0)
            {
                a[i]=id;
                return 1;
            }
        }
    }


    return 0;


}


int main()
{
    int i,j;
    int k;
    int g;
    scanf("%d",&k);


        for(g=1;g<=k;g++)
        {
            scanf("%d",&n);
            for(i=0;i<n;i++)
                scanf("%s",f[i]);
            sum=1;


            memset(ss,0,sizeof(ss));
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(f[i][j]=='#')
                    {
                        ss[i][j]=sum++;  //给每个点一个标号

                    }
                }
            }


            memset(ma,0,sizeof(ma));

建图,将图建委无向图
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(f[i][j]=='#')
                    {
                        if(i<(n-1)&&f[i+1][j]=='#')  
                        {
                            ma[ss[i][j]][ss[i+1][j]]=1;
                            ma[ss[i+1][j]][ss[i][j]]=1;
                        }
                        if(j<(n-1)&&f[i][j+1]=='#')
                        {
                            ma[ss[i][j]][ss[i][j+1]]=1;
                            ma[ss[i][j+1]][ss[i][j]]=1;
                        }
                    }
                }
            }


            int cnt=0;
            memset(a,0,sizeof(a));
            for(i=1;i<sum;i++)
            {
                memset(use,false,sizeof(use));
                if(fin(i))
                    cnt++;
            }
            printf("Case %d: %d\n",g,cnt/2);   //因为为无向图,所以说要初二


    }
}

///

这是我在网上找到别人的用临街表做的这道题,主要是临街表在二分匹配上的应用这个我需要熟悉一下

//296MS 4576K 
#include<stdio.h>
#include<string.h>


#define MAX 601


struct Edge
{
int  to,next;
}edge[MAX*MAX];
int head[MAX*MAX],tol;
void add(int a,int b)
{
edge[tol].to = b;
edge[tol].next = head[a];
head[a] = tol ++;
}


char map[MAX][MAX];
int sum,QQ[MAX][MAX];
int vis[2][2]={{1,0},{0,1}};


int link[MAX*MAX],flag[MAX*MAX];


bool dfs(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!flag[v])
{
flag[v] = 1;
if(link[v] == -1 || dfs(link[v]))
{
link[v] = u;return true;
}
}
}
return false;
}


int match()
{
int ans = 0;
memset(link,-1,sizeof(link));
for(int u = 0; u < sum; u ++)
{
memset(flag,0,sizeof(flag));
if(dfs(u)) ans ++;
}
return ans/2;
}


int main()
{
int i,j;
int T,N;
scanf("%d",&T);
for(int cas = 1; cas <= T; cas ++)
{
tol = 0;
sum = 0;
memset(head,-1,sizeof(head));
scanf("%d",&N);
for(i = 0; i < N; i ++,getchar())
scanf("%s",map[i]);


//建图
for(i = 0; i < N; i++)
for(j = 0; j < N; j ++)
if(map[i][j] == '#')
QQ[i][j] = sum++;//map[i][j] = sum ++ +'0';这样不可以,如果超过9,10这个不是一个字符啦....只能借助一个数组
for(i = 0; i < N; i ++)
for(j = 0; j < N; j ++)
{
if(map[i][j] != '.')
for(int k = 0; k < 4; k ++)
{
int x = i + vis[k][0],y = j + vis[k][1];
if(y >= 0 && y < N && x >= 0 && x < N && map[x][y] != '.')// 等价map[x][y] == '#'
add(QQ[i][j], QQ[x][y]),add(QQ[x][y], QQ[i][j]);
}
}
printf("Case %d: %d\n",cas,match());
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值