Farmer John and his brothers have found a new land. They are so excited and decide to build new farms on the land. The land is a rectangle and consists of N×MN×Mgrids. A farm consists of one or more connected grids. Two grids are adjacent if they share a common border, i.e. their Manhattan distance is exactly 1. In a farm, two grids are considered connected if there exist a series of adjacent grids, which also belong to that farm, between them.

Farmer John wants to build as many farms as possible on the new land. It is required that any two farms should not be adjacent. Otherwise, sheep from different farms would fight on the border. This should be an easy task until several ancient farms are discovered.

Each of the ancient farms also consists of one or more connected grids. Due to the respect to the ancient farmers, Farmer John do not want to divide any ancient farm. If a grid from an ancient farm is selected in a new farm, other grids from the ancient farm should also be selected in the new farm. Note that the ancient farms may be adjacent, because ancient sheep do not fight each other.

The problem is a little complicated now. Can you help Farmer John to find a plan with the maximum number of farms? 

InputThe first line of input contains a number TT indicating the number of test cases (T≤200T≤200).

Each test case starts with a line containing two integers NN and MM, indicating the size of the land. Each of the following NN lines contains MM characters, describing the map of the land (1≤N,M≤101≤N,M≤10). A grid of an ancient farm is indicated by a single digit (0-9). Grids with the same digit belong to the same ancient farm. Other grids are denoted with a single character “ .”. It is guaranteed that all test cases are valid. 
OutputFor each test case, output a single line consisting of “ Case #X: Y”. XX is the test case number starting from 1. YY is the maximum number of new farms.Sample Input

3
3 4
..3.
023.
.211
2 3
...
...
4 4
1111
1..1
1991
1111

Sample Output

Case #1: 4
Case #2: 3
Case #3: 1
题意:
一个N*M的矩阵,其中“.”代表空地,“0-9”代表古代建筑,我们如果选择了一个编号的古代建筑想要建立,
那么对应就要将全部该编号的建筑建立起来,如果在空地上建筑,只建立当前点。问最多能够建立多少种建筑,
并且每两种建筑之间没有公共边。
这题我做的非常吃力。 冷静分析 先考虑没有任何建筑的情况,也就是两两不能相连,看有多少个建筑
这个就是跑一边二分图最大匹配就出来了。
知道这个之后,然后再思考建筑总共只有10种,所以枚举一下有哪几种建筑存在,
暴搜每一种合法的情况。
细节一定要处理好,选定的建筑周围的空白格子根据题意要抠出来。选的的建筑不能有相连;
ans=扣点后的空白的数目-扣点后的空白的数目的最大匹配+选择的建筑的数目
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)+
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("data.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1e9 + ;
const int maxn = 3e6 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
char a[][];
int dx[] = {, , , -};
int dy[] = {, -, , };
int t, cas = , n, m, sum, ans, use[], vis[];
int vis2[][], vis3[][], match[], vis4[];
vector<int>mp[];
int Find ( int u ) {
for ( int i = ; i < mp[u].size(); i++ ) {
int v = mp[u][i];
if ( !vis4[v] ) {
vis4[v] = ;
if ( match[v] == - || Find ( match[v] ) ) {
match[v] = u;
return ;
}
}
}
return ;
}
void solve() {
mem ( vis2, );
for ( int i = ; i < n ; i++ ) {
for ( int j = ; j < m ; j++ ) {
if ( a[i][j] == '.' ) {
for ( int k = ; k < ; k++ ) {
int x = i + dx[k], y = j + dy[k];
if ( x >= && x < n && y >= && y < m && a[x][y] != '.' && vis[a[x][y] - ''] ) vis2[i][j] = ;
}
} else {
vis2[i][j] = ;
for ( int k = ; k < ; k++ ) {
int x = i + dx[k], y = j + dy[k];
if ( x >= && x < n && y >= && y < m ) {
if ( vis[a[i][j] - ''] == && vis[a[x][y] - ''] == && a[i][j] != a[x][y] ) {
return ;
}
}
}
}
}
}
int num = ;
for ( int i = ; i < n ; i++ )
for ( int j = ; j < m ; j++ )
if ( !vis2[i][j] ) vis3[i][j] = ++num;
for ( int i = ; i <= n * m ; i++ ) mp[i].clear();
for ( int i = ; i < n ; i++ ) {
for ( int j = ; j < m ; j++ ) {
if ( !vis2[i][j] ) {
for ( int k = ; k < ; k++ ) {
int x = i + dx[k], y = j + dy[k];
if ( x >= && x < n && y >= && y < m && !vis2[x][y] ) mp[vis3[i][j]].push_back ( vis3[x][y] );
}
}
}
}
int res = ;
mem ( match, - );
for ( int i = ; i <= num ; i++ ) {
mem ( vis4, );
if ( Find ( i ) ) res++;
}
int key = ;
for ( int i = ; i < sum ; i++ ) if ( vis[i] ) key++;
// bug, fuck ( key ), fuck ( num ), fuck ( res );
ans = max ( ans, key + num - res / );
}
void dfs ( int now ) {
if ( now == sum ) {
solve();
return ;
}
vis[now] = ;
dfs ( now + );
vis[now] = ;
dfs ( now + );
}
int main() {
// FIN;
sf ( t );
while ( t-- ) {
sum = ;
mem ( use, - );
sff ( n, m );
for ( int i = ; i < n ; i++ ) {
scanf ( "%s", a[i] );
for ( int j = ; j < m ; j++ ) {
if ( a[i][j] != '.' ) {
if ( use[a[i][j] - ''] != - ) a[i][j] = use[a[i][j] - ''] + '';
else use[a[i][j] - ''] = sum++, a[i][j] = use[a[i][j] - ''] + '';
}
}
}
ans = ;
dfs ( );
printf ( "Case #%d: %d\n", cas++, ans );
}
return ;
}
 

Land of Farms HDU - 5556 二分图匹配的更多相关文章

  1. hdu 2063 二分图匹配

    题意:一些女的和一些男的有好感,有好感的能一起坐过山车,问最多能组成多少对 hdu 11 页上少有的算法题,二分图匹配问题,匈牙利算法,对于每一个汉子,看和他有好感的妹子有没有配对了,没有配对过就可以 ...

  2. hdu 1281 二分图匹配

    题目:在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下.但是某些格子若不放子,就 无法保证放尽量多的“车”,这样的格子被称做重要点 ...

  3. hdu 1507(二分图匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. hdu 4185 二分图匹配

    题意用1*2的木板覆盖矩阵中的‘#’,(木板要覆盖的只能是‘#’),问最多能用几个木板覆盖 将#抽象为二分图的点,一个木板就是一个匹配,注意最后结果要除以2 Sample Input 1 6 .... ...

  5. 过山车 HDU 2063 (二分图匹配裸题)

    Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生 ...

  6. Fire Net HDU - 1045 (二分图匹配)

    题意: 给出一张图,图中'X'表示wall,'.'表示空地,可以放置blockhouse同一条直线上只能有一个blockhouse,除非有wall 隔开,问在给出的图中最多能放置多少个blockhou ...

  7. Girls and Boys HDU - 1068 二分图匹配(匈牙利)+最大独立集证明

    最大独立集证明参考:https://blog.csdn.net/qq_34564984/article/details/52778763 最大独立集证明: 上图,我们用两个红色的点覆盖了所有边.我们证 ...

  8. HDU 1507 Uncle Tom's Inherited Land*(二分图匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. hdu 5556 Land of Farms 最大团+暴力

    Land of Farms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. cinder的组件

    跟nova相似,cinder也有很多组件,每个组件负责各自的业务,然后共同协作完成volume的管理.组件之间的通信方式与nova个组件之间的通信方式相同,都是通过消息队列进行通信. cinder-a ...

  2. centos上搭建git服务--4

    Git是目前世界上最先进的分布式版本控制系统(没有之一).使用Svn的请参考<版本控制-svn服务器搭建和常用命令(centos 6.3)>,下面介绍Git的常用命令 常用命令 简单版 升 ...

  3. 移动端rem用法总结

    先介绍一下这个近年来突起的黑马 CSS3中新增的属性,从IE9开始兼容,手机端都兼容.参考的是<html>这个标签的font-size.rem中的r就是root根的意思.所以rem要比em ...

  4. 团队backlog和燃尽图

    首先我们的团队的任务是做一款一对一交流的软件,我们团队的backlog如下: 用电脑搭建一个服务器一对一平台 实现数据库和Android的链接 编写登陆,注册页面,和登陆成功界面,和登陆失败页面 实现 ...

  5. ACM-ICPC 2018 沈阳赛区网络预赛

    Supreme Number 1000ms 131072K   A prime number (or a prime) is a natural number greater than 111 tha ...

  6. spark作用流程

    原文:https://www.cnblogs.com/asura7969/p/8441471.html https://blog.csdn.net/xu__cg/article/details/700 ...

  7. HostsConfig文件修改器

    Hosts文件修改器 HostsConfig v1.1 免费版 最近工作需要,经常需要更换各种域名的内外网配置,频繁的修改HOSTS文件,很多的时间都用在的修改HOSTS文件上,工作效率大大降低,课余 ...

  8. Bootstrap-tagsinput标系统使用心得

    最近工作中由于需求使用到了Bootstrap-tagsinput标系统,我的需求是: 1)能够从后台数据库获取标签信息展示到前端页面: 2)能够实现输入标签添加到后台,并ajax刷新页面: 3)能够实 ...

  9. PreparedStatement的execute误解

    boolean execute()  throws SQLException在此 PreparedStatement 对象中执行 SQL 语句,该语句可以是任何种类的 SQL 语句.一些特别处理过的语 ...

  10. 关于SIGPIPE信号

    对一个对端已经关闭的socket调用两次write, 第二次将会生成SIGPIPE信号, 该信号默认结束进程.具体的分析可以结合TCP的"四次握手"关闭. TCP是全双工的信道, ...