HDU 5556 (最大独立集)
来源:互联网 发布:小米淘宝旗舰店优惠券 编辑:程序博客网 时间:2024/04/28 18:23
Land of Farms
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 320 Accepted Submission(s): 106
Problem Description
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?
Input
The first line of input contains a number T indicating the number of test cases (T≤200 ).
Each test case starts with a line containing two integersN and M , indicating the size of the land. Each of the following N lines contains M characters, describing the map of the land (1≤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.
Each test case starts with a line containing two integers
Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the maximum number of new farms.
Sample Input
33 4..3.023..2112 3......4 411111..119911111
Sample Output
Case #1: 4Case #2: 3Case #3: 1
题意:一个地图有数字的块表示古老地区,选择最多的地区使得选的地区没有公共边.同类型的古老地区必须
一起选择.
古老的地区最多只有10,所以暴力枚举古老的地区选择状态,然后选择地区周围的点删掉,剩下的点构成二分
图关系,答案就是选择的古老地区+剩下点的最大独立集.
#include <bits/stdc++.h>using namespace std;#define maxn 11#define maxm 111char mp[maxn][maxn], a[maxn][maxn];bool used[maxn][maxn];int n, m;bool have[maxn];//某个数字是否出现vector <int> num;//出现的数字#define move Moveconst int move[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};bool legal (int x, int y) { if (x < 0 || y < 0 || x >= n || y >= m) return 0; return 1;}void del (int x, int y) { for (int i = 0; i < 4; i++) { int xx = x+move[i][0], yy = y+move[i][1]; if (!legal (xx, yy) || mp[xx][yy] != '.') continue; used[xx][yy] = 1; }}struct node { int u, v, next;}edge[maxm*maxm];int head[maxm], cnt, tot;void add_edge (int u, int v) { edge[cnt].u = u, edge[cnt].v = v, edge[cnt].next = head[u], head[u] = cnt++;}int id (int x, int y) { return x*m+y;}int pre[maxm];bool vis[maxm];bool dfs (int u) { for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (!vis[v]) { vis[v] = 1; if (pre[v] == -1 || dfs (pre[v])) { pre[v] = u; return 1; } } } return 0;}int hungry () { int ans = 0; memset (pre, -1, sizeof pre); for (int i = 0; i < n*m; i++) { memset (vis, 0, sizeof vis); if (dfs (i)) ans++; } return ans;}bool ok () {//判断有没有相邻的古老土地 for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) if (used[x][y] && mp[x][y] != '.') { for (int k = 0; k < 4; k++) { int xx = x+move[k][0], yy = y+move[k][1]; if (!legal (xx, yy) || !used[xx][yy] || mp[xx][yy] == '.') continue; if (mp[xx][yy] != mp[x][y]) { return 0; } } } } return 1;}void solve () { int ans = 0; for (int i = 0; i < (1<<num.size()); i++) {//枚举选择的古老地区 int res = 0; memset (used, 0, sizeof used); int cur = i, j = 0; for (int bit = 1; bit < (1<<num.size()); bit <<= 1, j++) { //cout << ".." << endl; if (cur&bit) { res++; int tmp = num[j]; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { if (mp[x][y] == tmp+'0') { used[x][y] = 1; del (x, y); } } } } } if (!ok ()) continue; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) if (mp[x][y] != '.') used[x][y] = 1; } tot = 0; memset (head, -1, sizeof head); cnt = 0; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) if (!used[x][y]) { tot++; if ((x+y)%2 != 0) continue; for (int k = 0; k < 4; k++) { int xx = x+move[k][0], yy = y+move[k][1]; if (!legal (xx, yy) || used[xx][yy]) continue; add_edge (id (x, y), id (xx, yy)); } } } res += tot-hungry (); ans = max (ans, res); } printf ("%d\n", ans); return ;}int main () { //freopen ("in.txt", "r", stdin); int t, kase = 0; scanf ("%d", &t); while (t--) { scanf ("%d%d", &n, &m); num.clear (); memset (have, 0, sizeof have); printf ("Case #%d: ", ++kase); for (int i = 0; i < n; i++) { scanf ("%s", mp[i]); for (int j = 0; j < m; j++) { if (mp[i][j] != '.' && !have[mp[i][j]-'0']) { have[mp[i][j]-'0'] = 1; num.push_back (mp[i][j]-'0'); } } } solve (); } return 0;}
0 0
- HDU 5556 (最大独立集)
- 【最大独立集】HDU
- hdu 1068(最大独立集)
- HDU-1068 最大独立集
- hdu 2768 最大独立集
- hdu 1068 (最大独立集)
- HDU 1068 最大独立集
- hdu 1068 最大独立集
- hdu 5556 Land of Farms(最大独立集)
- hdu 3829 二分图最大独立集
- hdu 2458 Kindergarten (最大独立集)
- hdu 1569 最大点权独立集
- hdu 1068(最大独立集)
- hdu 2768(最大独立集)
- hdu 2458(最大独立集)
- HDU 1569 最大点权独立集
- hdu 1083 Courses (最大边独立集)
- hdu 3829 二分图最大独立集
- Codeforces 367A Sereja and Algorithm 【规律】
- Redis配置数据持久化---APPEND ONLY MODE
- 1154: 【C语言训练】邮票组合问题*__简单题
- Ubuntu安装深度学习平台Torch7以及zbs_torch IDE
- Problem Q
- HDU 5556 (最大独立集)
- hdu5379(树形dp)
- android之在activity中控制另一个activity的UI更新_如何在activity之间传递handler&利用broadcast广播机制
- POJ 2532 Star
- SAS:where 语句
- Linux常用软件总结
- Chapter 13:Understanding HTML&Assignment(2)
- IP数据报格式解读
- 第五章 深度神经网络为何很难训练