Battle ships

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

Total Submission(s): 39    Accepted Submission(s): 19
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

题意:给出一个n行m列的图,*代表海域,o代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互看到,之间只要有山就不能看到,问最多能放多少船。
分析:对于样例:
3 5
*##**

#****

*#**#

建图的时候按照行和列建立二部图,首先统计每行和每列的*联通个数

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"map"
#define inf 0x3f3f3f3f
#define M 3009
using namespace std;
char mp[55][55];
int row[55],col[55];
int l[M],r[M];
int G[3000][3000],x[3000],y[3000],use[3000];
int finde(int u,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(!use[i]&&G[u][i])
{
use[i]=1;
if(y[i]==0||finde(y[i],n))
{
y[i]=u;
x[u]=i;
return 1;
}
}
}
return 0;
}
int max_match(int cnt1,int cnt2)
{
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
int ans=0;
for(int i=1;i<=cnt1;i++)
{
if(x[i]==0)
{
memset(use,0,sizeof(use));
ans+=finde(i,cnt2);
}
}
return ans;
}
int num[88],vis[66][66];
struct node
{
int v;
node(int vv)
{
v=vv;
}
};
vector<node>edge[M];
int used[66];
int main()
{
int T,m,n,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%s",mp[i]);
int cnt1=0;
for(i=0;i<n;i++)
{
int flag=0;
row[i]=0;
for(j=0;j<m;j++)
{
if(mp[i][j]=='*')
{
if(!flag)
{
row[i]++;
l[++cnt1]=i;
flag=1;
}
edge[cnt1].push_back(j);//记录当前行和一个连续的*的列号
}
if(mp[i][j]=='#')
flag=0;
}
}
int cnt2=0;
for(j=0;j<m;j++)
{
col[j]=0;
int flag=0;
for(i=0;i<n;i++)
{
if(mp[i][j]=='*')
{
if(!flag)
{
col[j]++;
r[++cnt2]=j;
flag=1;
}
vis[i][j]=cnt2;//记录第i行第j列应与右边相连的点的编号
}
if(mp[i][j]=='#')
flag=0;
}
}
memset(G,0,sizeof(G));
for(i=1;i<=cnt1;i++)
{
memset(used,0,sizeof(used));
for(j=0;j<(int)edge[i].size();j++)
used[edge[i][j].v]=1;
for(j=1;j<=cnt2;j++)
{
if(mp[l[i]][r[j]]=='*'&&used[r[j]])
{
G[i][vis[l[i]][r[j]]]=1;
}
}
}
int ans=max_match(cnt1,cnt2);
printf("%d\n",ans);
for(i=1;i<=cnt1;i++)
edge[i].clear();
}
return 0;
}

二分多重匹配(HDU5093)的更多相关文章

  1. hdu 1669(二分+多重匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 思路:由于要求minimize the size of the largest group,由此 ...

  2. poj 2112(二分+多重匹配)

    题目链接:http://poj.org/problem?id=2112 思路:由于要求奶牛走的最远距离的最短路程,显然我们可以二分距离,如果奶牛与挤奶器的距离小于等于limit的情况下,能够满足,则在 ...

  3. poj-2289.jamies contact groups(二分答案 + 二分多重匹配)

    Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 9227   Accepted: ...

  4. [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)

    http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...

  5. 稳定的奶牛分配 && 二分图多重匹配+二分答案

    题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...

  6. POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)

    题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远   输入数据: 第一行三个数 K, C, M  接下来是   ...

  7. POJ 2289 Jamie's Contact Groups(多重匹配+二分)

    题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...

  8. POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)

    解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这 ...

  9. POJ 2289(多重匹配+二分)

    POJ 2289(多重匹配+二分) 把n个人,分到m个组中.题目给出每一个人可以被分到的那些组.要求分配完毕后,最大的那一个组的人数最小. 用二分查找来枚举. #include<iostream ...

随机推荐

  1. 一个Delphi7的BUG

    combobox有个属性DropDownCount可以控制显示的下拉数量, 但是 在Delphi7中, TCombobox或者任何从TCustomComboBox继承下来的类, 在windows7环境 ...

  2. useful tools, excel import mysql,swagger

    http://www.th7.cn/Program/java/201602/765943.shtml------swagger http://blog.csdn.net/csfreebird/arti ...

  3. dubbo,gradle,spring build from source

    https://github.com/alibaba/dubbo http://www.csdn.net/article/2012-11-13/2811827-alibaba-Dubbo-Distri ...

  4. java JDK8 学习笔记——第17章 反射与类加载器

    第十七章 反射与类加载器 17.1 运用反射 反射:.class文档反映了类基本信息,从Class等API取得类信息的方式称为反射. 17.1.1 Class与.class文档 1.java.lang ...

  5. 简单的form表单

    效果 html <ul class="edit_list"> <li><em>*</em><span class=" ...

  6. linux power button

    最近需要使用到power button按键,linux中有gpio keys的机制,只要注册即可. device注册 arch/arm/mach-mx6/board-mx6q_sabresd.c #d ...

  7. CDN的原理以及其中的一些技术

    本质:DNS解析CNAME时最终会请求到阿里CDN的DNS服务器上,阿里CDN的DNS服务器会判断请求ip的物理区域是哪里,同时根据各CDN节点的压力做全局的负载均衡 返回合适CDN节点的ip. ht ...

  8. AppStore提审攻略

    导语: AppStore后台上传新产品的时候需要填写很多资料,看似很复杂,其实搞清楚之后也比较简单. 下面就给大家介绍一下  iTunes Connect 后台上传新APP时需要提交的资料.不要等待需 ...

  9. Vim编辑器-批量注释与反注释

    标签:linuxLinuxLINUXvimVIMVim编程 2013-01-10 09:52 27517人阅读 评论(3) 收藏 举报  分类: Linux(18)  版权声明:本文为博主原创文章,未 ...

  10. [ROS]2 尝试编译OrbSLAM

    主要参考http://blog.csdn.net/dourenyin/article/details/48055441 1.编译g2o的过程中, 先是出现Eigen3依赖项的问题,要求Eigen3最低 ...