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. Brackets前端开发IDE工具

    Brackets是一个开源的前端开发IDE工具,网页设计师和前端开发人员必备的前端开发IDE工具. 它能够使你在开发WEB网站实时预览你的网页,目前版本只适用于Chrome浏览器可以实时预览效果 支持 ...

  2. <<< commons-fileupload 和 ajaxfileupload 实现局部上传

    最近弄了一个上传,要求实现页面的局部刷新,Java的上传组件大多还是用的 commons-fileupload,网上搜索了好多的教程,太麻烦了,看到了ajaxfileupload这个插件,不错,实现简 ...

  3. [UML]UML系列——类图class的实现关系Realization

    系列文章 [UML]UML系列——用例图Use Case       [UML]UML系列——用例图中的各种关系(include.extend)       [UML]UML系列——类图Class   ...

  4. PHP 表单

    PHP 中的 $_GET 和 $_POST 变量用于检索表单中的信息,比如用户输入. 表单验证: 应该在任何可能的时候对用户输入进行验证(通过客户端脚本).浏览器验证速度更快,并且可以减轻服务器的负载 ...

  5. tyvj1097 mm不哭

    背景 Bless all rp++.. 描述 在一个数轴上,有n个MM(绝非恐龙!)在哭泣(5555~一直哭). tcboy也在这个数轴上,并恰好看到了这一幕,由于每个MM哭都会让tcboy损失一定的 ...

  6. [SQL] SQL学习笔记之基础操作

    1 SQL介绍 SQL 是用于访问和处理数据库的标准的计算机语言.关于SQL的具体介绍,我们通过回答如下三个问题来进行. SQL 是什么? SQL,指结构化查询语言,全称是 Structured Qu ...

  7. 配置SQL server远程连接(局域网)

    具体步骤: 1) 2) 3) 4) 5) 6) 7) 最后为了防火墙有影响,直接把防火关了,测试连接通过在来设置防火墙.

  8. java基础知识(二)字符串处理

    字符串是程序开发中使用最为频繁,因此为了工作的高效和作为一名想进阶的程序员,了解并掌握字符串的处理显得尤为重要.java为我们提供了String.StringBuffer.StringBuilde三个 ...

  9. [BZOJ2391]Cirno的忧郁

    [BZOJ2391]Cirno的忧郁 试题描述 Cirno闲着无事的时候喜欢冰冻青蛙. Cirno每次从雾之湖中固定的n个结点中选出一些点构成一个简单多边形,Cirno运用自己的能力能将此多边形内所有 ...

  10. angularjs表单验证checkbox

    angularjs中默认有表单验证的支持,见文末的refer 我想要验证至少要选择一个checkbox,否则就不能提交 但是checkbox貌似没有简单的方法,想来想去给出下面的解决方案 valida ...