Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1091    Accepted Submission(s): 395

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
 
题意:给出一个地图,*为海水区,o为浮冰区,#为冰山   要在海水区放船,除非被冰山阻挡,否则每行或每列只能放一艘,问最多能放多少
思路:如果不管冰山阻挡的约数条件,每一行或每一列只能被选择一次,就是显然的二分图行列匹配。有了约数条件,其实就是把每一行或每一列根据#来分段,再匹配就行了
20160407群赛-补
#include <bits/stdc++.h>
using namespace std;
const int N = ;
char Mat[N][N];
int n, m;
int cnt1, cnt2;
int x[N][N], y[N][N], g[N * N][N * N];
void init() {
cnt1 = , cnt2 = ;
for(int i = ; i < n; ++i) {
int p = , j, f = ;
while(p < m) {
f = ;
for(j = p; j < m; ++j) {
if(Mat[i][j] == '*') { f = ; x[i][j] = cnt1; }
else if(Mat[i][j] == '#') break;
}
if(f)
++cnt1;
p = j;
++p;
}
}
for(int i = ; i < m; ++i) {
int p = , j, f = ;
while(p < n) {
f = ;
for(j = p; j < n; ++j) {
if(Mat[j][i] == '*') { f = ; y[j][i] = cnt2; }
else if(Mat[j][i] == '#') break;
}
if(f)
++cnt2;
p = j;
++p;
}
}
}
void look(int a[][]) {
for(int i = ; i < n; ++i) {
for(int j = ; j < m; ++j) printf("%d ", a[i][j]);
puts("");
}
}
void get() {
memset(g, , sizeof g);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j)
if(Mat[i][j] == '*')
g[ x[i][j] ][ y[i][j] ] = ;
}
int linker[N * N];
bool used[N * N];
bool dfs(int u) {
for(int v = ; v < cnt2; ++v)
if(g[u][v] && !used[v]) {
used[v] = true;
if(linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
return false;
}
int hungary() {
int res = ;
memset(linker, -, sizeof linker);
for(int u = ; u < cnt1; ++u)
{
memset(used, false, sizeof used);
if(dfs(u)) res++;
}
return res;
}
int main() {
int _; scanf("%d", &_);
while(_ --) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i) scanf("%s", Mat[i]);
init();
get();
// look(x);
//look(y);
printf("%d\n", hungary());
}
return ;
}

Hdu5093 Battle ships 二分图的更多相关文章

  1. HDOJ 5093 Battle ships 二分图匹配

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

  2. HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)

    Battle ships Problem DescriptionDear contestant, now you are an excellent navy commander, who is res ...

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

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

  4. HDU 5093 Battle ships(二分图最大匹配)

    题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...

  5. hdoj 5093 Battle ships 【二分图最大匹配】

    题目:pid=5093" target="_blank">hdoj 5093 Battle ships 题意:给你一个n*m的图,图中有冰山 '# ',浮冰 'o' ...

  6. Codeforces 567D One-Dimensional Battle Ships

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

  7. Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞

    D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

  8. Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set区间分解

    D. One-Dimensional Battle ShipsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...

  9. [ZOJ 3623] Battle Ships

    Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is s ...

随机推荐

  1. iOS - 在工程中试玩状态模式

    做了一个项目,项目中一个藏品详情界面针对不同用户,和用户所处于的状态的不同,展示的效果和操作的权限都会不同.想到了状态模式,从来没有用过,赶紧学一下然后用一用.期待兴奋 看了这么多的博客,终于找到一个 ...

  2. Dell服务器安装OpenManage(OMSA)

    公司上架了一批戴尔服务器,公司要求对这些服务器的硬件做一系列的监控,如CPU的温度,内存,风扇的状态,转速,磁盘等硬件的监控. 在对服务器的硬件监控上,目前业界主要基于如下两种: 1.服务器自带的工具 ...

  3. 浅谈MVC中路由

    引言 学习ASP.NET MVC 路由这一关是肯定必不可少的.这一节,我们就来简单介绍下MVC中的路由机制.简单的路由机制相信大家都已了解,这一节主要介绍路由中很少使用的部分. 使用静态URL片段 在 ...

  4. 在Activity之间传递参数(一)

    准备: 一.创建主界面:activity_main.xml文件中<Button android:text="启动另一个Activity" android:id="@ ...

  5. Ubuntu14.04 lamp环境 php 无法加载mcrypt扩展

    Ubuntu14.04中安装后的LAMP环境(http://www.cnblogs.com/daiyu/p/4380657.html)中没有加载:mcrypt扩展,后期再laravel5使用中发现报错 ...

  6. POJ 2478 Farey Sequence

     名字是法雷数列其实是欧拉phi函数              Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. cookie中文乱码

    在学习当中碰到cookie中文乱码问题,问题原因:cookie对中文不太支持,将中文放入cookie中会报错误. 解决办法: 1.编码 将中文进行编码再放入cookie中: String userna ...

  8. 安装切换openjdk

    安装各种版本openjdk sudo apt-get install openjdk-6-jdk sudo apt-get install openjdk-7-jdk sudo apt-get ins ...

  9. 开源实时日志分析ELK平台部署

    参考帖子: (1)自动化测试Web服务器性能autobench+httperf

  10. quartz集群报错but has failed to stop it. This is very likely to create a memory leak.

    quartz集群报错but has failed to stop it. This is very likely to create a memory leak. 在一台配置1核2G内存的阿里云服务器 ...