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 ...
随机推荐
- Sublime Text博客插件 --- iblog
iblog是一款 sublime 博客插件,目前只支持cnblog. 项目地址:https://github.com/iskeeter/iblog 功能介绍 新建和更新cnblog的博客 支持mark ...
- mac os快捷键
选中一个词,使用control+command+d,可以启用词典 option+command+d,隐藏/显示 doc command + k terminal 清除历史记录 control + up ...
- 在Web API中使用Swagger-UI开源组件(一个深坑的解决)
介绍: Swagger-Ui是一个非常棒的Web API说明帮助页,具体详情可自行Google和百度. 官网:http://swagger.io/ GitHub地址:https://github ...
- perl 脚本测试
原文地址: http://blog.csdn.net/johnny710vip/article/details/8905239 这是一篇关于perl脚本测试的总结性文章,其中提到了很多实用的 ...
- css的display属性小实验
div与span是常用的盒子模型; 区别: div默认是垂直分布(独占一行) span默认是水平分布(一行可以有多个) 通过float属性可以改变div容器的分布方式达到span容器的效果; 下面 ...
- OpenWrt固件刷入后串口终端没有反应的问题
[路由器开发板硬件固件配置] MTK双频:MT7620a + MT7612e 内存:256 MB 闪存:16 MB 固件:MTK自带SDK中的OpenWrt固件(mtksdk-openwrt-2.6. ...
- sharepoint 列表的column验证----------SharePoint 2010 List Validation Formula
首先,依次打开-站点->列表名称->列表设置->验证设置: 我们设置一个时间的列不能小于当前时间,并且在编辑的时候不需要验证. =OR([,Created<TODAY())
- Mysql几种索引类型的区别及适用情况
如大家所知道的,Mysql目前主要有以下几种索引类型:FULLTEXT,HASH,BTREE,RTREE. 那么,这几种索引有什么功能和性能上的不同呢? FULLTEXT 即为全文索引,目前只有MyI ...
- android 弹出框(输入框和选择框)
1.输入框: final EditText inputServer = new EditText(this); inputServer.setFilters(new InputFilter[]{new ...
- SUID ,SGID ,Sticky
SUID passwd:s SUID: 运行某程序时,相应进程的属主是程序文件自身的属主,而不是启动者: chmod u+s FILE chmod u-s FILE 如果FILE本身原来就有执行权限, ...