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 ;
}
 

最新文章

  1. TCP/IP之TCP_NODELAY与TCP_CORK
  2. ios开发--animation flash动画
  3. nodejs redis
  4. hdu 1709 The Balance
  5. ASP.NET中身份验证的三种方法
  6. CodeForces 540E - Gerald and Giant Chess(数论)
  7. 把cygwin加入右键菜单
  8. ASP.NET菜鸟之路之Seesion小例子
  9. python保留指定文件、删除目录其他文件的功能(2)
  10. JS事件——禁止事件冒泡和禁止默认事件
  11. js 的作用域 域解析 分析
  12. 如何将Ubuntu部署到U盘中,用U盘安装linux操作系统
  13. 2018-07-10 为Chrome和火狐浏览器编写扩展
  14. 好代码是管出来的——使用GitHub实现简单的CI/CD
  15. dotnet core 入门命令
  16. Linux基础之常用命令整理(二)
  17. GitHub-标签管理
  18. 转:MySQL如何修改密码
  19. 【springboot】【socket】spring boot整合socket,实现服务器端两种消息推送
  20. nginx跨域

热门文章

  1. react-native debug js remotely跨域问题
  2. Java中的网络编程-1
  3. mysql的程序组成
  4. 结对编程--fault,error,failure的程序设计
  5. Windows下基于http的git服务器搭建-gitstack
  6. PAT 甲级 1129 Recommendation System
  7. 剖析Vue原理&amp;实现双向绑定MVVM-2
  8. Privoxy
  9. sublime py不能输入中文
  10. Kafka发布订阅消息