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. Linux命令:查看登录用户

    查看所有用户: # w 查看当前用户: # who am i 结束用户进程:# pkill -9 -t pts/0

  2. XSS攻击测试代码

    '><script>alert(document.cookie)</script>='><script>alert(document.cookie)&l ...

  3. mysql关联表的复制

    1. 复制被参照的表: CREATE TABLE clone_product_1 LIKE product_1; INSERT INTO clone_product_1 SELECT * FROM p ...

  4. boost之lexical_cast

    第一次翻译,虽然是个很简单的函数介绍... 文件boost/lexical_cast.hpp中定义了此函数: namespace boost { class bad_lexical_cast; tem ...

  5. c++11 中成员变量初始化的顺序

    参考C++11FAQ https://www.chenlq.net/cpp11-faq-chs 11以后可以直接在类里面初始化成员变量,类似这样 class A { int a=1; const in ...

  6. Twemproxy 缓存代理服务器

    Twemproxy 缓存代理服务器 Twemproxy 概述 Twemproxy(又称为nutcracker)是一个轻量级的Redis和Memcached代理,主要用来减少对后端缓存服务器的连接数.T ...

  7. ueditor 百度编辑器,取消或自定义右键菜单

    如图:有2种自定义方法,一种是改源码,一种是初始化 初始化,如下代码: var ue = UE.getEditor('XXXid',{ // contextMenu:[ {label:'', cmdN ...

  8. Coding编译连接过程中遇到的问题及解决方法(iOS)

    Coding 上下载地址:https://coding.net/u/coding/p/Coding-iOS/git Github源码下载地址:https://github.com/Coding/Cod ...

  9. Json.Net 数据解析

    参考资料: 随笔分类 - Json.Net系列

  10. Mongo DB 2.6 需要知道的一些自身限定

    在现实的世界中,任何事情都有两面性,在程序的世界中,亦然! 我们不论是在使用一门新的语言,还是一门新的技术,在了解它有多么的让人兴奋,让人轻松,多么的优秀之余,还是很有必要了解一些他的局限性,方便你在 ...