hdoj--5093--Battle ships(二分图经典建图)
Battle ships
Total Submission(s): 1061 Accepted Submission(s): 377
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.
2
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
3
5
题意:给你一张n*m的图,图中*表示海洋空地,o表示浮冰,#表示冰山,现在你作为指挥官,安排军舰,军舰不可以放在浮冰或者冰山上,并且两艘军舰不可以在同行或者同列,除非中间有冰山阻挡,输出最多安排多少军舰
二分图的一个经典建图,将同行的*合并,遇到#换行,并且不同行的*不可以在一起,建出新图G,跑一边匈牙利算法就行,这个题跟Fire net差不多
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
}rec[100][100];
int used[10010],pipei[10010];
vector<int>G[10010];
int n,m,row,cur;
char map[100][100];
void getmap()
{
cur=row=0;
bool flag=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='*')
rec[i][j].x=row,flag=true;
if(map[i][j]=='#')
row++,flag=false;
}
if(flag)
row++;
}
flag=false;
for(int j=0;j<m;j++)
{
for(int i=0;i<n;i++)
{
if(map[i][j]=='*')
rec[i][j].y=cur,flag=true;
if(map[i][j]=='#')
cur++,flag=false;
}
if(flag)
cur++;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='*')
{
int x=rec[i][j].x;
int y=rec[i][j].y;
G[x].push_back(y);
}
}
}
}
int find(int x)
{
for(int i=0;i<G[x].size();i++)
{
int y=G[x][i];
if(!used[y])
{
used[y]=1;
if(pipei[y]==-1||find(pipei[y]))
{
pipei[y]=x;
return 1;
}
}
}
return 0;
}
void solve()
{
int sum=0;
memset(pipei,-1,sizeof(pipei));
for(int i=0;i<row;i++)
{
memset(used,0,sizeof(used));
sum+=find(i);
}
printf("%d\n",sum);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",map[i]);
for(int i=0;i<1000;i++)
G[i].clear();
getmap();
solve();
}
return 0;
}
hdoj--5093--Battle ships(二分图经典建图)的更多相关文章
- HDOJ 5093 Battle ships 二分图匹配
二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- hdoj 5093 Battle ships 【二分图最大匹配】
题目:pid=5093" target="_blank">hdoj 5093 Battle ships 题意:给你一个n*m的图,图中有冰山 '# ',浮冰 'o' ...
- HDU 5093 Battle ships(二分图最大匹配)
题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...
- poj--1149--PIGS(最大流经典建图)
PIGS Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)
Problem Description Thanks to a certain "green" resources company, there is a new profitab ...
- POJ 2226 最小点覆盖(经典建图)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8881 Accepted: 3300 Desc ...
- 2-sat——poj3678经典建图
比较经典的建图,详见进阶指南 2-sat一般要用到tarjan来求强连通分量 /*2-sat要加的是具有强制关系的边*/ #include<iostream> #include<cs ...
- POJ1149 最大流经典建图PIG
题意: 有一个人,他有m个猪圈,每个猪圈里都有一定数量的猪,但是他没有钥匙,然后依次来了n个顾客,每个顾客都有一些钥匙,还有他要卖猪的数量,每个顾客来的时候主人用顾客的钥匙打开相应的门,可 ...
随机推荐
- 5.30dao-service-controller层,mybatis自动生成。(获取根据id主键获取指定详细数据)
获取权限详细数据:(参考) 1.controller:1.注入Servcie调用方法findConsumerById(参数是id); ...
- 存档:Telerik Test Studio的摸索笔记
http://www.51testing.com/?uid-170604-action-spacelist-starttime-1328025600-endtime-1330531200 http:/ ...
- GrepWin:Win7下的文本替换工具
工作环境退回到Win7之后,内容查找功能非常不给力,推荐一个文本内容查找工具grepWin. Win7下的文本查找/替换工具: grepWin
- Redis 之string结构及命令详解
1.set key value ex 秒数 px 毫秒数 nx / xx 设置一个值,注:ex 与 px 同时存在时,取写在后面的一个为有效期,nx表示key不存时设置成功,xx表示 ...
- Bootstrap 模态框(Modal)带参数传值实例
模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. 为了实现父窗体与其的交互,通常需要向其传值,实现 ...
- MySql数据库多表操作
一.连接查询[连表查询.多表查询] 当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回 mysql支持三种类型的连接查询,分别为: 内连接查询(inner join) ...
- matlab学习GUI可调的界面窗口
创建一个GUI界面,在此依然利用GUI_01的窗口来演示 发现它的最大化窗口不可调 在GUI绘制中,工具--->选择GUI选项---->选择第二个成比例 再运行就可以调控大小了
- Webpack 快速上手(下)
杏仁前端开发工程师,代码洁癖症早期,关注前端技术. 由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件 ...
- PAT_A1115#Counting Nodes in a BST
Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursiv ...
- MySQL创建临时表
drop TEMPORARY table if EXISTS temp_table; create TEMPORARY table temp_table( id int not null, usern ...