HDU 5093 Battle ships(二部图最大匹配)

Battle ships

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


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)都不能再用,就是一个二部图的思想。




参考代码: 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

const int MAXN = 55*25;
char graph[MAXN][MAXN];
int nCase, m, n, idx, totalR, matching, pairRC[MAXN];
bool visitedC[MAXN];
vector<int> reachedC[MAXN];

struct {
    int r, c;
} id[MAXN][MAXN];

void init() {
    idx = matching = 0;
    for (int i = 0; i < MAXN; i++) {
        reachedC[i].clear();
    }
    memset(pairRC, -1, sizeof(pairRC));
}

void input() {
    scanf("%d%d", &m, &n);
    for (int i = 0; i < m; i++) {
        scanf("%s", graph[i]);
    }
}

bool findPath(int r) {
    for (int i = 0; i < reachedC[r].size(); i++) {
        int c = reachedC[r][i];
        if (!visitedC[c]) {
            visitedC[c] = true;
            if (pairRC[c] == -1 || findPath(pairRC[c])) {
                pairRC[c] = r;
                return true;
            }
        }
    }
    return false;
}

void solve() {
    for (int i = 0; i < m; i++) {
        id[i][0].r = ++idx;
        for (int j = 1; j < n; j++) {
            if (graph[i][j] == '#' && graph[i][j] != graph[i][j-1]) {
                id[i][j].r = ++idx;
            } else {
                id[i][j].r = idx;
            }
        }
        if (graph[i][n-1] == '#') idx--;
    }
    totalR = idx;
    for (int j = 0; j < n; j++) {
        id[0][j].c = ++idx;
        for (int i = 1; i < m; i++) {
            if (graph[i][j] == '#' && graph[i][j] != graph[i-1][j]) {
                id[i][j].c = ++idx;
            } else {
                id[i][j].c = idx;
            }
        }
        if (graph[n-1][j] == '#') idx--;
    }

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (graph[i][j] == '*') {
                reachedC[id[i][j].r].push_back(id[i][j].c);
            }
        }
    }
/*
    for (int i = 1; i <= totalR; i++) {
        printf("%d: ", i);
        for (int j = 0; j < reachedC[i].size(); j++) {
            printf("%d ", reachedC[i][j]);
        }
        printf("\n");
    }
*/
    for (int r = 1; r <= totalR; r++) {
        memset(visitedC, false, sizeof(visitedC));
        if (findPath(r)) {
            matching++;
        }
    }

    printf("%d\n", matching);
}

int main() {
    scanf("%d", &nCase);
    while (nCase--) {
        init();
        input();
        solve();
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值