HDU 5556 Land of Farms 最大团

来源:互联网 发布:手机 电脑 相册软件 编辑:程序博客网 时间:2024/06/03 21:54

一个N*M的矩阵,一个农场会覆盖一片连通的区域,新农场与其他农场不可相邻,新农场可以建在空地上,也可以完全地占据一个旧农场,旧农场(不超过10个)已给定,问最多能安排多少个农场?

考虑将将旧农场的所有格子缩成1点,空地保持1格1点,那么问题就变成求图的最大独立点集,求补图的最大团即可。

Rank 6惊了

#include <cstdio>#include <cstring>using namespace std;#define FOR(i,j,k) for(int i=j;i<=k;++i)const int dx[] = {1, 0, -1, 0};const int dy[] = {0, 1, 0, -1};const int N = 105;int ans, f[N], set[N][N], a[N][N], id[N][N], p[N];char mp[N][N];bool dfs(int sz, int dep) {    if (!sz)         if (dep > ans) ans = dep, 1;        else return 0;    FOR(i,1,sz) {        if (dep + sz - i + 1 <= ans) return 0;        int u = set[dep][i];        if (dep + f[u] <= ans) return 0;        int num = 0;        FOR(j,i+1,sz) if (a[u][set[dep][j]])            set[dep + 1][++num] = set[dep][j];        if (dfs(num, dep + 1)) return 1;    }    return 0;}int main() {    int l, w, t, n, sz, kase = 0;    scanf("%d", &t);    while (t--) {        n = 0;        scanf("%d%d", &l, &w);        FOR(i,0,9) p[i] = 0;        FOR(i,1,l) {            scanf("%s", mp[i] + 1);            FOR(j,1,w)                if (mp[i][j] == '.')                    id[i][j] = ++n;                else {                    if (!p[mp[i][j] - '0'])                        p[mp[i][j] - '0'] = ++n;                    id[i][j] = p[mp[i][j] - '0'];                }        }        FOR(i,1,n) FOR(j,1,n) a[i][j] = i != j;        FOR(i,1,l) FOR(j,1,w) FOR(k,0,3) {            int nx = i + dx[k], ny = j + dy[k];            if (nx < 1 || nx > l || ny < 1 || ny > w)                continue;            a[id[i][j]][id[nx][ny]] = 0;            a[id[nx][ny]][id[i][j]] = 0;        }        ans = 0;        for (int i = n; i; i--) {            sz = 0;            FOR(j,i+1,n) if (a[i][j]) set[1][++sz] = j;            dfs(sz, 1);            f[i] = ans;        }        printf("Case #%d: %d\n", ++kase, ans);    }    return 0;}

Land of Farms

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?

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 integers N 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.

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

Source

2015ACM/ICPC亚洲区合肥站-重现赛(感谢中科大)

Recommend

wange2014

0 0