HDU5556:Land of Farms(图的最大独立集 & 最大团)

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 N×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? 
Input
The first line of input contains a number  T T indicating the number of test cases ( T200 T≤200). 

Each test case starts with a line containing two integers  N N and  M M, indicating the size of the land. Each of the following  N N lines contains  M M characters, describing the map of the land ( 1N,M10 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 X is the test case number starting from 1.  Y 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
题意:有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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值