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. [转载] Centos7的安装、Docker1.12.3的安装,以及Docker Swarm集群的简单实例

    1.环境准备 ​ 本文中的案例会有四台机器,他们的Host和IP地址如下 c1 -> 10.0.0.31 c2 -> 10.0.0.32 c3 -> 10.0.0.33 c4 -&g ...

  2. Docker 快速入门教程

    本文目的是给几乎从未接触过docker,或者仅仅是听说或者通过新闻了解过Docker的同学 通过一个已有的Docker仓库构建和提交自己的Docker 镜像 这里会涉及到一些概念,但是不单独介绍 这里 ...

  3. Selenium WebDriver 下 plugin container for firefox has stopped working

    用selenium 的webdriver 和 firefox 浏览器做自动化测试,经常会出现 plugin container for firefox has stopped working 如下图所 ...

  4. Jquery mobile div常用属性

    组件 页面 jQuery Mobile 应用了 HTML5 标准的特性,在结构化的页面中完整的页面结构分为 header. content.footer 这三个主要区域. 在 body 中插入内容块: ...

  5. vue+postcss报错: variable '--primary-color' is undefined and used without a fallback

    之前vue-cli3引入postcss的配置: https://www.cnblogs.com/XHappyness/p/7676680.html 发现这么一个问题,我再全局global.css中定义 ...

  6. PHP面向对象之抽象类,抽象方法

    抽象类,抽象方法 抽象类: 是一个不能实例化的类: 定义形式: abstract  class  类名{} 为什么需要抽象类: 它是为了技术管理而设计! 抽象方法: 是一个只有方法头,没有方法体的方法 ...

  7. form 表单提交类型

    multipart/form-data与x-www-form-urlencoded区别 multipart/form-data:既可以上传文件等二进制数据,也可以上传表单键值对,只是最后会转化为一条信 ...

  8. idea导出包含main函数的jar

    1.首先打开File->project stucture->Artifacts 2.按照下图方式: 3.选择面main函数的所在的类,选择MAINFEST.MF问的生成路径 这里一定选择 ...

  9. delphi self 的使用

    delphi之self 在使用delphi的对象技术的时候,经常会看到一个词汇:self,它到底指的是什么呢? 我们还要从对象与类的关系谈起. 类是对将要创建的对象的性质的描述,是一种文档.这很重要: ...

  10. Spring Autowired原理

    今天来整理一下Spring的自动装配 autowire一节,在这里我们要解决以下问题: 什么是自动装配? 自动装配的意义? 自动装配有几种类型? 如何启用自动装配? 自动装配将引发的问题? 一.什么是 ...