HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)
Battle ships
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
题目大意:
给出一个n行m列的图,*代表海域,o代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互看到,之间只要有山就不能看到,问最多能放多少船。
解题思路:
比赛的时候没有想出来,看了别人的博客也是花了很长时间才想明白为什么是二分匹配(在这里谢谢这些大牛,感谢你们的blog,要不我现在还不会呢。。。)。
姑且将一片最多只能放一个船的连续网格叫做‘块’。
以样例一为例
首先只考虑行,将每个块标号:将答案存入num1
1000
0000
2203
0004
再此只考虑列,将每个块标号:将答案存入num2
1000
0000
1203
0003
那么对于任意一个可以放船的二维坐标A(i,j),假设其放船,那么num1与num2中与A(i,j)对应num相同的坐标都不能再放船。相当于A所在的行块和列块实现了一一映射关系。
所以将行块看做一个集合,列块看做一个集合。
所求的最大放船数就是两个集合之间的最大匹配数。
匈牙利模版解决问题。
Code:
/*************************************************************************
> File Name: shanghai_1004.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月04日 星期二 01时28分18秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 100000
using namespace std;
bool edge[][];
char map[][];
int num1[][],num2[][],link[];
bool vis[];
int k1,k2,cnt;
bool dfs(int x)
{
for (int y=; y<k2; y++)
if (edge[x][y]&&!vis[y])
{
vis[y]=;
if (link[y]==||dfs(link[y]))
{
link[y]=x;
return true;
}
}
return false;
}
void search()
{
//cout<<"1"<<endl;
memset(link,,sizeof(link));
for (int x=; x<k1; x++)
{
memset(vis,,sizeof(vis));
if (dfs(x))
cnt++;
}
}
int main()
{
int T;
cin>>T;
while (T--)
{
int N,M;
cin>>M>>N;
//cout<<M<<N;
memset(edge,,sizeof(edge));
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
for (int i=; i<=M; i++)
for (int j=; j<=N; j++)
cin>>map[i][j];
k1=k2=;
for (int i=; i<=M; i++)
{
for (int j=; j<=N; j++)
{
if (map[i][j]=='#') k1++;
if (map[i][j]=='*') num1[i][j]=k1;
}
k1++; //注意k累加的位置,放在for循环之后保证最后的块
} //的编号为k-1
for (int i=; i<=N; i++)
{
for (int j=; j<=M; j++)
{
if (map[j][i]=='#') k2++;
if (map[j][i]=='*') num2[j][i]=k2;
}
k2++;
}
for (int i=; i<=M; i++)
for (int j=; j<=N; j++)
edge[num1[i][j]][num2[i][j]]=;
cnt=;
search();
cout<<cnt<<endl;
}
return ;
}
HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)的更多相关文章
- HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)
Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...
- HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)
Seam Carving DescriptionFish likes to take photo with his friends. Several days ago, he found that s ...
- HDU5099——Comparison of Android versions(简单题)(2014上海邀请赛重现)
Comparison of Android versionsProblem DescriptionAs an Android developer, itˇs really not easy to fi ...
- Hdu5093 Battle ships 二分图
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...
- Codeforces 567D:One-Dimensional Battle Ships(二分)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...
- Battle ships HDU - 5093二分匹配
Battle shipsHDU - 5093 题目大意:n*m的地图,*代表海洋,#代表冰山,o代表浮冰,海洋上可以放置船舰,但是每一行每一列只能有一个船舰(类似象棋的車),除非同行或者同列的船舰中间 ...
- HDOJ 5093 Battle ships 二分图匹配
二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Codeforces 567D One-Dimensional Battle Ships
传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...
随机推荐
- 【转】你需要知道的Python用法
在使用Python多年以后,我偶然发现了一些我们过去不知道的功能和特性.一些可以说是非常有用,但却没有充分利用.考虑到这一点,我编辑了一些的你应该了解的Pyghon功能特色. 带任意数量参数的函数 你 ...
- 自改xss小平台上线
原先一直用xss.hk结果不知怎么被关的,正好手上有代码于是自己搭了一个,网上的类似的xss平台大多一样,原先的xss的chrome插件,不适合 "manifest_version" ...
- 【转载】Powershell连接世纪互联Office365
$User = "admin@contoso.com" $PWord = ConvertTo-SecureString –String "password" – ...
- 键盘样式风格有关设置-iOS开发
一.键盘风格 UIKit框架支持8种风格键盘. typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKey ...
- 人工智能起步-反向回馈神经网路算法(BP算法)
人工智能分为强人工,弱人工. 弱人工智能就包括我们常用的语音识别,图像识别等,或者为了某一个固定目标实现的人工算法,如:下围棋,游戏的AI,聊天机器人,阿尔法狗等. 强人工智能目前只是一个幻想,就是自 ...
- SlimDx绘制点图元的问题
问题:点图元在自己创建的三维环境里渲染不出来,代码如下: GMCustomVertex.PositionNormalColored wellPart = new GMCustomVertex.Posi ...
- nginx 如何显示真实ip
nginx做反向代理显示在后台访问的真实ip总是显示127.0.0.1 只要添加如下内容: proxy_set_header Host $host; proxy_set_header X-For ...
- 【BZOJ】【2820】YY的GCD
莫比乌斯反演 PoPoQQQ讲义第二题. 暴力枚举每个质数,然后去更新它的倍数即可,那个g[x]看不懂就算了…… 为什么去掉了一个memset就不T了→_→…… /****************** ...
- setDepthStencilState
cgfx->hlsl StencilFunc={always,1,0xff}->setDepthStencilState(DepthState,0) StencilFunc={always ...
- PHP开发框架[流行度排名]
在PHP开发中,选择合适的框架有助于加快软件开发,节约宝贵的项目时间,让开发者专注于功能的实现上.Sitepoint网站做了一个小的调查,结果显示最流行的PHP框架前三甲为:Laravel.Phalc ...