hdu 5093 Battle ships(最大流 || 二分图)

本文介绍了一个基于战舰布局的问题,通过二分图最大匹配和最大流算法解决战舰在地图上的最优放置方案。地图包含战舰、浮冰和冰山,需确保战舰不位于同一行或列,除非其间有冰山隔开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                            hdu 5093 Battle ships



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
 



题目大意:给你一张地图,上面有三种标记:战舰 *,浮冰 o,冰山 #。战舰不能放在同一行或者同一列,除非有冰山相隔。

解题思路:1)二分图最大匹配:


*ooo                       行集合                1ooo                   列集合       1ooo

o###                                                 o###                                     o###

**#*                                                   22#3                                     12#3

ooo*                                                  ooo4                                     ooo3

根据原图得出行集合和列集合,然后相同位置的 * 点进行匹配建图。建完图以后,就是匈牙利算法的模板。

                 2)最大流

先把第一行和第一列用#填充,然后遍历一遍把所有的 # 拆成两个点a和b设置一个超级源点连向所有的a,设置一个超级汇点,使所有的b连向它。拆完点之后遍历找 * ,当找到一个*之后,算出他的左边第一个#A, 上边第一个#B,然后使A的a点连向B的b点,边的容量都为1。建完图后直接求最大流。




二分图最大匹配

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int PO = 55;
const int N = 1005;
typedef long long ll;
int n, m, ans, cr, cc;
char map[PO][PO];
int row[PO][PO], col[PO][PO];
int G[N][N], vis[N], rec[N];
void init() {
	ans = 0;
	cr = cc = 0;
	memset(map, 0, sizeof(map));
	memset(row, 0, sizeof(row));
	memset(col, 0, sizeof(col));
	memset(G, 0, sizeof(G));
	memset(rec, 0, sizeof(rec));
}
int find(int x) {
	for (int i = 1; i < cc; i++) {
		if (G[x][i] && !vis[i]) {
			vis[i] = 1;	
			if (!rec[i] || find(rec[i])) {
				rec[i] = x;
				return 1;
			}
		}
	}
	return 0;
}
void hungary() {
	for (int i = 1; i < cr; i++) {
		memset(vis, 0, sizeof(vis));	
		if (find(i)) ans++;
	}
}
void input() {
	scanf("%d %d\n", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%s", map[i]);	
	}
	cr = 1;
	for (int i = 0; i < n; i++) {
		int flag = 0;
		for (int j = 0; j < m; j++)	{
			if (map[i][j] == '*') row[i][j] = cr, flag = 1;
			if (map[i][j] == '#') cr++, flag = 0;
		}
		if (flag) cr++;
	}
	cc = 1;
	for (int i = 0; i < m; i++) {
		int flag = 0;
		for (int j = 0; j < n; j++)	{
			if (map[j][i] == '*') col[j][i] = cc, flag = 1;
			if (map[j][i] == '#') cc++, flag = 0;
		}
		if (flag) cc++;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (map[i][j] == '*') G[row[i][j]][col[i][j]] = 1;
		}	
	}
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		init();
		input();	
		hungary();
		printf("%d\n", ans);
	}
	return 0;
}


最大流

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int PO = 60;
const int N = 20005;
const int M = 30005;
const int INF = 0x3f3f3f3f;
int n, m, s, t, OF;
char map[PO][PO];
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M];

void init() {
    ec = 0;
    memset(first, -1, sizeof(first));
	memset(map, 0, sizeof(map));
}

void addEdge(int a,int b,int c) {
    to[ec] = b;
    v[ec] = c;
    Next[ec] = first[a];
    first[a] = ec++;

    to[ec] = a;
    v[ec] = 0;
    Next[ec] = first[b];
    first[b] = ec++;
}

int BFS() {
    int kid, now, f = 0, r = 1, i;
    memset(lev, 0, sizeof(lev));
    que[0] = s, lev[s] = 1;
    while (f < r) {
        now = que[f++];
        for (i = first[now]; i != -1; i = Next[i]) {
            kid = to[i];    
            if (!lev[kid] && v[i]) {
                lev[kid] = lev[now] + 1;    
                if (kid == t) return 1;
                que[r++] = kid;
            }
        }
    }
    return 0;
}

int DFS(int now, int sum) {
    int kid, flow, rt = 0;
    if (now == t) return sum;
    for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
        head[now] = i;  
        kid = to[i];
        if (lev[kid] == lev[now] + 1 && v[i]) {
            flow = DFS(kid, min(sum - rt, v[i]));
            if (flow) {
                v[i] -= flow;
                v[i^1] += flow;
                rt += flow;
            } else lev[kid] = -1;   
        }           
    }
    return rt;
}

int dinic() {
    int ans = 0;
    while (BFS()) {
        for (int i = 0; i <= t; i++) {
            head[i] = first[i];
        }           
        ans += DFS(s, INF);
    }
    return ans;
}   
int findUp(int x, int y) {
	for (int i = x - 1; i >= 0; i--) {
		if (map[i][y] == '#') return i * (m + 1) + y + 1;	
	}
}
int findLeft(int x, int y) {
	for (int i = y - 1; i >= 0; i--) {
		if (map[x][i] == '#') return x * (m + 1) + i + 1;	
	}
}

void input() {
	scanf("%d %d\n", &n, &m);
	OF = (n + 1) * (m + 1);
	s = 0, t = OF * 2 + 1;
	for (int i = 1; i <= n; i++) {
		scanf("%s", map[i] + 1);
	}
	for (int i = 0; i <= m; i++) {
		map[0][i] = '#';
	}
	for (int i = 0; i <= n; i++) {
		map[i][0] = '#';	
	}
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= m; j++) {
			if (map[i][j] == '#') {
				int id = i * (m + 1) + j + 1;
				addEdge(s, id, 1);
				addEdge(id + OF, t, 1);
			}		
		}
	}
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= m; j++) {
			if (map[i][j] == '*') {
				int id1 = findUp(i, j);	
				int id2 = findLeft(i, j);
				addEdge(id1, id2 + OF, 1);
			}
		}	
	}
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		init();
		input();	
		printf("%d\n", dinic());
	}	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值