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. python3【基础】-集合

    集合( set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员(不可重复) class set(object) | set() - ...

  2. Python Pygame (4) 图像的变换

    Pygame中的transform模块可以使得你能够对图像(也就是Surface对象)做各种动作,列如左右上下翻转,按角度转动,放大缩小......,并返回Surface对象.这里列举了transfo ...

  3. Tempter of the Bone HDU 1010(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. Polycarp and Letters(set首战!)

    Description Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s con ...

  5. 软工 · BETA 版冲刺前准备(团队)

    软工 · BETA 版冲刺前准备(团队) 过去存在的问题 组员之间缺乏沟通,前后端缺乏沟通协作 组员积极性不高 基础知识不够扎实 手动整合代码效率过低 我们已经做了哪些调整/改进 通过会议加强组员之间 ...

  6. 福大软工1816:Beta(3/7)

    Beta 冲刺 (3/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 参与开发关键词提醒部分 展示GitHu ...

  7. VMware12 pro装unlocker207补丁后依然没有apple mac选项,问题解决

    把VMware所有的服务先停止,任务管理器里面的也停止.然后再安装unlocker207补丁就行了.亲测.

  8. Struts2(四)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.struts2配置文件加载的顺序 struts2的StrutsPrepareAndExecuteFilter拦截器中对Dispatc ...

  9. 第七周PSP(10.27-11.03)

    psp   进度条 项目 细则 总计 代码行数   0 随笔字数   0 知识点   无 累计曲线 饼图

  10. 【Leetcode】771. Jewels and Stones

    (找了leetcode上最简单的一个题来找一下存在感) You're given strings J representing the types of stones that are jewels, ...