Hdu 1045(二分图最大匹配)

Hdu 1045
题意:题目告诉你一张网格图,图上有的格子有障碍物挡着,问一个在图上最多放几个炮台能覆盖整张图,且不会火力部重叠。

考虑二分图,并且如果没有障碍物,那二分图一条边相当于一个格子,求最大匹配即可。
如果有障碍物,我们不妨拆行拆列,即如果一行中有障碍物,那么障碍物前后的行是互不影响的,所以我们可以拆出一个行,之后也是同理,列的情况也是同理。

(样例一为例)

.X..
....
XX..
....

拆行直接在后面加行即可,行列坐标就可以是((X, X)是X)

(1, 1), (X, X), (5, 3), (5, 4)
(2, 1), (2, 5), (2, 3), (2, 4)
(X, X), (X, X), (6, 3), (6, 4)
(4, 6), (4, 7), (4, 3), (4, 4)

然后用这个坐标来加边,对新图进行最大匹配即可。

#include<cstdio> 
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
int n, hang, lie, vis[1005], cnt, lk[1005];
char map[10][10];
vector<int> G[1005];
inline void ins(int x, int y) {G[x].push_back(y);}
int hungary(int u) {
    for (int i = 0; i < (int)G[u].size(); i++) {
        int v = G[u][i];
        if (vis[v] != cnt) {
            vis[v] = cnt;
            if (!lk[v] || hungary(lk[v])) {
                lk[v] = u;
                return true;
            }
        }
    }
    return false;
}
void clean() {
    hang = lie = 0;
    for (int i = 0; i <= 1000; i++) G[i].clear(), lk[i] = vis[i] = 0;
}
int solve() {
    clean();
    for (int i = 1; i <= n; i++) scanf("%s", map[i] + 1);
    for (int i = 1; i <= n; i++) {
        int fh = 0, fl = 0;
        for (int j = 1; j <= n; j++) {
            if (map[i][j] == 'X') continue;
            if (map[i][j - 1] == 'X') fh = 1, ++hang;
            if (map[i - 1][j] == 'X') fl = 1, ++lie;
            if (fh) {
                if (map[i - 1][j] == 'X' && fl) {
                    ins(hang + n, lie + n);
                } else ins(hang + n, j);
            } else {
                if (map[i - 1][j] == 'X' && fl) {
                    ins(i, lie + n);
                } else ins(i, j);
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= hang + n; i++) ans += hungary(cnt = i);
    printf("%d\n", ans);
    return 0; 
}
int main() {
    while (scanf("%d", &n) == 1 && n) solve();
    return 0;
}
------ 本文结束 ------