HDU 5556 (最大独立集)

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?
 

Input
The first line of input contains a number  T  indicating the number of test cases ( T200 ).

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 ( 1N,M10 ). 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

题意:一个地图有数字的块表示古老地区,选择最多的地区使得选的地区没有公共边.同类型的古老地区必须

一起选择.

古老的地区最多只有10,所以暴力枚举古老的地区选择状态,然后选择地区周围的点删掉,剩下的点构成二分

图关系,答案就是选择的古老地区+剩下点的最大独立集.

#include <bits/stdc++.h>
using namespace std;
#define maxn 11
#define maxm 111

char mp[maxn][maxn], a[maxn][maxn];
bool used[maxn][maxn];
int n, m;
bool have[maxn];//某个数字是否出现
vector <int> num;//出现的数字
#define move Move
const 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值