Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1061    Accepted Submission(s): 377

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
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)



题意:给你一张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(二分图经典建图)的更多相关文章

  1. HDOJ 5093 Battle ships 二分图匹配

    二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...

  2. Battle ships(二分图,建图,好题)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  3. hdoj 5093 Battle ships 【二分图最大匹配】

    题目:pid=5093" target="_blank">hdoj 5093 Battle ships 题意:给你一个n*m的图,图中有冰山 '# ',浮冰 'o' ...

  4. HDU 5093 Battle ships(二分图最大匹配)

    题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...

  5. poj--1149--PIGS(最大流经典建图)

    PIGS Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  6. hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)

    Problem Description Thanks to a certain "green" resources company, there is a new profitab ...

  7. POJ 2226 最小点覆盖(经典建图)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8881   Accepted: 3300 Desc ...

  8. 2-sat——poj3678经典建图

    比较经典的建图,详见进阶指南 2-sat一般要用到tarjan来求强连通分量 /*2-sat要加的是具有强制关系的边*/ #include<iostream> #include<cs ...

  9. POJ1149 最大流经典建图PIG

    题意:       有一个人,他有m个猪圈,每个猪圈里都有一定数量的猪,但是他没有钥匙,然后依次来了n个顾客,每个顾客都有一些钥匙,还有他要卖猪的数量,每个顾客来的时候主人用顾客的钥匙打开相应的门,可 ...

随机推荐

  1. TCP/IP,必知必会的

    文章目录 前言 TCP/IP模型 数据链路层 网络层 ping Traceroute TCP/UDP DNS TCP连接的建立与终止 TCP流量控制 TCP拥塞控制 0 前言 本文整理了一些TCP/I ...

  2. poj3009 Curling 2.0 深搜

    PS:以前看到题目这么长就没写下去了.今天做了半天,没做出来.准备看题解,打开了网站都忍住了,最后还是靠自己做出来的.算是一点进步吧. 分析: 题目的意思没明白或者理解有偏差都没办法做题.看样例3和样 ...

  3. Android 概览屏幕

    文章照搬过来的:原文地址https://developer.android.google.cn/guide/components/recents.html 概览屏幕(也称为最新动态屏幕.最近任务列表或 ...

  4. 百鸡百钱===百马百担====for循环嵌套

    package com.zuoye.test;//百鸡百钱5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡.public class Baiji { public static voi ...

  5. sql server 存储过程(事务,带参数声明,数据库瘦身)

    CREATE PROCEDURE procedureName (@var1 as varchar(50),@var2 as varchar(50)) --建立未发临时表 AS begin tran - ...

  6. CNN结构:SPP-Net为CNNs添加空间尺度卷积-神经元层

    前几个CNN检测的框架要求网络的图像输入为固定长宽,而SPP-Net在CNN结构中添加了一个实现图像金字塔功能的卷积层SPP层,用于在网络中实现多尺度卷积,由此对应多尺度输入,以此应对图像的缩放变换和 ...

  7. React Native Windows下环境安装(一)

    1.安装chocolatey 以管理员权限运行命令提示符(cmd.exe) @powershell -NoProfile -ExecutionPolicy Bypass -Command " ...

  8. 将 GNOME 默认的界面切换动画功能关闭

    gsettings set org.gnome.desktop.interface enable-animations false

  9. Object.assign和序列/反序列

    Object.assign let testObj = { a:[1,2,4], b:{ name:'ls', school:['huf','yelu'], parent:{ father:'lili ...

  10. 在vue项目中引用element-ui时 让el-input 获取焦点的方法

    在制作项目的时候遇到一个需求,点击一个按钮弹出一个input输入框,并让输入框获得焦点,项目中引用了ElementUI 在网上查找了很多方法,但是在实际使用中发现了一个问题无论是使用$ref获取inp ...