Battle ships(二分图)

Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.

Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.

But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.

The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:

A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg

Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
 

 

Input
There is only one integer T (0<T<12) at the beginning line, which means following T test cases.

For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
 

 

Output
For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.
 

 

Sample Input
2
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
 

 

Sample Output

 

3
5

      题意:

      给出 T 组样例,输入 N 行 M 列。o 代表是可穿区域,# 代表不可穿区域,* 代表可放区域。问要求放物体到 * 上面,要求不允许放同一行或者同一列,# 不可以穿过,但是 o 是可以穿过的。输出最大能放的个数。

 

      思路:

      二分图。连通块建图。当时想到的是每个点之间建图,并没有想到用连通块。每个点建图的话,构出来的图有可能不是二分图,所以不能用点建图。

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 55;

char Map[MAX][MAX];
int cro[MAX][MAX], ver[MAX][MAX];
int n, m;

int G[MAX * MAX][MAX * MAX];
int u;
int linker[MAX * MAX];
bool vis[MAX * MAX];

void build () {
    memset(cro, -1, sizeof(cro));
    memset(ver, -1, sizeof(ver));

    u = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (Map[i][j] == '*' && cro[i][j] == -1) {
                int t = j;
                ++u;
                while (t <= m && Map[i][t] != '#') {
                    if (Map[i][t] == '*') cro[i][t] = u;
                    ++t;
                }
                j = t;
            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (Map[i][j] == '*' && ver[i][j] == -1) {
                int t = i;
                ++u;
                while (t <= n && Map[t][j] != '#') {
                    if (Map[t][j] == '*') ver[t][j] = u;
                    ++t;
                }
            }
        }
    }

    memset(G, 0, sizeof(G));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (Map[i][j] == '*') {
                int a = cro[i][j];
                int b = ver[i][j];
                G[a][b] = G[b][a] = 1;
            }
        }
    }
}

bool dfs (int x) {
    for (int i = 1; i <= u; ++i) {
        if (G[x][i] && !vis[i]) {
            vis[i] = 1;
            if (linker[i] == -1 || dfs(linker[i])) {
                linker[i] = x;
                return true;
            }
        }
    }

    return false;
}

int hungary () {
    int res = 0;
    memset(linker, -1, sizeof(linker));

    for (int i = 1; i <= u; ++i) {
        memset(vis, 0, sizeof(vis));
        if (dfs(i)) ++res;
    }

    return res;
}

int main() {

    int t;
    scanf("%d", &t);

    while (t--) {

        scanf("%d%d", &n, &m);

        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                scanf(" %c", &Map[i][j]);
            }
        }

        build();

        printf("%d\n", hungary() / 2);
    }

    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值