Hdu5093 Battle ships 二分图

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


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
 

 

Source
 
题意:给出一个地图,*为海水区,o为浮冰区,#为冰山   要在海水区放船,除非被冰山阻挡,否则每行或每列只能放一艘,问最多能放多少
思路:如果不管冰山阻挡的约数条件,每一行或每一列只能被选择一次,就是显然的二分图行列匹配。有了约数条件,其实就是把每一行或每一列根据#来分段,再匹配就行了
20160407群赛-补
复制代码
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
char Mat[N][N];
int n, m;
int cnt1, cnt2;
int x[N][N], y[N][N], g[N * N][N * N];
void init() {
    cnt1 = 0, cnt2 = 0;
    for(int i = 0; i < n; ++i) {
        int p = 0, j, f = 0;
        while(p < m) {
            f = 0;
            for(j = p; j < m; ++j) {
                    if(Mat[i][j] == '*') { f = 1; x[i][j] = cnt1; }
                    else if(Mat[i][j] == '#') break;
            }
            if(f)
            ++cnt1;
            p = j;
            ++p;
        }
    }
    for(int i = 0; i < m; ++i) {
        int p = 0, j, f = 0;
        while(p < n) {
            f = 0;
            for(j = p; j < n; ++j) {
                    if(Mat[j][i] == '*') { f = 1; y[j][i] = cnt2; }
                    else if(Mat[j][i] == '#') break;
            }
            if(f)
            ++cnt2;
            p = j;
            ++p;
        }
    }
}
void look(int a[][55]) {
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) printf("%d ", a[i][j]);
        puts("");
    }
}
void get() {
    memset(g, 0, sizeof g);
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < m; ++j)
        if(Mat[i][j] == '*')
        g[ x[i][j] ][ y[i][j] ] = 1;
}
int linker[N * N];
bool used[N * N];
bool dfs(int u) {
    for(int v = 0; v < cnt2; ++v)
        if(g[u][v] && !used[v]) {
            used[v] = true;
            if(linker[v] == -1 || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    return false;
}
int hungary() {
    int res = 0;
    memset(linker, -1, sizeof linker);
    for(int u = 0; u < cnt1; ++u)
    {
        memset(used, false, sizeof used);
        if(dfs(u)) res++;
    }
    return res;
}
int main() {
    int _; scanf("%d", &_);
    while(_ --) {
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; ++i) scanf("%s", Mat[i]);
        init();
        get();
       // look(x);
        //look(y);
       printf("%d\n", hungary());
    }
    return 0;
}
View Code
复制代码

 

posted @   JL_Zhou  阅读(224)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2015-04-08 畅通工程
点击右上角即可分享
微信分享提示