`
Simone_chou
  • 浏览: 184722 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Battle ships(二分图)

    博客分类:
  • HDOJ
阅读更多

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;
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics