挖油田——二分图最大匹配


问题描述:

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.

样例输入:

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

样例输出:

Case 1: 3


题目大意:

将‘#’看作油漆,将给定的图中的‘#’相邻的所有‘#’作为一个整体。

你的任务是统计图中有能配出几个整体。

 


思路分析:

本体为二分配图(男生女生搭配过山车)的模板改编题。首先搜索图找到一个‘#’后,再遍历该‘#’的附近是否有‘#’。

 

解决方案:

 

#include  "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;
char a[1100][1100];
int e[1100][1100];
int vis[1100][1100];
int match[1000],book[1100];
int to[4][2]={0,1,1,0,-1,0,0,-1};
int n,m,k,h=1;
int s=0;
//二分配图算法模板 
int dfs(int x)
{
	for(int i=0;i<k;i++)
	{
		if(e[x][i]==1&&book[i]==0)
		{
			book[i]=1;
			if(match[i]==0||dfs(match[i]))
			{
				match[i]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main ()
{
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		int sum=0;
		memset(vis,0,sizeof(vis));
		memset(e,0,sizeof(e));
		memset(match,0,sizeof(match));	
		k=0;	
		for(int i=0;i<m;i++)		
			{
				scanf("%s",a[i]);
				for(int j=0;j<m;j++)														
				if(a[i][j]=='#')
				{
					vis[i][j]=k++;//将图中‘#’依次编号。第一个‘#’为1,以此类推。 
				}
			}		
		for(int i=0;i<m;i++)		
			 for(int j=0;j<m;j++)
			 {
			 
			 if(a[i][j]!='#')
			 continue;
			 //
			 if(a[i+1][j]=='#')
			 e[vis[i][j]][vis[i+1][j]]=1;
			 if(a[i][j+1]=='#')
			 e[vis[i][j]][vis[i][j+1]]=1;
			 if(a[i-1][j]=='#')
			 e[vis[i][j]][vis[i-1][j]]=1;
			 if(a[i][j-1]=='#')
			 e[vis[i][j]][vis[i][j-1]]=1;
			 /*	这一部分是遍历‘#’附近的‘#’,并建立关系。
			 例如:编号为1,2的‘#’建立关系就是:1,2可以配对。(类似一号女生和二号男生配对)		 			 													
			 也可以用方向搜索代替。*/ 
			}	
			//遍历建立的所有的关系。
			 
			for(int i=0;i<k;i++)
			{
				memset(book,0,sizeof(book));
				if(dfs(i))
				sum++;
			}			
		printf("Case %d: %d\n",h++,sum/2);
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值