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

题目大意:

给出n,m。给出一个地图,图中有海(*),山(#),冰(o),船可以放到海里,但不能放到冰和山上。船会互相攻击,所以同一列的船必须有山相间隔。问最多放多少船。

思路:

二分图建图之行列建图法----一行变多行,一列变多列。

代码:

 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>

using namespace std;
#define ll long long
const int mod = 1e9 + 7;
const int maxn = 550;
const int maxe = 90050;
bool inpath[maxe];
int match[maxe];
int head[maxe];int edgeNum = 0;
struct Edge {
	int to, next;
}edge[maxe];
int cntx, cnty;
void addEdge(int a, int b) {
	edge[edgeNum].to = b;
	edge[edgeNum].next = head[a];
	head[a] = edgeNum++;
}

bool findpath(int k) {
	for (int i = head[k];i != -1;i = edge[i].next) {
		int j = edge[i].to;
		if (!inpath[j]) {
			inpath[j] = true; 
			if (match[j] == -1 || findpath(match[j])) {
				match[j] = k;return true;
			}
		}
	}
	return false;
}

void hungary() {
	int cnt = 0;
	for (int i = 1;i <= cntx;i++) {
		memset(inpath, 0, sizeof(inpath));
		if (findpath(i)) {
			cnt++;
		}
	}
	cout << cnt << endl;
}
void init() {
	memset(inpath, false, sizeof(inpath));
	memset(match, -1, sizeof(match));
	memset(head, -1, sizeof(head));
	edgeNum = 0;
}

int n, m;


char Map[maxn][maxn];
int x[maxn][maxn], y[maxn][maxn];

void getx()
{
	cntx = 1;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			if (Map[i][j] == '#')cntx++;
			x[i][j] = cntx;
		}
		cntx++;
	}
}

void gety()
{
	cnty = 1;
	for (int i = 1;i <= m;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			if (Map[j][i] == '#')cnty++;
			y[j][i] = cnty;
		}
		cnty++;
	}
}

void getMap()
{

	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			if (Map[i][j] == '*')addEdge(x[i][j], y[i][j]);
		}
	}

}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		init();

		scanf("%d%d", &n, &m);
		for (int i = 1;i <= n;i++)
		{
			scanf("%s", Map[i] + 1);
		}

		getx();
		gety();
		getMap();
		hungary();
	}
	return 0;
}

 

### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是论最短路等相关知识点。如果是后者的话那么就需要构相应的邻接表表示形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值