【HDU】5093 二分图 ?最大匹配 行列两个集合间的最大匹配

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

题意:

给定一个MxN大小的图,有3种点,冰山、浮冰、海。现在希望能在图中放置尽可能多的船。船的四个方向上不能有其他的船,除非有冰山阻隔。

思路:
可以将一行被冰山隔开且包含海水的连续区域叫做“块”。
把每个横向“块”看做二部图中的X中的顶点,竖向“块”看做集合中Y的顶点,若两个“块”有公共的顶点海水,于是就连一条边。

这样就转换成了没有公共顶点的最大边集,即最大匹配。
我们怎么去求“块”呢?用一个2个二维数组x,y来对水平方向和垂直方向上的“块”进行编号,

编号之后如果两个块有公共的海水的话,那么就在“块”与“块”之间连边,等于说是,这个点只能利用一次。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 55
using namespace std;

int map[maxn * 55][maxn * 55];
char str[maxn][maxn];
int used[maxn * 55];
int link[maxn * 55];
int x[maxn][maxn];
int y[maxn][maxn];
int n, m;
int X1, Y1;

void init(){
    memset(map, 0, sizeof(map));
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
}

void input(){
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i)
        scanf("%s", str[i]);
}
void creat_x(){
    X1 = 1;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(str[i][j] == '*')
                x[i][j] = X1;
            if(str[i][j] == '#')
                X1++;
        }
        X1++;
    }
    return ;
}

void creat_y(){
    Y1 = 1;
    for(int j = 0; j < m; ++j){
        for(int i = 0; i < n; ++i){
            if(str[i][j] == '*')
                y[i][j] = Y1;
            if(str[i][j] == '#')
                Y1++;
        }
        Y1++;
    }
    return ;
}

void getmap(){
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j)
            if(str[i][j] == '*')
            map[x[i][j]][y[i][j]] = 1;
    }
    return ;
}

bool dfs(int x){
    for(int i = 1; i < Y1; ++i){
        if(map[x][i] && !used[i]){
            used[i] = 1;
            if(link[i] == -1 || dfs(link[i])){
                link[i] = x;
                return true;
            }
        }
    }
    return false;
}

int hungary(){
    int ans = 0;
    memset(link, -1, sizeof(link));
    for(int j = 1; j < X1; ++j){
        memset(used, 0, sizeof(used));
        if(dfs(j))
            ans++;
    }
    return ans;
}
int main (){
    int T;
    scanf("%d", &T);
    while(T--){
        init();
        input();
        creat_x();
        creat_y();
        getmap();
        int sum = hungary();
        printf("%d\n", sum);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值