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. Just a Hook:线段树+区间修改

    E - Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most ...

  2. linux云主机小技巧

    微信服务器安装 安装库 python 3.5环境下 pip安装web.py时 会报错 "no module named "utils" 等问题 更换命令为“pip ins ...

  3. linux下搭建python机器学习环境

    前言 在 linux 下搭建 python 机器学习环境还是比较容易的,考虑到包依赖的问题,最好建立一个虚拟环境作为机器学习工作环境,在建立的虚拟环境中,再安装各种需要的包,主要有以下6个(这是看这个 ...

  4. 如何更改Arcmap里经纬度小数点后面的位数?

    customize>arcmap option>data view >round coordinate to 改成想要显示的小数位数

  5. 2.hive里的增删改查

    1.hive的增删改查 查询数据库 hive> show databases; OK default Time taken: 0.254 seconds, Fetched: 1 row(s) h ...

  6. Scrum 项目6.0-展示Sprint回顾的过程及成果。

    6.0----------------------------------------------------- sprint演示 1.坚持所有的sprint都结束于演示. 团队的成果得到认可,会感觉 ...

  7. Scrum立会报告+燃尽图(十一月十六日总第二十四次):功能开发与设计页面

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2384 项目地址:https://git.coding.net/zhang ...

  8. vue知识拓展

    组件 *组件里面如果要放数据:        data必须是函数的形式,函数必须返回一个对象(json),有的时候我们自己创建的组件需要使用到自己的数据,此外组建中也可以放入自己的其他的比如事件之类的 ...

  9. 使用 Maven 管理项目

    最近的练手项目使用的是 Maven 在管理项目,在使用 Maven 管理项目时,三层的开发时分模块开发的,parent-dao-service-web,所有的spring+struts + Hiber ...

  10. SPOJ3899——Finding Fractions

    SPOJ上的每个题目都做得我泪牛满面. 这个题目也是的.题目意思是给你两个分数a/b和c/d,要你求出一个分数p/q,使得a/b<p/q<c/d,且p最小. 看完题目后半天都没有任何思路哦 ...