Hdu5093 Battle ships 二分图
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1091 Accepted Submission(s): 395
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.
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.
#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 二分图的更多相关文章
- HDOJ 5093 Battle ships 二分图匹配
二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...
- HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)
Battle ships Problem DescriptionDear contestant, now you are an excellent navy commander, who is res ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- HDU 5093 Battle ships(二分图最大匹配)
题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...
- hdoj 5093 Battle ships 【二分图最大匹配】
题目:pid=5093" target="_blank">hdoj 5093 Battle ships 题意:给你一个n*m的图,图中有冰山 '# ',浮冰 'o' ...
- Codeforces 567D One-Dimensional Battle Ships
传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...
- 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 ...
- 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 ...
- [ZOJ 3623] Battle Ships
Battle Ships Time Limit: 2 Seconds Memory Limit: 65536 KB Battle Ships is a new game which is s ...
随机推荐
- 如何实现CDN的ns智能解析和动手验证Akamai的实现
1.什么是ns智能解析 通常CDN业务中,智能解析域名,是根据请求方ip的不同给出不同的A记录. 而ns智能解析,是根据请求方ip的不同让他去不同的ns上解析域名,把ns推向离用户更近的边缘节点来缩短 ...
- PDFobject插件使用,PDF在线查看插件
1.引入插件JS <script type="text/javascript" src="PDFobject.js"></script> ...
- Kafka实战-Flume到Kafka
1.概述 前面给大家介绍了整个Kafka项目的开发流程,今天给大家分享Kafka如何获取数据源,即Kafka生产数据.下面是今天要分享的目录: 数据来源 Flume到Kafka 数据源加载 预览 下面 ...
- [Web开发] 在HTML代码里面如何判断IE版本
在上一篇blog里面提到IE有不同的显示模式以及如何用Javascript 来动态判定. Web开发者可以根据不同显示模式导入不同的内容.这篇blog 主要讲如何让静态HTML代码根据不同IE版本显示 ...
- session生命周期(一)
Session存储在服务器端,一般为了防止在服务器的内存中(为了高速存取),Session在用户访问第一次访问服务器时创建,需要注意只有访问JSP.Servlet等程序时才会创建Session,只访问 ...
- JDI tutorial (trace example)
Components Debugger Interfaces / |--------------| / | VM | debuggee ----( |--------------| <----- ...
- Android通过名称找图片
开发中往往会遇到这种情况:在Drawable文件夹中有若干张相似的图片(这里的相似指的是不仅图片名称相似,用途也相似),现在要根据用户的某个操作选出其中的一张.例如,在类似微信语音功能的开发中,按住“ ...
- 自定义PHP系统异常处理类
<?php // 自定义异常函数 set_exception_handler('handle_exception'); // 自定义错误函数 set_error_handler('handle_ ...
- Vue in 2016
原文链接:Vue in 2016 Vue 作者尤雨溪对 Vue 在 2016 年的总结以及未来的展望 现在已经是2016的尾声了!在这过去的12个月里,Vue的持续增长速度已经超过了我的预期--这个项 ...
- PHP 动态生成验证码
……机器人会在网站中搜寻允许他们插入广告的输入表单,在虚拟世界没有什么能阻挡它们胡作非为.这些机器人效率极高,完全不关心所攻击的表单的本来用途.它们唯一的目标就是用它们的垃圾广告覆盖你的内容,残忍地为 ...