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上海邀请赛重现)的更多相关文章

  1. HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)

    Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...

  2. HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)

    Seam Carving DescriptionFish likes to take photo with his friends. Several days ago, he found that s ...

  3. HDU5099——Comparison of Android versions(简单题)(2014上海邀请赛重现)

    Comparison of Android versionsProblem DescriptionAs an Android developer, itˇs really not easy to fi ...

  4. Hdu5093 Battle ships 二分图

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

  5. Codeforces 567D:One-Dimensional Battle Ships(二分)

    time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...

  6. Battle ships HDU - 5093二分匹配

    Battle shipsHDU - 5093 题目大意:n*m的地图,*代表海洋,#代表冰山,o代表浮冰,海洋上可以放置船舰,但是每一行每一列只能有一个船舰(类似象棋的車),除非同行或者同列的船舰中间 ...

  7. HDOJ 5093 Battle ships 二分图匹配

    二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...

  8. Battle ships(二分图,建图,好题)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  9. Codeforces 567D One-Dimensional Battle Ships

    传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...

随机推荐

  1. openerp模块收藏 基于Lodop的报表打印模块(转载)

    基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...

  2. 利用ddmlib 实现 PC端与android手机端adb forword socket通信(转)

    上篇文章讲了PC与android手机连接的办法 ,通过java调用系统命令执行adb命令操作,实际上是一个比较笨的办法. 网上查阅资料,发现google 提供了ddmlib库 (adt-bundle\ ...

  3. linux c 实现大数相乘

      #include <stdio.h> #include <string.h> #include <math.h> #include <stdbool.h& ...

  4. MySQL通过Binlog恢复删除的表

    查看log-bin是否开启:mysql> show variables like '%log%bin%';+---------------------------------+-------+| ...

  5. 因修改system密码导致expdp备份失败

    今天发现一套系统的逻辑备份失效了,检查了一下,发现主要是由于之前其他管理员修改了system用户的密码,导致备份不成功了.为了今后此类的问题发生,修改expdp的脚本连接部分如下:expdp \' / ...

  6. Flash制作遇到的小问题1--为何变形需要将图形打散(Ctrl+b)

    今天上Flash实验课遇到一个小问题,就是我在画一个矩形如下图:

  7. How to: Add Missing ContentPlaceHolder

    In Microsoft SharePoint Server 2010, the BlueBand master page is not supported in the Search Center ...

  8. JSP:useBean,setProperty的使用

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  9. ASP.NET Web - 开篇

    ASP.NET运行库 服务器系统上需要ASP.NET运行库.如果系统上有IIS,就会在安装.NET Framework时为服务器配置ASP.NET运行库.开发过程中,不需要IIS,因为VS发布了自己的 ...

  10. iOS 23 种设计模式

    设计模式主要分三个类型:创建型.结构型和行为型. 其中创建型有: 一.Singleton,单例模式:保证一个类只有一个实例,并提供一个访问它的全局访问点 二.Abstract Factory,抽象工厂 ...