HDU 5556 Land of Farms 最大团

本文探讨了一种策略,用于在限定区域内最大化农场数量,同时遵循特定的规则以避免农场之间的冲突。通过将问题转化为图论问题并采用深度优先搜索算法解决,实现了对最优农场布局的高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个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

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

Source

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

Recommend

wange2014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值