HDU5556:Land of Farms(图的最大独立集 & 最大团)
来源:互联网 发布:java计算时间差 毫秒 编辑:程序博客网 时间:2024/06/04 00:46
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×M grids. 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?
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?
Each test case starts with a line containing two integers
33 4..3.023..2112 3......4 411111..119911111
Case #1: 4Case #2: 3Case #3: 1题意:有N*M的地图,要求建尽量多的不相邻的农场,若建在有数字的地方,拥有同样数字的格子都要建农场(若这些特殊农场本来就相邻也没关系),问最多建几个。
思路:将每个'.'和每类数字编号,相邻的元素建无向图,相当于求图的最大独立集,相当于求补图的最大团。
因为HDU挂了,程序待提交测试。
# include <iostream># include <cstdio># include <vector># include <cstring># include <algorithm>using namespace std;const int maxn = 1e5+8;char s[13][13];int n, m, g[300][300];int id[13][13], vis[103], tmp[103], tot=0;int dp[103], a[103][103], imax, _;void init(){ for(int i=1; i<=n; ++i) { for(int j=1; j<=m; ++j) { if(s[i][j] == '.') id[i][j] = ++tot; else if(!vis[s[i][j]]) { vis[s[i][j]] = ++tot; id[i][j] = tot; } else id[i][j] = vis[s[i][j]]; } } for(int i=1; i<=n; ++i) { for(int j=1; j<=m; ++j) { if(i>1 && (s[i-1][j] == '.'||s[i-1][j] != s[i][j])) g[id[i][j]][id[i-1][j]] = 1; if(i<n && (s[i+1][j] == '.'||s[i+1][j] != s[i][j])) g[id[i][j]][id[i+1][j]] = 1; if(j>1 && (s[i][j-1] == '.'||s[i][j-1] != s[i][j])) g[id[i][j]][id[i][j-1]] = 1; if(j<m && (s[i][j+1] == '.'||s[i][j+1] != s[i][j])) g[id[i][j]][id[i][j+1]] = 1; } }}void dfs(int up, int dep){ if(up == 0) { if(dep > imax) imax = dep; return; } for(int i=0; i<up; ++i) { int u = a[dep][i]; if(dep + tot - u + 1 <= imax) return; if(dep + dp[u] <= imax) return; _++; int cnt = 0; for(int j=i+1; j<up; ++j) { int v = a[dep][j]; if(!g[u][v]) a[dep+1][cnt++] = v; } dfs(cnt, dep+1); --_; }}int solve(){ for(int i=tot; i>=1; --i) { int cnt = 0; _ = 0; tmp[_++] = i; for(int j=i+1; j<=tot; ++j) if(!g[i][j]) a[1][cnt++] = j; dfs(cnt, 1); dp[i] = imax; } return imax;}int main(){ int t, cas=1; scanf("%d",&t); while(t--) { imax = tot = 0; memset(g, 0, sizeof(g)); memset(vis, 0, sizeof(vis)); memset(dp, 0, sizeof(dp)); scanf("%d%d",&n,&m); for(int i=1; i<=n; ++i) scanf("%s",s[i]+1); init(); printf("Case #%d: %d\n",cas++,solve()); } return 0;}
阅读全文
0 0
- HDU5556:Land of Farms(图的最大独立集 & 最大团)
- HDU5556 Land of Farms 最大独立集
- HDU5556 Land of Farms(最大独立集)
- 【HDU5556 2015合肥赛区E】【最大团or二分图匹配】Land of Farms 不同编号不相邻条件下的最大农场数
- hdu5556 Land of Farms
- hdoj 5556 Land of Farms 【DFS + 最大独立集->补图最大团】
- hdu 5556 Land of Farms(最大独立集)
- HDU 5556 Land of Farms 最大团
- HDU-5556 Land of Farms(无向图的最大独立集)
- hdu 5556 Land of Farms【思维+暴力枚举+最大独立集】好题!
- 无向图的最大团与最大独立集
- 无向图的最大独立集和最大团
- 最大团,最大独立集
- POJ 1419 Graph Coloring(最大独立集/补图的最大团)
- POJ3692 二分图最大团 最大独立集
- hdu 5556 Land of Farms(二分图匹配)
- HDU 5556 Land of Farms(枚举+二分图匹配)
- POJ 1419 最大独立集(即最大团)
- js获取样式
- 简单工厂
- 以Point类为基础,定义一个平面中的Circle类
- 【笔记】JAVA API 访问 HDFS
- mysql中的连接操作
- HDU5556:Land of Farms(图的最大独立集 & 最大团)
- MYSQL初次安装
- 设计模式---状态模式
- 无限轮播
- 1052. 卖个萌 (20)
- 如何避开无线AP选购的“误区”呢?
- TCP协议总结(理解三次握手,四次挥手)
- 欢迎使用CSDN-markdown编辑器
- Gym 100825 G