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 ...
随机推荐
- Spring-IOC-BeanFactory
BeanFactory BeanFactory 是 Spring 的“心脏”.它就是 Spring IoC 容器的真面目.Spring 使用 BeanFactory 来实例化.配置和管理 Bean.但 ...
- Spring JdbcTemplate 方法详解
JdbcTemplate主要提供以下五类方法: execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句: update方法及batchUpdate方法:update方法用于执行新增.修 ...
- Java开发面试
有很多文章说面试相关的问题,有国内也有国外的,但是我相信不少人,特 别是新人看完后还是觉得比较虚比较泛,似乎好像懂了,但是一遇到面试还 是有些手无足措或者重复犯一些错误.本篇文章正是结合实际经 ...
- 深度分析mysql GROUP BY 与 ORDER BY
鉴于项目的需要,就从网上找到该文章,文章分析得很详细也很易懂,在android里,(不知道是不是现在水平的限制,总之我还没找到在用ContentProvider时可以使用子查询),主要方法是用SQLi ...
- 原生JavaScript技巧
时常在技术论坛有看见一些比较好的示例,于是就出于一种收集并学习的态度,于是就保留下来啦~ 当然现在展示的也只是一部分,先放一部分出来尝尝鲜~~~
- HTML5 data-* 属性
HTML5 data-* 属性 jQuery Mobile 依赖 HTML5 data-* 属性来支持各种 UI 元素.过渡和页面结构.不支持它们的浏览器将以静默方式弃用它们.表 2 显示如何使用 d ...
- 号外!GNOME 3.22 正式发布喽!!!
导读 经过半年的努力开发,别名为“卡尔斯鲁厄”的 GNOME 3.22 正式发布了!“GNOME Software 可以安装和更新 Flatpak 软件包,GNOME Builder 则可以创建它们, ...
- (转)dp和dip是同一个单位
原文地址:http://blog.csdn.net/chenyufei1013/article/details/8363619 摘要 本文介绍了android单位dp,dip的概念,并给出了它的确切含 ...
- 获取FIle路径下所有文件的地址和名称
public static void getFileName(File[] files) { String address=""; if (files != null)// 先判断 ...
- Cannot find `aapt.exe`. Please install the Android SDK Build-tools package
Google has updated their SDK tools ("Android SDK Tools" Rev. 23) in a way that also requir ...