HDU 5093 Battle ships(二分图匹配)

12 篇文章 0 订阅

Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 815    Accepted Submission(s): 292

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
 


开始时没思路,后来发现是经典的二分图匹配,好吧,还需要好好理解理解。

后来无限WA,历经千辛万苦,我发现是我精心设计的数组开小了。

我特么到底是怎么算的,才算了一个数,嗯,数组大小肯定不会超过这个数(本着节省内存的原则),

然后,match数组用了memset成-1,感觉,也是一个问题。(-1没有被卡

还有,我特地用了short,因为数据都是50以内,50*50也才2500,也在short范围内,结果,我把所有的short改成int就AC了,这,我无语了

反正,很迷啊,看来被卡的应该就是short了,居然是short。。。short。。。醉


AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int m, n, max_x, max_y;
char map_ice[52][52];
int order_x[52][52], order_y[52][52];
bool graph[2501][2501];
bool vis[2501];
int match[2501];
bool find(int x)
{
	int j;
	for (j = 1; j <= max_y; j++)
	{
		if (graph[x][j] == true && vis[j] == false)
		{
			vis[j] = true;
			if (match[j] == 0 || find(match[j]))
			{
				match[j] = x;
				return true;
			}
		}
	}
	return false;
}
void init()
{
	memset(map_ice, 0, sizeof(char) * 52 * 52);
	memset(order_x, 0, sizeof(int) * 52 * 52);
	memset(order_y, 0, sizeof(int) * 52 * 52);
	memset(graph, 0, sizeof(bool) * 2501 * 2501);
	memset(vis, 0, sizeof(bool) * 2501);
	memset(match, 0, sizeof(int) * 2501);
	scanf("%d %d", &m, &n);
	for (int i = 0; i < m; i++)
	{
		scanf("%s", map_ice[i]);
	}
	for (int i = 0, t = 0; i < m; i++)
	{
		bool flag = false;
		for (int j = 0; j < n; j++)
		{
			if (map_ice[i][j] == '#')
			{
				if (flag)
				{
					order_x[i][j] = 0;
					flag = false;
				}
			}
			else if (map_ice[i][j] == '*')
			{
				if (flag)
				{
					order_x[i][j] = t;
				}
				else
				{
					flag = true;
					t++;
					order_x[i][j] = t;
				}
			}
			else if (map_ice[i][j] == 'o')
			{
				order_x[i][j] = 0;
			}
		}
		if (i == m - 1)
			max_x = t;
	}
	for (int i = 0, t = 0; i < n; i++)
	{
		bool flag = false;
		for (int j = 0; j < m; j++)
		{
			if (map_ice[j][i] == '#')
			{
				if (flag)
				{
					order_y[j][i] = 0;
					flag = false;
				}

			}
			else if (map_ice[j][i] == '*')
			{
				if (flag)
				{
					order_y[j][i] = t;
				}
				else
				{
					flag = true;
					t++;
					order_y[j][i] = t;
				}
			}
			else if (map_ice[j][i] == 'o')
			{
				order_y[j][i] = 0;
			}
		}
		if (i == n - 1)
			max_y = t;
	}
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			graph[order_x[i][j]][order_y[i][j]] = true;
		}
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int ans = 0;
		init();
		for (int i = 1; i <= max_x; i++)
		{
			memset(vis, 0, sizeof(bool) * 2501);
			if (find(i)) ans += 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值